2016-03-17 20:59:15 +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 (
|
|
|
|
"fmt"
|
2016-02-28 20:09:05 +00:00
|
|
|
"net"
|
2016-03-17 20:59:15 +00:00
|
|
|
"os"
|
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
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2016-02-29 02:48:41 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface that
|
2015-12-03 20:01:46 +00:00
|
|
|
// uses dynamic DNS updates (RFC 2136) to create TXT records on a nameserver.
|
2016-03-11 02:46:09 +00:00
|
|
|
type DNSProvider struct {
|
2016-02-06 23:09:43 +00:00
|
|
|
nameserver string
|
|
|
|
tsigAlgorithm string
|
|
|
|
tsigKey string
|
|
|
|
tsigSecret string
|
2017-07-17 20:05:47 +00:00
|
|
|
timeout time.Duration
|
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-28 16:31:28 +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.
|
2017-07-17 20:05:47 +00:00
|
|
|
// RFC2136_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) {
|
|
|
|
nameserver := os.Getenv("RFC2136_NAMESERVER")
|
|
|
|
tsigAlgorithm := os.Getenv("RFC2136_TSIG_ALGORITHM")
|
|
|
|
tsigKey := os.Getenv("RFC2136_TSIG_KEY")
|
|
|
|
tsigSecret := os.Getenv("RFC2136_TSIG_SECRET")
|
2017-07-17 20:05:47 +00:00
|
|
|
timeout := os.Getenv("RFC2136_TIMEOUT")
|
|
|
|
return NewDNSProviderCredentials(nameserver, tsigAlgorithm, tsigKey, tsigSecret, timeout)
|
2016-03-17 20:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderCredentials uses the supplied credentials to return a
|
|
|
|
// DNSProvider instance configured for rfc2136 dynamic update. To disable TSIG
|
|
|
|
// authentication, leave the TSIG parameters as empty strings.
|
|
|
|
// nameserver must be a network address in the form "host" or "host:port".
|
2017-07-17 20:05:47 +00:00
|
|
|
func NewDNSProviderCredentials(nameserver, tsigAlgorithm, tsigKey, tsigSecret, timeout string) (*DNSProvider, error) {
|
2016-03-17 20:59:15 +00:00
|
|
|
if nameserver == "" {
|
|
|
|
return nil, fmt.Errorf("RFC2136 nameserver missing")
|
|
|
|
}
|
|
|
|
|
2016-02-06 23:12:48 +00:00
|
|
|
// Append the default DNS port if none is specified.
|
2016-02-28 20:09:05 +00:00
|
|
|
if _, _, err := net.SplitHostPort(nameserver); err != nil {
|
|
|
|
if strings.Contains(err.Error(), "missing port") {
|
|
|
|
nameserver = net.JoinHostPort(nameserver, "53")
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-06 23:12:48 +00:00
|
|
|
}
|
2016-03-11 02:46:09 +00:00
|
|
|
d := &DNSProvider{
|
2016-02-28 20:09:05 +00:00
|
|
|
nameserver: nameserver,
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
2016-02-06 23:09:43 +00:00
|
|
|
if tsigAlgorithm == "" {
|
|
|
|
tsigAlgorithm = dns.HmacMD5
|
|
|
|
}
|
|
|
|
d.tsigAlgorithm = tsigAlgorithm
|
2015-12-03 20:01:46 +00:00
|
|
|
if len(tsigKey) > 0 && len(tsigSecret) > 0 {
|
|
|
|
d.tsigKey = tsigKey
|
|
|
|
d.tsigSecret = tsigSecret
|
|
|
|
}
|
|
|
|
|
2017-07-17 20:05:47 +00:00
|
|
|
if timeout == "" {
|
|
|
|
d.timeout = 60 * time.Second
|
|
|
|
} else {
|
|
|
|
t, err := time.ParseDuration(timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if t < 0 {
|
2018-05-28 16:31:28 +00:00
|
|
|
return nil, fmt.Errorf("invalid/negative RFC2136_TIMEOUT: %v", timeout)
|
2017-07-17 20:05:47 +00:00
|
|
|
} else {
|
|
|
|
d.timeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2018-05-28 16:31:28 +00:00
|
|
|
// Timeout Returns the timeout configured with RFC2136_TIMEOUT, or 60s.
|
2017-07-17 20:05:47 +00:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-05-28 16:31:28 +00:00
|
|
|
return d.timeout, 2 * time.Second
|
2017-07-17 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
// Present creates a TXT record using the specified parameters
|
2018-05-28 16:31:28 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2016-02-29 02:48:41 +00:00
|
|
|
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
|
2018-05-28 16:31:28 +00:00
|
|
|
return d.changeRecord("INSERT", fqdn, value, ttl)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
2018-05-28 16:31:28 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-02-29 02:48:41 +00:00
|
|
|
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
|
2018-05-28 16:31:28 +00:00
|
|
|
return d.changeRecord("REMOVE", fqdn, value, ttl)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 16:31:28 +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-05-28 16:31:28 +00:00
|
|
|
zone, err := acme.FindZoneByFqdn(fqdn, []string{d.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-28 16:31:28 +00:00
|
|
|
return fmt.Errorf("unexpected action: %s", action)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup client
|
|
|
|
c := new(dns.Client)
|
|
|
|
c.SingleInflight = true
|
|
|
|
// TSIG authentication / msg signing
|
2018-05-28 16:31:28 +00:00
|
|
|
if len(d.tsigKey) > 0 && len(d.tsigSecret) > 0 {
|
|
|
|
m.SetTsig(dns.Fqdn(d.tsigKey), d.tsigAlgorithm, 300, time.Now().Unix())
|
|
|
|
c.TsigSecret = map[string]string{dns.Fqdn(d.tsigKey): d.tsigSecret}
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the query
|
2018-05-28 16:31:28 +00:00
|
|
|
reply, _, err := c.Exchange(m, d.nameserver)
|
2015-12-03 20:01:46 +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
|
|
|
|
}
|