Files
jarl/internal/server/h_nscheck.go
T
abrown38andClaude Opus 5.5 5630e7a848 Add DNSSEC signing, key backups and Porkbun support
Zones get a DNSSEC setting: named signs them with BIND's default (or a
custom) dnssec-policy and inline signing, with keys in a per-view key
directory. Jarl shows the keys, the DS record and when it is safe to
publish it, checks the parent's DS records (check-ds, which also tells
named), and refuses steps that would break a signed domain: switching
straight to unsigned, finishing a turn-off too early, deleting or moving
a signed zone, TTLs above max-zone-ttl. Turning off goes through the
insecure policy; wipe starts over once the parent has no DS.

Backups store the DNSSEC keys and restore puts them back for named; the
backup unit gets CAP_DAC_READ_SEARCH to read them. Jarl raises a zone's
serial above named's whenever it writes an older one (after a restore).
The serials check compares the signed serial.

With a Porkbun API key in jarl.yaml, the registrar's name servers, glue
records and DS records can be shown and changed, each change checked
first (dry run, force to override).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 21:19:04 +00:00

288 lines
9.1 KiB
Go

package server
// Name server checks (internal/nscheck): GET /zones/{zone}/serials and
// GET /zones/{zone}/delegation. Both only run when asked for (the zone
// page's Name servers tab, 'jarl zone serials' and 'jarl zone
// delegation'); nothing checks in the background.
import (
"context"
"fmt"
"net"
"net/http"
"net/netip"
"strings"
"time"
"jarl/internal/api"
"jarl/internal/auth"
"jarl/internal/dnsname"
"jarl/internal/dnssec"
"jarl/internal/model"
"jarl/internal/namedconf"
"jarl/internal/nscheck"
)
// checker is the nscheck.Checker the handlers use (tests replace it).
func (s *Server) checker() *nscheck.Checker {
if s.nsChecker != nil {
c := *s.nsChecker
return &c
}
return &nscheck.Checker{}
}
func (s *Server) zoneSerials(w http.ResponseWriter, r *http.Request, p *auth.Principal) error {
st, err := s.state(r)
if err != nil {
return err
}
z, err := s.zoneByPath(r, st, p)
if err != nil {
return err
}
resp := api.SerialsResponse{Zone: z.Name, View: st.ViewName(z), Type: z.Type, Servers: []nscheck.Result{}, Notes: []string{}}
var targets []nscheck.Target
switch z.Type {
case model.ZonePrimary:
targets = append(targets, nsTargets(st, z)...)
an, notes := remoteTargets(st, inheritedOption(st, z, "also-notify"), "also-notify")
targets = append(targets, an...)
resp.Notes = append(resp.Notes, notes...)
case model.ZoneSecondary, model.ZoneMirror:
pr, notes := remoteTargets(st, st.FindOption(model.ScopeZone, z.ID, "primaries"), "primaries")
targets = append(targets, pr...)
resp.Notes = append(resp.Notes, notes...)
default:
return badRequest("only primary, secondary and mirror zones have serials to compare (this is a %s zone)", z.Type)
}
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
resp.Serial, resp.SerialSource, err = s.localSerial(ctx, st, z)
if err != nil {
resp.Notes = append(resp.Notes, "named on this server didn't report the zone's serial: "+err.Error())
}
if z.Signed() && resp.SerialSource == "jarl" {
// named gives the signed zone its own serial, ahead of Jarl's.
resp.Notes = append(resp.Notes, "The zone is signed and named wasn't asked: other servers serve the signed zone's serial, which named sets and which is usually ahead of Jarl's.")
resp.Serial = nil
}
if len(targets) == 0 {
if z.Type == model.ZonePrimary {
resp.Notes = append(resp.Notes, "The zone has no NS records or also-notify targets to check.")
} else {
resp.Notes = append(resp.Notes, "The zone has no primaries to check.")
}
}
if resp.View != "" {
resp.Notes = append(resp.Notes, fmt.Sprintf("The zone is in view %s. Other servers answer from the view that matches this server's address (or key), as they do for its zone transfers and NOTIFYs.", resp.View))
}
resp.Servers = s.checker().Serials(ctx, z.Name, resp.Serial, targets)
if len(resp.Servers) > 0 && !hasOther(resp.Servers) {
resp.Notes = append(resp.Notes, "Only this server is listed, so there is nothing to compare with.")
}
resp.CheckedAt = time.Now()
writeJSON(w, http.StatusOK, resp)
return nil
}
func (s *Server) zoneDelegation(w http.ResponseWriter, r *http.Request, p *auth.Principal) error {
st, err := s.state(r)
if err != nil {
return err
}
z, err := s.zoneByPath(r, st, p)
if err != nil {
return err
}
if z.Type != model.ZonePrimary {
return badRequest("the delegation check compares the parent's delegation with the zone's NS records, so it needs a primary zone (this is a %s zone; check it where its primary is)", z.Type)
}
c := s.checker()
if v := r.URL.Query().Get("resolver"); v != "" {
// An address only, always port 53: this is a DNS resolver.
a, err := netip.ParseAddr(v)
if err != nil {
return badRequest("resolver must be an IP address, e.g. 1.1.1.1 or 9.9.9.9")
}
c.Resolver = net.JoinHostPort(a.String(), "53")
}
var ns []string
for _, rec := range st.ZoneRecords(z.ID) {
if rec.Type == "NS" && rec.Name == z.Name {
ns = append(ns, rec.Data)
}
}
addrs := map[string][]string{}
for _, n := range ns {
if a := jarlAddresses(st, z.ViewID, dnsname.Fqdn(n)); len(a) > 0 {
addrs[dnsname.Fqdn(n)] = a
}
}
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
ref, _, _ := s.localSerial(ctx, st, z)
resp := api.DelegationResponse{DelegationReport: c.Delegation(ctx, z.Name, ns, addrs, ref, s.signing(ctx, st, z)), View: st.ViewName(z), Notes: []string{}, CheckedAt: time.Now()}
if resp.View != "" {
resp.Notes = append(resp.Notes, fmt.Sprintf("Compared with the zone in view %s. Resolvers on the internet get the view that matches their address; make sure that's this one.", resp.View))
}
writeJSON(w, http.StatusOK, resp)
return nil
}
// signing describes how Jarl signs z, for comparing with the parent's DS
// records: the DS of every key-signing key named still publishes.
func (s *Server) signing(ctx context.Context, st *model.State, z *model.Zone) *nscheck.Signing {
sig := &nscheck.Signing{Policy: z.DNSSECPolicy}
for _, k := range s.eng.DNSSEC(ctx, st, z).Keys {
if k.DS != nil && k.DNSKEYState != dnssec.Hidden {
sig.DS = append(sig.DS, *k.DS)
}
}
return sig
}
// localSerial is the serial this server serves for z: named's, or for a
// primary zone Jarl's when named isn't asked.
func (s *Server) localSerial(ctx context.Context, st *model.State, z *model.Zone) (*uint32, string, error) {
serial, ok, err := s.eng.LoadedSerial(ctx, st, z)
if ok {
return &serial, "named", nil
}
if z.Type == model.ZonePrimary {
v := z.SOA.Serial
return &v, "jarl", err
}
return nil, "", err
}
// nsTargets are the name servers in the zone's apex NS records, with the
// addresses Jarl has for them in the same view (or nil, to be looked up).
func nsTargets(st *model.State, z *model.Zone) []nscheck.Target {
var out []nscheck.Target
for _, rec := range st.ZoneRecords(z.ID) {
if rec.Type != "NS" || rec.Name != z.Name {
continue
}
name := dnsname.Fqdn(rec.Data)
out = append(out, nscheck.Target{Name: name, Addresses: jarlAddresses(st, z.ViewID, name), Source: "NS record"})
}
return out
}
// jarlAddresses returns the A and AAAA records for name in the primary
// zones of a view (nil when Jarl has none).
func jarlAddresses(st *model.State, viewID int64, name string) []string {
var out []string
for _, z := range st.ZonesInView(viewID) {
if !z.HasRecords() || !dnsname.IsSubdomain(name, z.Name) {
continue
}
for _, rec := range st.ZoneRecords(z.ID) {
if rec.Name == name && (rec.Type == "A" || rec.Type == "AAAA") {
out = append(out, rec.Data)
}
}
}
return out
}
// inheritedOption finds an option for z as named does: the zone's own,
// else its view's, else the server's.
func inheritedOption(st *model.State, z *model.Zone, name string) *model.Option {
if o := st.FindOption(model.ScopeZone, z.ID, name); o != nil {
return o
}
if z.ViewID != 0 {
if o := st.FindOption(model.ScopeView, z.ViewID, name); o != nil {
return o
}
}
return st.FindOption(model.ScopeOptions, 0, name)
}
// remoteTargets turns an also-notify or primaries option into targets:
// addresses with their port and key, following named remote-servers
// lists.
func remoteTargets(st *model.State, o *model.Option, source string) ([]nscheck.Target, []string) {
if o == nil {
return nil, nil
}
var out []nscheck.Target
var notes []string
var walk func(name, value string, port string, depth int)
walk = func(name, value, port string, depth int) {
stmt, err := namedconf.ParseOptionValue(name, value)
if err != nil {
notes = append(notes, fmt.Sprintf("couldn't read %s: %v", name, err))
return
}
vals := stmt.Values()
for i := 0; i+1 < len(vals); i++ {
if strings.EqualFold(vals[i], "port") {
port = vals[i+1]
}
}
blk := stmt.FirstBlock()
if blk == nil {
return
}
for _, el := range blk.Stmts {
if len(el.Items) == 0 || el.Items[0].Value == nil {
continue
}
first := el.Items[0].Value.Text
t := nscheck.Target{Source: source, Port: port}
words := el.Values()
for i := 0; i+1 < len(words); i++ {
switch strings.ToLower(words[i]) {
case "port":
t.Port = words[i+1]
case "key":
if k := st.KeyByName(words[i+1]); k != nil {
t.Key = &nscheck.Key{Name: k.Name, Algorithm: k.Algorithm, Secret: k.Secret}
}
}
}
if a, err := netip.ParseAddr(first); err == nil {
t.Addresses = []string{a.String()}
out = append(out, t)
continue
}
// A named list of servers.
if depth >= 5 {
continue
}
if lo := remoteList(st, first); lo != nil {
walk(lo.Name, lo.Value, port, depth+1)
} else {
notes = append(notes, fmt.Sprintf("%s refers to %q, which isn't an address or a server list Jarl knows", source, first))
}
}
}
walk(o.Name, o.Value, "", 0)
return out, notes
}
// remoteList finds a top-level remote-servers (or primaries) list.
func remoteList(st *model.State, name string) *model.Option {
for _, o := range st.ScopedOptions(model.ScopeTopLevel, 0) {
switch o.Name {
case "remote-servers", "primaries", "masters":
if f := strings.Fields(o.Value); len(f) > 0 && strings.Trim(f[0], `"`) == name {
return o
}
}
}
return nil
}
func hasOther(rs []nscheck.Result) bool {
for _, r := range rs {
if r.Status != "self" {
return true
}
}
return false
}