2018-12-06 21:50:17 +00:00
|
|
|
// Package rfc2136 implements a DNS provider for solving the DNS-01 challenge using the rfc2136 dynamic update.
|
2016-02-29 02:48:41 +00:00
|
|
|
package rfc2136
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
2015-12-03 20:01:46 +00:00
|
|
|
"fmt"
|
2016-02-28 20:09:05 +00:00
|
|
|
"net"
|
2016-02-06 23:12:48 +00:00
|
|
|
"strings"
|
2015-12-03 20:01:46 +00:00
|
|
|
"time"
|
2016-02-06 23:09:43 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2016-02-06 23:09:43 +00:00
|
|
|
"github.com/miekg/dns"
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "RFC2136_"
|
|
|
|
|
|
|
|
EnvTSIGKey = envNamespace + "TSIG_KEY"
|
|
|
|
EnvTSIGSecret = envNamespace + "TSIG_SECRET"
|
|
|
|
EnvTSIGAlgorithm = envNamespace + "TSIG_ALGORITHM"
|
|
|
|
EnvNameserver = envNamespace + "NAMESERVER"
|
|
|
|
EnvDNSTimeout = envNamespace + "DNS_TIMEOUT"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
type Config struct {
|
|
|
|
Nameserver string
|
|
|
|
TSIGAlgorithm string
|
|
|
|
TSIGKey string
|
|
|
|
TSIGSecret string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
2019-01-09 07:23:33 +00:00
|
|
|
SequenceInterval time.Duration
|
|
|
|
DNSTimeout time.Duration
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2021-03-10 09:42:00 +00:00
|
|
|
TSIGAlgorithm: env.GetOrDefaultString(EnvTSIGAlgorithm, dns.HmacSHA1),
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, env.GetOrDefaultSecond("RFC2136_TIMEOUT", 60*time.Second)),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
|
|
|
|
SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout),
|
|
|
|
DNSTimeout: env.GetOrDefaultSecond(EnvDNSTimeout, 10*time.Second),
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2016-03-11 02:46:09 +00:00
|
|
|
type DNSProvider struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for rfc2136
|
2017-07-17 20:05:47 +00:00
|
|
|
// dynamic update. Configured with environment variables:
|
2017-04-26 02:01:22 +00:00
|
|
|
// RFC2136_NAMESERVER: Network address in the form "host" or "host:port".
|
|
|
|
// RFC2136_TSIG_ALGORITHM: Defaults to hmac-md5.sig-alg.reg.int. (HMAC-MD5).
|
2018-05-30 17:53:04 +00:00
|
|
|
// See https://github.com/miekg/dns/blob/master/tsig.go for supported values.
|
2017-04-26 02:01:22 +00:00
|
|
|
// RFC2136_TSIG_KEY: Name of the secret key as defined in DNS server configuration.
|
|
|
|
// RFC2136_TSIG_SECRET: Secret key payload.
|
2019-03-08 18:47:06 +00:00
|
|
|
// RFC2136_PROPAGATION_TIMEOUT: DNS propagation timeout in time.ParseDuration format. (60s)
|
2017-04-26 02:01:22 +00:00
|
|
|
// To disable TSIG authentication, leave the RFC2136_TSIG* variables unset.
|
2016-03-17 20:59:15 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvNameserver)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("rfc2136: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.Nameserver = values[EnvNameserver]
|
|
|
|
config.TSIGKey = env.GetOrFile(EnvTSIGKey)
|
|
|
|
config.TSIGSecret = env.GetOrFile(EnvTSIGSecret)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-03-17 20:59:15 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for rfc2136.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("rfc2136: the configuration of the DNS provider is nil")
|
2016-02-06 23:09:43 +00:00
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.Nameserver == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("rfc2136: nameserver missing")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.TSIGAlgorithm == "" {
|
2021-03-10 09:42:00 +00:00
|
|
|
config.TSIGAlgorithm = dns.HmacSHA1
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append the default DNS port if none is specified.
|
|
|
|
if _, _, err := net.SplitHostPort(config.Nameserver); err != nil {
|
|
|
|
if strings.Contains(err.Error(), "missing port") {
|
|
|
|
config.Nameserver = net.JoinHostPort(config.Nameserver, "53")
|
2017-07-17 20:05:47 +00:00
|
|
|
} else {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("rfc2136: %w", err)
|
2017-07-17 20:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if config.TSIGKey == "" || config.TSIGSecret == "" {
|
2018-09-15 17:07:24 +00:00
|
|
|
config.TSIGKey = ""
|
|
|
|
config.TSIGSecret = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{config: config}, nil
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
2017-07-17 20:05:47 +00:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-09-15 17:07:24 +00:00
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2017-07-17 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-09 07:23:33 +00:00
|
|
|
// Sequential All DNS challenges for this provider will be resolved sequentially.
|
|
|
|
// Returns the interval between each iteration.
|
|
|
|
func (d *DNSProvider) Sequential() time.Duration {
|
|
|
|
return d.config.SequenceInterval
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Present creates a TXT record using the specified parameters.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
err := d.changeRecord("INSERT", fqdn, value, d.config.TTL)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("rfc2136: failed to insert: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-05-30 17:53:04 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
err := d.changeRecord("REMOVE", fqdn, value, d.config.TTL)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("rfc2136: failed to remove: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
func (d *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
|
2016-02-28 20:09:05 +00:00
|
|
|
// Find the zone for the given fqdn
|
2018-12-06 21:50:17 +00:00
|
|
|
zone, err := dns01.FindZoneByFqdnCustom(fqdn, []string{d.config.Nameserver})
|
2016-02-27 22:50:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
// Create RR
|
|
|
|
rr := new(dns.TXT)
|
|
|
|
rr.Hdr = dns.RR_Header{Name: fqdn, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: uint32(ttl)}
|
|
|
|
rr.Txt = []string{value}
|
2016-02-28 20:09:05 +00:00
|
|
|
rrs := []dns.RR{rr}
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
// Create dynamic update packet
|
|
|
|
m := new(dns.Msg)
|
2016-02-27 22:50:42 +00:00
|
|
|
m.SetUpdate(zone)
|
2015-12-03 20:01:46 +00:00
|
|
|
switch action {
|
|
|
|
case "INSERT":
|
2016-02-27 22:50:42 +00:00
|
|
|
// Always remove old challenge left over from who knows what.
|
|
|
|
m.RemoveRRset(rrs)
|
2015-12-03 20:01:46 +00:00
|
|
|
m.Insert(rrs)
|
|
|
|
case "REMOVE":
|
|
|
|
m.Remove(rrs)
|
|
|
|
default:
|
2018-05-30 17:53:04 +00:00
|
|
|
return fmt.Errorf("unexpected action: %s", action)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup client
|
2019-01-09 07:23:33 +00:00
|
|
|
c := &dns.Client{Timeout: d.config.DNSTimeout}
|
2015-12-03 20:01:46 +00:00
|
|
|
c.SingleInflight = true
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
// TSIG authentication / msg signing
|
2018-09-15 17:07:24 +00:00
|
|
|
if len(d.config.TSIGKey) > 0 && len(d.config.TSIGSecret) > 0 {
|
2020-09-27 19:32:10 +00:00
|
|
|
key := dns.Fqdn(d.config.TSIGKey)
|
|
|
|
alg := dns.Fqdn(d.config.TSIGAlgorithm)
|
|
|
|
m.SetTsig(key, alg, 300, time.Now().Unix())
|
2018-09-15 17:07:24 +00:00
|
|
|
c.TsigSecret = map[string]string{dns.Fqdn(d.config.TSIGKey): d.config.TSIGSecret}
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the query
|
2018-09-15 17:07:24 +00:00
|
|
|
reply, _, err := c.Exchange(m, d.config.Nameserver)
|
2015-12-03 20:01:46 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("DNS update failed: %w", err)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
if reply != nil && reply.Rcode != dns.RcodeSuccess {
|
2019-01-09 07:23:33 +00:00
|
|
|
return fmt.Errorf("DNS update failed: server replied: %s", dns.RcodeToString[reply.Rcode])
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|