feat: implement dns record model and zone checks
- feat: implement dns record model and zone checks - feat: implement zone file import and export
This commit is contained in:
1 parent
5ac8f98f10
commit
7ba68aba15
18 files changed
+2590
-21
No files matched your search
@@ -0,0 +1,141 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func checkZone(zone string, rows []recordRow, now time.Time) []zoneProblem {
|
||||
var problems []zoneProblem
|
||||
for _, row := range rows {
|
||||
problems = append(problems, checkRecord(row, now)...)
|
||||
}
|
||||
ruleErrors, warnings := zoneRuleProblems(zone, rows, summarizeZone(rows), false)
|
||||
problems = append(problems, ruleErrors...)
|
||||
return append(problems, warnings...)
|
||||
}
|
||||
|
||||
func checkRecord(row recordRow, now time.Time) []zoneProblem {
|
||||
var problems []zoneProblem
|
||||
addError := func(code, message string) {
|
||||
problems = append(problems, recordProblem(row, "error", code, message))
|
||||
}
|
||||
warn := func(code, message string) {
|
||||
problems = append(problems, recordProblem(row, "warning", code, message))
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(row.Zone, ".") {
|
||||
addError("zone_not_fqdn", "stored zone is missing its trailing dot")
|
||||
}
|
||||
if row.Zone != strings.ToLower(row.Zone) {
|
||||
addError("zone_case", "stored zone contains uppercase letters")
|
||||
}
|
||||
if _, err := normalizeZone(row.Zone); err != nil {
|
||||
addError("field_invalid", "zone: "+err.Error())
|
||||
}
|
||||
if relative, _, err := normalizeOwner(row.Zone, row.Name); err != nil {
|
||||
addError("field_invalid", "name: "+err.Error())
|
||||
} else if relative != strings.ToLower(row.Name) {
|
||||
addError("field_invalid", "name must be stored in relative form with an empty apex")
|
||||
} else if err := validateRecordOwner(row.RecordType, relative); err != nil && row.RecordType != "SOA" && row.RecordType != "CNAME" {
|
||||
addError("field_invalid", "name: "+err.Error())
|
||||
}
|
||||
if row.Name != strings.ToLower(row.Name) {
|
||||
warn("name_case", "stored owner name contains uppercase letters")
|
||||
}
|
||||
switch {
|
||||
case !row.TTL.Valid:
|
||||
addError("ttl_null", "stored TTL is NULL")
|
||||
case row.TTL.Int64 < 0:
|
||||
addError("ttl_negative", "stored TTL is negative")
|
||||
case row.TTL.Int64 > maxRecordSeconds:
|
||||
addError("field_invalid", "ttl: exceeds 2147483647")
|
||||
case row.TTL.Int64 == 0:
|
||||
warn("ttl_zero", "TTL zero is served as 360 seconds")
|
||||
}
|
||||
|
||||
_, knownType := recordRules[row.RecordType]
|
||||
if !knownType {
|
||||
addError("type_unknown", "stored record type is unsupported")
|
||||
}
|
||||
if !row.Content.Valid {
|
||||
addError("content_null", "stored content is NULL")
|
||||
return problems
|
||||
}
|
||||
if !knownType {
|
||||
var object map[string]json.RawMessage
|
||||
if err := json.Unmarshal([]byte(row.Content.String), &object); err != nil || object == nil {
|
||||
addError("content_json", "stored content must be a JSON object")
|
||||
}
|
||||
return problems
|
||||
}
|
||||
decoded, err := inspectRecordContent(row.RecordType, row.Content.String)
|
||||
if err != nil {
|
||||
addError("content_json", err.Error())
|
||||
return problems
|
||||
}
|
||||
if row.RecordType == "MX" && hasContentKey(decoded.object, "priority") && !hasContentKey(decoded.object, "preference") {
|
||||
addError("mx_priority_key", "MX uses the preference key")
|
||||
}
|
||||
normalized, fields := normalizeRecordData(row.Zone, decoded.data)
|
||||
for key, message := range decoded.fields {
|
||||
fields[key] = message
|
||||
}
|
||||
targets := recordDomainFields(decoded.data)
|
||||
for _, key := range slices.Sorted(maps.Keys(targets)) {
|
||||
name := targets[key]
|
||||
if name != "" && !strings.HasSuffix(name, ".") {
|
||||
addError("name_not_fqdn", key+" must have a trailing dot")
|
||||
} else if key == "MBox" && strings.Contains(name, "@") {
|
||||
fields[key] = "must be stored as a DNS name"
|
||||
}
|
||||
}
|
||||
for _, key := range slices.Sorted(maps.Keys(fields)) {
|
||||
addError("field_invalid", key+": "+fields[key])
|
||||
}
|
||||
if soa, ok := decoded.data.(soaData); ok && soa.Serial == 0 && fields["serial"] == "" {
|
||||
warn("soa_dynamic_serial", "SOA uses a dynamic Unix time serial")
|
||||
}
|
||||
if canonical, _ := encodeRecord(normalized); len(fields) == 0 && canonical != row.Content.String {
|
||||
warn("content_not_canonical", "stored content differs from its canonical form")
|
||||
}
|
||||
if decoded.decodeErr == nil && row.TTL.Valid && row.TTL.Int64 >= 0 && row.TTL.Int64 <= int64(^uint32(0)) {
|
||||
rr, buildErr := buildRecordRR(row, decoded.data, now)
|
||||
if buildErr == nil && rr != nil && packRecordRR(rr) != nil {
|
||||
addError("pack_failed", "the plugin's resource record cannot be packed")
|
||||
}
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
func hasContentKey(object map[string]json.RawMessage, key string) bool {
|
||||
for name := range object {
|
||||
if strings.EqualFold(name, key) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func recordDomainFields(data any) map[string]string {
|
||||
switch v := data.(type) {
|
||||
case cnameData:
|
||||
return map[string]string{"host": v.Host}
|
||||
case nsData:
|
||||
return map[string]string{"host": v.Host}
|
||||
case mxData:
|
||||
return map[string]string{"host": v.Host}
|
||||
case srvData:
|
||||
return map[string]string{"target": v.Target}
|
||||
case soaData:
|
||||
return map[string]string{"ns": v.NS, "MBox": v.MBox}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user