2018-02-15 12:02:50 -05:00
|
|
|
package models
|
|
|
|
|
2022-06-20 11:34:05 -04:00
|
|
|
import (
|
|
|
|
"strings"
|
2023-11-12 08:46:23 -05:00
|
|
|
|
|
|
|
"github.com/StackExchange/dnscontrol/v4/pkg/txtutil"
|
2022-06-20 11:34:05 -04:00
|
|
|
)
|
2022-06-18 09:58:55 -04:00
|
|
|
|
2021-03-07 13:19:22 -05:00
|
|
|
/*
|
2022-06-20 11:34:05 -04:00
|
|
|
Sadly many providers handle TXT records in strange and non-compliant
|
|
|
|
ways. DNSControl has to handle all of them. Over the years we've
|
|
|
|
tried many things. This explain the current state of the code.
|
|
|
|
|
2023-06-19 17:31:12 -06:00
|
|
|
DNSControl stores the TXT record target as a single string of any length.
|
|
|
|
Providers take care of any splitting, excaping, or quoting.
|
2022-06-20 11:34:05 -04:00
|
|
|
|
2023-06-19 17:31:12 -06:00
|
|
|
NOTE: Older versions of DNSControl stored the TXT record as
|
|
|
|
represented by the provider, which could be a single string, a series
|
|
|
|
of smaller strings, or a single string that is quoted/escaped. This
|
|
|
|
created tons of edge-cases and other distractions.
|
2022-06-20 11:34:05 -04:00
|
|
|
|
2023-06-19 17:31:12 -06:00
|
|
|
If a provider doesn't support certain charactors in a TXT record, use
|
|
|
|
the providers/$PROVIDER/auditrecords.go file to indicate this.
|
|
|
|
DNSControl uses this information to warn users of unsupporrted input,
|
|
|
|
and to skip related integration tests.
|
2021-03-07 13:19:22 -05:00
|
|
|
|
|
|
|
*/
|
2020-11-18 07:05:26 -05:00
|
|
|
|
2021-01-24 21:36:48 +01:00
|
|
|
// HasFormatIdenticalToTXT returns if a RecordConfig has a format which is
|
|
|
|
// identical to TXT, such as SPF. For more details, read
|
|
|
|
// https://tools.ietf.org/html/rfc4408#section-3.1.1
|
|
|
|
func (rc *RecordConfig) HasFormatIdenticalToTXT() bool {
|
2021-03-07 13:19:22 -05:00
|
|
|
return rc.Type == "TXT" || rc.Type == "SPF"
|
2021-01-24 21:36:48 +01:00
|
|
|
}
|
|
|
|
|
2018-02-15 12:02:50 -05:00
|
|
|
// SetTargetTXT sets the TXT fields when there is 1 string.
|
|
|
|
func (rc *RecordConfig) SetTargetTXT(s string) error {
|
|
|
|
if rc.Type == "" {
|
|
|
|
rc.Type = "TXT"
|
2021-03-07 13:19:22 -05:00
|
|
|
} else if !rc.HasFormatIdenticalToTXT() {
|
|
|
|
panic("assertion failed: SetTargetTXT called when .Type is not TXT or compatible type")
|
2018-02-15 12:02:50 -05:00
|
|
|
}
|
2021-03-07 13:19:22 -05:00
|
|
|
|
2023-10-25 13:06:55 -04:00
|
|
|
return rc.SetTarget(s)
|
2018-02-15 12:02:50 -05:00
|
|
|
}
|
|
|
|
|
2023-11-12 21:37:11 -05:00
|
|
|
// SetTargetTXTs sets the TXT fields when there are many strings. They are stored concatenated.
|
2018-02-15 12:02:50 -05:00
|
|
|
func (rc *RecordConfig) SetTargetTXTs(s []string) error {
|
2023-10-25 13:06:55 -04:00
|
|
|
return rc.SetTargetTXT(strings.Join(s, ""))
|
2018-02-15 12:02:50 -05:00
|
|
|
}
|
|
|
|
|
2023-11-12 08:46:23 -05:00
|
|
|
// GetTargetTXTJoined returns the TXT target as one string.
|
2022-06-18 09:58:55 -04:00
|
|
|
func (rc *RecordConfig) GetTargetTXTJoined() string {
|
2023-11-01 13:52:23 -04:00
|
|
|
return rc.target
|
2022-06-18 09:58:55 -04:00
|
|
|
}
|
|
|
|
|
2023-11-12 08:46:23 -05:00
|
|
|
// GetTargetTXTChunked255 returns the TXT target as a list of strings, 255 octets each with the remainder on the last string.
|
|
|
|
func (rc *RecordConfig) GetTargetTXTChunked255() []string {
|
|
|
|
return txtutil.ToChunks(rc.target)
|
|
|
|
}
|