Create basic forms for DKIM, DMARC, MTA-STS and TLS-RPT

This commit is contained in:
nemunaire 2023-12-06 04:50:30 +01:00
parent 01f39f3188
commit df76dd5476
7 changed files with 473 additions and 20 deletions

View File

@ -237,12 +237,10 @@ func email_analyze(a *svcs.Analyzer) (err error) {
}
if record.Type == "TXT" {
newfields := strings.Split(record.GetTargetTXTJoined(), ";")
for i, field := range newfields {
newfields[i] = strings.TrimSpace(field)
err = service.DKIM[selector].Analyze(record.GetTargetTXTJoined())
if err != nil {
return
}
service.DKIM[selector].Fields = append(service.DKIM[selector].Fields, newfields...)
}
err = a.UseRR(record, domain, service)
@ -258,7 +256,10 @@ func email_analyze(a *svcs.Analyzer) (err error) {
}
if record.Type == "TXT" {
service.DMARC.Fields = append(service.DMARC.Fields, strings.Split(strings.TrimPrefix(record.GetTargetTXTJoined(), "v=DMARC1;"), ";")...)
err = service.DMARC.Analyze(record.GetTargetTXTJoined())
if err != nil {
return
}
}
err = a.UseRR(record, domain, service)
@ -274,7 +275,10 @@ func email_analyze(a *svcs.Analyzer) (err error) {
}
if record.Type == "TXT" {
service.MTA_STS.Fields = append(service.MTA_STS.Fields, strings.Split(record.GetTargetTXTJoined(), ";")...)
err = service.MTA_STS.Analyze(record.GetTargetTXTJoined())
if err != nil {
return
}
}
err = a.UseRR(record, domain, service)
@ -283,14 +287,25 @@ func email_analyze(a *svcs.Analyzer) (err error) {
}
}
// Is there MTA-STS record?
// Is there TLS-RPT record?
for _, record := range a.SearchRR(svcs.AnalyzerRecordFilter{Type: dns.TypeTXT, Domain: "_smtp._tls." + domain}) {
// rfc8460: 3. records that do not begin with "v=TLSRPTv1;" are discarded
if !strings.HasPrefix(record.GetTargetTXTJoined(), "v=TLSRPT") {
continue
}
if service.TLS_RPT == nil {
service.TLS_RPT = &svcs.TLS_RPT{}
} else {
// rfc8460: 3. If the number of resulting records is not one, senders MUST assume the recipient domain does not implement TLSRPT.
continue
}
if record.Type == "TXT" {
service.TLS_RPT.Fields = append(service.TLS_RPT.Fields, strings.Split(record.GetTargetTXTJoined(), ";")...)
err = service.TLS_RPT.Analyze(record.GetTargetTXTJoined())
if err != nil {
return
}
}
err = a.UseRR(record, domain, service)

View File

@ -32,13 +32,92 @@
package svcs
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
)
type DKIM struct {
Fields []string
Version uint `json:"version" happydomain:"label=Version,placeholder=1,required,description=The version of DKIM to use.,default=1,hidden"`
AcceptableHash []string `json:"h" happydomain:"label=Hash Algorithms,choices=*;sha1;sha256"`
KeyType string `json:"k" happydomain:"label=Key Type,choices=rsa"`
Notes string `json:"n" happydomain:"label=Notes,description=Notes intended for a foreign postmaster"`
PublicKey []byte `json:"p" happydomain:"label=Public Key,placeholder=a0b1c2d3e4f5==,required"`
ServiceType []string `json:"s" happydomain:"label=Service Types,choices=*;email"`
Flags []string `json:"t" happydomain:"label=Flags,choices=y;s"`
}
func (t *DKIM) Analyze(txt string) error {
fields := analyseFields(txt)
if v, ok := fields["v"]; ok {
if !strings.HasPrefix(v, "DKIM") {
return fmt.Errorf("not a valid DKIM record: should begin with v=DKIMv1, seen v=%q", v)
}
version, err := strconv.ParseUint(v[4:], 10, 32)
if err != nil {
return fmt.Errorf("not a valid DKIM record: bad version number: %w", err)
}
t.Version = uint(version)
} else {
return fmt.Errorf("not a valid DKIM record: version not found")
}
if h, ok := fields["h"]; ok {
t.AcceptableHash = strings.Split(h, ":")
} else {
t.AcceptableHash = []string{"*"}
}
if k, ok := fields["k"]; ok {
t.KeyType = k
}
if n, ok := fields["n"]; ok {
t.Notes = n
}
if p, ok := fields["p"]; ok {
var err error
t.PublicKey, err = base64.StdEncoding.DecodeString(p)
if err != nil {
return fmt.Errorf("not a valid DKIM record: public key is not base64 valid: %w", err)
}
}
if s, ok := fields["s"]; ok {
t.ServiceType = strings.Split(s, ":")
} else {
t.ServiceType = []string{"*"}
}
if f, ok := fields["t"]; ok {
t.Flags = strings.Split(f, ":")
}
return nil
}
func (t *DKIM) String() string {
return strings.Join(t.Fields, "; ")
fields := []string{
fmt.Sprintf("v=DKIM%d", t.Version),
}
if len(t.AcceptableHash) > 1 || (len(t.AcceptableHash) > 0 && t.AcceptableHash[0] != "*") {
fields = append(fields, fmt.Sprintf("h=%s", strings.Join(t.AcceptableHash, ":")))
}
if t.KeyType != "" {
fields = append(fields, fmt.Sprintf("k=%s", t.KeyType))
}
if t.Notes != "" {
fields = append(fields, fmt.Sprintf("n=%s", t.Notes))
}
if len(t.PublicKey) > 0 {
fields = append(fields, fmt.Sprintf("p=%s", base64.StdEncoding.EncodeToString(t.PublicKey)))
}
if len(t.ServiceType) > 1 || (len(t.ServiceType) > 0 && t.ServiceType[0] != "*") {
fields = append(fields, fmt.Sprintf("s=%s", strings.Join(t.ServiceType, ":")))
}
if len(t.Flags) > 0 {
fields = append(fields, fmt.Sprintf("t=%s", strings.Join(t.Flags, ":")))
}
return strings.Join(fields, ";")
}

View File

@ -32,17 +32,148 @@
package svcs
import (
"fmt"
"strconv"
"strings"
"git.happydns.org/happyDomain/services/common"
)
type DMARC struct {
Fields []string
Version uint `json:"version" happydomain:"label=Version,placeholder=1,required,description=The version of DMARC to use.,default=1,hidden"`
Request string `json:"p" happydomain:"label=Requested Mail Receiver policy,choices=none;quarantine;reject,description=Indicates the policy to be enacted by the Receiver,required"`
SRequest string `json:"sp" happydomain:"label=Requested Mail Receiver policy for all subdomains,choices=;none;quarantaine;reject,description=Indicates the policy to be enacted by the Receiver when it receives mail for a subdomain"`
AURI []string `json:"rua" happydomain:"label=RUA,description=Addresses for aggregate feedback,placeholder=mailto:name@example.com"`
FURI []string `json:"ruf" happydomain:"label=RUF,description=Addresses for message-specific failure information,placeholder=mailto:name@example.com"`
ADKIM bool `json:"adkim" happydomain:"label=Strict DKIM Alignment"`
ASPF bool `json:"aspf" happydomain:"label=Strict SPF Alignment"`
AInterval common.Duration `json:"ri" happydomain:"label=Interval between aggregate reports"`
FailureOptions []string `json:"fo" happydomain:"label=Failure reporting options,choices=0;1;d;s"`
RegisteredFormats []string `json:"rf" happydomain:"label=Format of the failure reports,choices=;afrf"`
Percent uint8 `json:"pct" happydomain:"label=Policy applies on,description=Percentage of messages to which the DMARC policy is to be applied.,unit=%"`
}
func analyseFields(txt string) map[string]string {
ret := map[string]string{}
for _, f := range strings.Split(txt, ";") {
f = strings.TrimSpace(f)
kv := strings.SplitN(f, "=", 2)
if len(kv) == 1 {
ret[strings.TrimSpace(kv[0])] = ""
} else {
ret[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
}
}
return ret
}
func (t *DMARC) Analyze(txt string) error {
fields := analyseFields(txt)
if v, ok := fields["v"]; ok {
if !strings.HasPrefix(v, "DMARC") {
return fmt.Errorf("not a valid DMARC record: should begin with v=DMARCv1, seen v=%q", v)
}
version, err := strconv.ParseUint(v[5:], 10, 32)
if err != nil {
return fmt.Errorf("not a valid DMARC record: bad version number: %w", err)
}
t.Version = uint(version)
} else {
return fmt.Errorf("not a valid DMARC record: version not found")
}
if p, ok := fields["p"]; ok {
t.Request = p
}
if sp, ok := fields["sp"]; ok {
t.SRequest = sp
}
if rua, ok := fields["rua"]; ok {
t.AURI = strings.Split(rua, ",")
}
if ruf, ok := fields["ruf"]; ok {
t.FURI = strings.Split(ruf, ",")
}
if adkim, ok := fields["adkim"]; ok && adkim == "s" {
t.ADKIM = true
}
if aspf, ok := fields["aspf"]; ok && aspf == "s" {
t.ASPF = true
}
if ri, ok := fields["ri"]; ok {
v, err := strconv.ParseUint(ri, 10, 32)
if err != nil {
return fmt.Errorf("not a valid DMARC record: bad interval value (ri): %w", err)
}
t.AInterval = common.Duration(v)
} else {
t.AInterval = 86400
}
if fo, ok := fields["fo"]; ok {
t.FailureOptions = strings.Split(fo, ":")
}
if rf, ok := fields["rf"]; ok {
t.RegisteredFormats = strings.Split(rf, ":")
}
if pct, ok := fields["pct"]; ok {
v, err := strconv.ParseUint(pct, 10, 8)
if err != nil {
return fmt.Errorf("not a valid DMARC record: bad percent value (prc): %w", err)
}
t.Percent = uint8(v)
} else {
t.Percent = 100
}
return nil
}
func (t *DMARC) String() string {
var fields = t.Fields[:]
if len(t.Fields) == 0 || t.Fields[0] != "v=DMARC1" {
fields = append([]string{"v=DMARC1"}, fields...)
fields := []string{
fmt.Sprintf("v=DMARC%d", t.Version),
}
if t.Request != "" {
fields = append(fields, fmt.Sprintf("p=%s", t.Request))
}
if t.SRequest != "" {
fields = append(fields, fmt.Sprintf("sp=%s", t.SRequest))
}
if len(t.AURI) > 0 {
fields = append(fields, fmt.Sprintf("rua=%s", strings.Join(t.AURI, ",")))
}
if len(t.FURI) > 0 {
fields = append(fields, fmt.Sprintf("ruf=%s", strings.Join(t.FURI, ",")))
}
if t.ADKIM {
fields = append(fields, "adkim=s")
} else {
fields = append(fields, "adkim=r")
}
if t.ASPF {
fields = append(fields, "aspf=s")
} else {
fields = append(fields, "aspf=r")
}
if t.AInterval != 86400 && t.AInterval != 0 {
fields = append(fields, fmt.Sprintf("ri=%d", t.AInterval))
}
if len(t.FailureOptions) > 0 {
fields = append(fields, fmt.Sprintf("fo=%s", strings.Join(t.FailureOptions, ":")))
}
if len(t.RegisteredFormats) > 0 {
fields = append(fields, fmt.Sprintf("rf=%s", strings.Join(t.RegisteredFormats, ":")))
}
if t.Percent != 100 {
fields = append(fields, fmt.Sprintf("pct=%d", t.Percent))
}
return strings.Join(fields, ";")
}

View File

@ -32,13 +32,49 @@
package svcs
import (
"fmt"
"strconv"
"strings"
)
type MTA_STS struct {
Fields []string
Version uint `json:"version" happydomain:"label=Version,placeholder=1,required,description=The version of MTA-STS to use.,default=1,hidden"`
Id string `json:"id" happydomain:"label=Policy Identifier,placeholder=,description=A short string used to track policy updates."`
}
func (t *MTA_STS) Analyze(txt string) error {
fields := strings.Split(txt, ";")
if len(fields) < 2 {
return fmt.Errorf("not a valid MTA-STS record: should have a version AND a id, only one field found")
}
if len(fields) > 3 || (len(fields) == 3 && fields[2] != "") {
return fmt.Errorf("not a valid MTA-STS record: should have exactly 2 fields: seen %d", len(fields))
}
for i := range fields {
fields[i] = strings.TrimSpace(fields[i])
}
if !strings.HasPrefix(fields[0], "v=STSv") {
return fmt.Errorf("not a valid MTA-STS record: should begin with v=STSv1, seen %q", fields[0])
}
version, err := strconv.ParseUint(fields[0][6:], 10, 32)
if err != nil {
return fmt.Errorf("not a valid MTA-STS record: bad version number: %w", err)
}
t.Version = uint(version)
if !strings.HasPrefix(fields[1], "id=") {
return fmt.Errorf("not a valid MTA-STS record: expected id=, found %q", fields[1])
}
t.Id = strings.TrimSpace(strings.TrimPrefix(fields[1], "id="))
return nil
}
func (t *MTA_STS) String() string {
return strings.Join(t.Fields, ";")
return fmt.Sprintf("v=STSv%d; id=%s", t.Version, t.Id)
}

View File

@ -1,4 +1,4 @@
// Copyright or © or Copr. happyDNS (2020)
// Copyright or © or Copr. happyDNS (2023)
//
// contact@happydomain.org
//
@ -32,13 +32,53 @@
package svcs
import (
"fmt"
"strconv"
"strings"
)
type TLS_RPT struct {
Fields []string
Version uint `json:"version" happydomain:"label=Version,placeholder=1,required,description=The version of TLSRPT to use.,default=1,hidden"`
Rua []string `json:"rua" happydomain:"label=Aggregate Report URI,placeholder=https://example.com/path|mailto:name@example.com"`
}
func (t *TLS_RPT) Analyze(txt string) error {
fields := strings.Split(txt, ";")
if len(fields) < 2 {
return fmt.Errorf("not a valid TLS-RPT record: should have a version AND a rua, only one field found")
}
if len(fields) > 3 || (len(fields) == 3 && fields[2] != "") {
return fmt.Errorf("not a valid TLS-RPT record: should have exactly 2 fields: seen %d", len(fields))
}
for i := range fields {
fields[i] = strings.TrimSpace(fields[i])
}
if !strings.HasPrefix(fields[0], "v=TLSRPTv") {
return fmt.Errorf("not a valid TLS-RPT record: should begin with v=TLSRPTv1, seen %q", fields[0])
}
version, err := strconv.ParseUint(fields[0][9:], 10, 32)
if err != nil {
return fmt.Errorf("not a valid TLS-RPT record: bad version number: %w", err)
}
t.Version = uint(version)
if !strings.HasPrefix(fields[1], "rua=") {
return fmt.Errorf("not a valid TLS-RPT record: expected rua=, found %q", fields[1])
}
t.Rua = strings.Split(strings.TrimPrefix(fields[1], "rua="), ",")
for i := range t.Rua {
t.Rua[i] = strings.TrimSpace(t.Rua[i])
}
return nil
}
func (t *TLS_RPT) String() string {
return strings.Join(t.Fields, ";")
return fmt.Sprintf("v=TLSRPTv%d; rua=%s", t.Version, strings.Join(t.Rua, ","))
}

View File

@ -0,0 +1,151 @@
// Copyright or © or Copr. happyDNS (2023)
//
// contact@happydomain.org
//
// This software is a computer program whose purpose is to provide a modern
// interface to interact with DNS systems.
//
// This software is governed by the CeCILL license under French law and abiding
// by the rules of distribution of free software. You can use, modify and/or
// redistribute the software under the terms of the CeCILL license as
// circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy, modify
// and redistribute granted by the license, users are provided only with a
// limited warranty and the software's author, the holder of the economic
// rights, and the successive licensors have only limited liability.
//
// In this respect, the user's attention is drawn to the risks associated with
// loading, using, modifying and/or developing or reproducing the software by
// the user in light of its specific status of free software, that may mean
// that it is complicated to manipulate, and that also therefore means that it
// is reserved for developers and experienced professionals having in-depth
// computer knowledge. Users are therefore encouraged to load and test the
// software's suitability as regards their requirements in conditions enabling
// the security of their systems and/or data to be ensured and, more generally,
// to use and operate it in the same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
package database
import (
"fmt"
"log"
"strings"
"git.happydns.org/happyDomain/model"
"git.happydns.org/happyDomain/services"
)
func migrateFrom5(s *LevelDBStorage) (err error) {
err = migrateFrom5_analyseEMailSvc(s)
if err != nil {
return
}
err = s.Tidy()
if err != nil {
return
}
return
}
type ServiceCombined struct {
Service map[string]interface{}
happydns.ServiceMeta
}
type Zone struct {
happydns.ZoneMeta
Services map[string][]*ServiceCombined `json:"services"`
}
func migrateFrom5_analyseEMailSvc(s *LevelDBStorage) (err error) {
var zone *Zone
iter := s.search("domain.zone-")
for iter.Next() {
zone = &Zone{}
err = s.get(string(iter.Key()), &zone)
changed := false
for dn, zServices := range zone.Services {
for ksvc, svc := range zServices {
if svc.Type == "abstract.EMail" {
// DKIM
newdkim := map[string]*svcs.DKIM{}
if olddkim, ok := svc.Service["dkim"].(map[string]interface{}); ok {
for k, v := range olddkim {
if oldv, ok := v.(map[string]interface{}); ok {
var fields []string
for _, f := range oldv["Fields"].([]interface{}) {
fields = append(fields, f.(string))
}
newdkim[k] = &svcs.DKIM{}
newdkim[k].Analyze(strings.Join(fields, ";"))
changed = true
}
}
}
zone.Services[dn][ksvc].Service["dkim"] = newdkim
// DMARC
if olddmarc, ok := svc.Service["dmarc"].(map[string]interface{}); ok {
var fields []string
for _, f := range olddmarc["Fields"].([]interface{}) {
fields = append(fields, f.(string))
}
newdmarc := &svcs.DMARC{}
newdmarc.Analyze("v=DMARC1;" + strings.Join(fields, ";"))
zone.Services[dn][ksvc].Service["dmarc"] = newdmarc
changed = true
}
// MTA-STS
if oldsts, ok := svc.Service["mta_sts"].(map[string]interface{}); ok {
var fields []string
for _, f := range oldsts["Fields"].([]interface{}) {
fields = append(fields, f.(string))
}
newsts := &svcs.MTA_STS{}
newsts.Analyze(strings.Join(fields, ";"))
zone.Services[dn][ksvc].Service["mta_sts"] = newsts
changed = true
}
// TLS-RPT
if oldrpt, ok := svc.Service["tls_rpt"].(map[string]interface{}); ok {
var fields []string
for _, f := range oldrpt["Fields"].([]interface{}) {
fields = append(fields, f.(string))
}
newrpt := &svcs.TLS_RPT{}
newrpt.Analyze(strings.Join(fields, ";"))
zone.Services[dn][ksvc].Service["tls_rpt"] = newrpt
changed = true
}
}
}
}
if changed {
err = s.put(string(iter.Key()), zone)
if err != nil {
return fmt.Errorf("unable to write %s: %w", iter.Key(), err)
}
log.Printf("Migrating v4 -> v5: %s (update abstract.EMail)...", iter.Key())
}
}
return nil
}

View File

@ -44,6 +44,7 @@ var migrations []LevelDBMigrationFunc = []LevelDBMigrationFunc{
migrateFrom2,
migrateFrom3,
migrateFrom4,
migrateFrom5,
}
func (s *LevelDBStorage) DoMigration() (err error) {