2019-01-07 17:30:06 +00:00
|
|
|
// Package rfc2136 implements a DNS provider for solving the DNS-01 challenge using the rfc2136 dynamic update.
|
2017-02-07 21:33:23 +00:00
|
|
|
package rfc2136
|
|
|
|
|
|
|
|
import (
|
2018-09-17 13:16:03 +00:00
|
|
|
"errors"
|
2017-02-07 21:33:23 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2019-01-07 17:30:06 +00:00
|
|
|
"github.com/xenolf/lego/challenge/dns01"
|
2018-09-17 13:16:03 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2017-02-07 21:33:23 +00:00
|
|
|
)
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
Nameserver string
|
|
|
|
TSIGAlgorithm string
|
|
|
|
TSIGKey string
|
|
|
|
TSIGSecret string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
TSIGAlgorithm: env.GetOrDefaultString("RFC2136_TSIG_ALGORITHM", dns.HmacMD5),
|
2019-01-07 17:30:06 +00:00
|
|
|
TTL: env.GetOrDefaultInt("RFC2136_TTL", dns01.DefaultTTL),
|
2018-09-17 13:16:03 +00:00
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("RFC2136_PROPAGATION_TIMEOUT",
|
|
|
|
env.GetOrDefaultSecond("RFC2136_TIMEOUT", 60*time.Second)),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("RFC2136_POLLING_INTERVAL", 2*time.Second),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:30:04 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface that
|
2017-02-07 21:33:23 +00:00
|
|
|
// uses dynamic DNS updates (RFC 2136) to create TXT records on a nameserver.
|
|
|
|
type DNSProvider struct {
|
2018-09-17 13:16:03 +00:00
|
|
|
config *Config
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for rfc2136
|
2017-10-31 09:42:03 +00:00
|
|
|
// dynamic update. Configured with environment variables:
|
|
|
|
// 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-04-06 15:04:03 +00:00
|
|
|
// See https://github.com/miekg/dns/blob/master/tsig.go for supported values.
|
2017-10-31 09:42:03 +00:00
|
|
|
// RFC2136_TSIG_KEY: Name of the secret key as defined in DNS server configuration.
|
|
|
|
// RFC2136_TSIG_SECRET: Secret key payload.
|
|
|
|
// RFC2136_TIMEOUT: DNS propagation timeout in time.ParseDuration format. (60s)
|
|
|
|
// To disable TSIG authentication, leave the RFC2136_TSIG* variables unset.
|
2017-02-07 21:33:23 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-09-17 13:16:03 +00:00
|
|
|
values, err := env.Get("RFC2136_NAMESERVER")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("rfc2136: %v", err)
|
|
|
|
}
|
2018-07-03 10:44:04 +00:00
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Nameserver = values["RFC2136_NAMESERVER"]
|
2018-10-10 14:28:04 +00:00
|
|
|
config.TSIGKey = env.GetOrFile("RFC2136_TSIG_KEY")
|
|
|
|
config.TSIGSecret = env.GetOrFile("RFC2136_TSIG_SECRET")
|
2018-09-17 13:16:03 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +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")
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
2018-07-03 10:44:04 +00:00
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
if config.Nameserver == "" {
|
|
|
|
return nil, fmt.Errorf("rfc2136: nameserver missing")
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
if config.TSIGAlgorithm == "" {
|
|
|
|
config.TSIGAlgorithm = dns.HmacMD5
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-10-31 09:42:03 +00:00
|
|
|
} else {
|
2018-09-17 13:16:03 +00:00
|
|
|
return nil, fmt.Errorf("rfc2136: %v", err)
|
2017-10-31 09:42:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
if len(config.TSIGKey) == 0 && len(config.TSIGSecret) > 0 ||
|
|
|
|
len(config.TSIGKey) > 0 && len(config.TSIGSecret) == 0 {
|
|
|
|
config.TSIGKey = ""
|
|
|
|
config.TSIGSecret = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{config: config}, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +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-10-31 09:42:03 +00:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-09-17 13:16:03 +00:00
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2017-10-31 09:42:03 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 21:33:23 +00:00
|
|
|
// Present creates a TXT record using the specified parameters
|
2018-05-31 07:30:04 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2019-01-07 17:30:06 +00:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-09-17 13:16:03 +00:00
|
|
|
|
|
|
|
err := d.changeRecord("INSERT", fqdn, value, d.config.TTL)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("rfc2136: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
2018-05-31 07:30:04 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2019-01-07 17:30:06 +00:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-09-17 13:16:03 +00:00
|
|
|
|
|
|
|
err := d.changeRecord("REMOVE", fqdn, value, d.config.TTL)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("rfc2136: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:30:04 +00:00
|
|
|
func (d *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
|
2017-02-07 21:33:23 +00:00
|
|
|
// Find the zone for the given fqdn
|
2019-01-07 17:30:06 +00:00
|
|
|
zone, err := dns01.FindZoneByFqdnCustom(fqdn, []string{d.config.Nameserver})
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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}
|
|
|
|
rrs := []dns.RR{rr}
|
|
|
|
|
|
|
|
// Create dynamic update packet
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetUpdate(zone)
|
|
|
|
switch action {
|
|
|
|
case "INSERT":
|
|
|
|
// Always remove old challenge left over from who knows what.
|
|
|
|
m.RemoveRRset(rrs)
|
|
|
|
m.Insert(rrs)
|
|
|
|
case "REMOVE":
|
|
|
|
m.Remove(rrs)
|
|
|
|
default:
|
2018-05-31 07:30:04 +00:00
|
|
|
return fmt.Errorf("unexpected action: %s", action)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup client
|
|
|
|
c := new(dns.Client)
|
|
|
|
c.SingleInflight = true
|
2018-09-17 13:16:03 +00:00
|
|
|
|
2017-02-07 21:33:23 +00:00
|
|
|
// TSIG authentication / msg signing
|
2018-09-17 13:16:03 +00:00
|
|
|
if len(d.config.TSIGKey) > 0 && len(d.config.TSIGSecret) > 0 {
|
|
|
|
m.SetTsig(dns.Fqdn(d.config.TSIGKey), d.config.TSIGAlgorithm, 300, time.Now().Unix())
|
|
|
|
c.TsigSecret = map[string]string{dns.Fqdn(d.config.TSIGKey): d.config.TSIGSecret}
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the query
|
2018-09-17 13:16:03 +00:00
|
|
|
reply, _, err := c.Exchange(m, d.config.Nameserver)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("DNS update failed: %v", err)
|
|
|
|
}
|
|
|
|
if reply != nil && reply.Rcode != dns.RcodeSuccess {
|
|
|
|
return fmt.Errorf("DNS update failed. Server replied: %s", dns.RcodeToString[reply.Rcode])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|