2015-06-11 00:12:46 +02:00
|
|
|
package acme
|
2015-11-17 00:57:41 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2016-01-01 15:36:30 +02:00
|
|
|
"encoding/base64"
|
2015-11-17 00:57:41 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-02-03 06:03:03 +02:00
|
|
|
"net"
|
2016-01-01 15:36:30 +02:00
|
|
|
"strings"
|
2016-03-11 05:51:02 +02:00
|
|
|
"time"
|
2016-01-22 02:38:15 +02:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2018-05-30 19:53:04 +02:00
|
|
|
"github.com/xenolf/lego/log"
|
2015-11-17 00:57:41 +02:00
|
|
|
)
|
|
|
|
|
2016-02-10 16:52:53 +02:00
|
|
|
type preCheckDNSFunc func(fqdn, value string) (bool, error)
|
2016-01-22 02:38:15 +02:00
|
|
|
|
2016-02-28 22:09:05 +02:00
|
|
|
var (
|
2016-08-10 07:15:54 +02:00
|
|
|
// PreCheckDNS checks DNS propagation before notifying ACME that
|
|
|
|
// the DNS challenge is ready.
|
|
|
|
PreCheckDNS preCheckDNSFunc = checkDNSPropagation
|
2016-02-29 09:46:15 +02:00
|
|
|
fqdnToZone = map[string]string{}
|
2016-02-28 22:09:05 +02:00
|
|
|
)
|
2016-01-22 02:38:15 +02:00
|
|
|
|
2016-11-03 20:37:15 +02:00
|
|
|
const defaultResolvConf = "/etc/resolv.conf"
|
|
|
|
|
|
|
|
var defaultNameservers = []string{
|
2016-04-12 00:59:59 +02:00
|
|
|
"google-public-dns-a.google.com:53",
|
|
|
|
"google-public-dns-b.google.com:53",
|
|
|
|
}
|
2016-01-22 02:38:15 +02:00
|
|
|
|
2018-05-30 19:53:04 +02:00
|
|
|
// RecursiveNameservers are used to pre-check DNS propagations
|
2016-11-03 20:37:15 +02:00
|
|
|
var RecursiveNameservers = getNameservers(defaultResolvConf, defaultNameservers)
|
|
|
|
|
2016-05-25 05:22:09 +02:00
|
|
|
// DNSTimeout is used to override the default DNS timeout of 10 seconds.
|
|
|
|
var DNSTimeout = 10 * time.Second
|
|
|
|
|
2016-11-03 20:37:15 +02:00
|
|
|
// getNameservers attempts to get systems nameservers before falling back to the defaults
|
|
|
|
func getNameservers(path string, defaults []string) []string {
|
|
|
|
config, err := dns.ClientConfigFromFile(path)
|
|
|
|
if err != nil || len(config.Servers) == 0 {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
|
|
|
|
systemNameservers := []string{}
|
|
|
|
for _, server := range config.Servers {
|
|
|
|
// ensure all servers have a port number
|
|
|
|
if _, _, err := net.SplitHostPort(server); err != nil {
|
|
|
|
systemNameservers = append(systemNameservers, net.JoinHostPort(server, "53"))
|
|
|
|
} else {
|
|
|
|
systemNameservers = append(systemNameservers, server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return systemNameservers
|
|
|
|
}
|
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
// DNS01Record returns a DNS record which will fulfill the `dns-01` challenge
|
|
|
|
func DNS01Record(domain, keyAuth string) (fqdn string, value string, ttl int) {
|
|
|
|
keyAuthShaBytes := sha256.Sum256([]byte(keyAuth))
|
|
|
|
// base64URL encoding without padding
|
2018-05-30 19:53:04 +02:00
|
|
|
value = base64.RawURLEncoding.EncodeToString(keyAuthShaBytes[:sha256.Size])
|
2016-01-15 06:06:25 +02:00
|
|
|
ttl = 120
|
|
|
|
fqdn = fmt.Sprintf("_acme-challenge.%s.", domain)
|
|
|
|
return
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
2015-11-17 00:57:41 +02:00
|
|
|
|
|
|
|
// dnsChallenge implements the dns-01 challenge according to ACME 7.5
|
|
|
|
type dnsChallenge struct {
|
2015-12-03 22:01:46 +02:00
|
|
|
jws *jws
|
2016-01-25 01:23:21 +02:00
|
|
|
validate validateFunc
|
2016-01-15 06:06:25 +02:00
|
|
|
provider ChallengeProvider
|
2015-11-17 00:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *dnsChallenge) Solve(chlng challenge, domain string) error {
|
2018-05-30 19:53:04 +02:00
|
|
|
log.Printf("[INFO][%s] acme: Trying to solve DNS-01", domain)
|
2015-11-17 00:57:41 +02:00
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
if s.provider == nil {
|
|
|
|
return errors.New("No DNS Provider configured")
|
|
|
|
}
|
|
|
|
|
2015-11-17 00:57:41 +02:00
|
|
|
// Generate the Key Authorization for the challenge
|
2016-01-27 03:01:39 +02:00
|
|
|
keyAuth, err := getKeyAuthorization(chlng.Token, s.jws.privKey)
|
2015-11-17 00:57:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
err = s.provider.Present(domain, chlng.Token, keyAuth)
|
|
|
|
if err != nil {
|
2016-04-12 00:59:59 +02:00
|
|
|
return fmt.Errorf("Error presenting token: %s", err)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
2016-01-15 06:06:25 +02:00
|
|
|
defer func() {
|
|
|
|
err := s.provider.CleanUp(domain, chlng.Token, keyAuth)
|
|
|
|
if err != nil {
|
2016-04-12 00:59:59 +02:00
|
|
|
log.Printf("Error cleaning up %s: %v ", domain, err)
|
2016-01-15 06:06:25 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-02-03 06:03:03 +02:00
|
|
|
fqdn, value, _ := DNS01Record(domain, keyAuth)
|
2015-11-17 00:57:41 +02:00
|
|
|
|
2018-05-30 19:53:04 +02:00
|
|
|
log.Printf("[INFO][%s] Checking DNS record propagation using %+v", domain, RecursiveNameservers)
|
2016-02-03 06:03:03 +02:00
|
|
|
|
2016-03-12 16:58:44 +02:00
|
|
|
var timeout, interval time.Duration
|
|
|
|
switch provider := s.provider.(type) {
|
|
|
|
case ChallengeProviderTimeout:
|
|
|
|
timeout, interval = provider.Timeout()
|
|
|
|
default:
|
|
|
|
timeout, interval = 60*time.Second, 2*time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
err = WaitFor(timeout, interval, func() (bool, error) {
|
2016-08-10 07:15:54 +02:00
|
|
|
return PreCheckDNS(fqdn, value)
|
2016-02-10 16:52:53 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2016-02-03 06:03:03 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-01-22 02:38:15 +02:00
|
|
|
|
2018-05-30 19:53:04 +02:00
|
|
|
return s.validate(s.jws, domain, chlng.URL, challenge{Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth})
|
2015-11-17 00:57:41 +02:00
|
|
|
}
|
2016-01-22 03:25:27 +02:00
|
|
|
|
2016-02-14 08:24:19 +02:00
|
|
|
// checkDNSPropagation checks if the expected TXT record has been propagated to all authoritative nameservers.
|
|
|
|
func checkDNSPropagation(fqdn, value string) (bool, error) {
|
2016-02-10 16:52:53 +02:00
|
|
|
// Initial attempt to resolve at the recursive NS
|
2016-04-12 00:59:59 +02:00
|
|
|
r, err := dnsQuery(fqdn, dns.TypeTXT, RecursiveNameservers, true)
|
2016-01-22 03:25:27 +02:00
|
|
|
if err != nil {
|
2016-02-10 16:52:53 +02:00
|
|
|
return false, err
|
|
|
|
}
|
2016-02-19 10:14:26 +02:00
|
|
|
if r.Rcode == dns.RcodeSuccess {
|
|
|
|
// If we see a CNAME here then use the alias
|
|
|
|
for _, rr := range r.Answer {
|
|
|
|
if cn, ok := rr.(*dns.CNAME); ok {
|
|
|
|
if cn.Hdr.Name == fqdn {
|
|
|
|
fqdn = cn.Target
|
|
|
|
break
|
|
|
|
}
|
2016-02-10 16:52:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
authoritativeNss, err := lookupNameservers(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2016-01-22 03:25:27 +02:00
|
|
|
}
|
|
|
|
|
2016-02-10 16:52:53 +02:00
|
|
|
return checkAuthoritativeNss(fqdn, value, authoritativeNss)
|
2016-02-03 06:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkAuthoritativeNss queries each of the given nameservers for the expected TXT record.
|
|
|
|
func checkAuthoritativeNss(fqdn, value string, nameservers []string) (bool, error) {
|
|
|
|
for _, ns := range nameservers {
|
2016-04-12 00:59:59 +02:00
|
|
|
r, err := dnsQuery(fqdn, dns.TypeTXT, []string{net.JoinHostPort(ns, "53")}, false)
|
2016-01-22 03:25:27 +02:00
|
|
|
if err != nil {
|
2016-02-03 06:03:03 +02:00
|
|
|
return false, err
|
2016-01-22 03:25:27 +02:00
|
|
|
}
|
|
|
|
|
2016-02-03 06:03:03 +02:00
|
|
|
if r.Rcode != dns.RcodeSuccess {
|
2016-02-10 16:52:53 +02:00
|
|
|
return false, fmt.Errorf("NS %s returned %s for %s", ns, dns.RcodeToString[r.Rcode], fqdn)
|
2016-01-22 03:25:27 +02:00
|
|
|
}
|
|
|
|
|
2016-02-03 06:03:03 +02:00
|
|
|
var found bool
|
|
|
|
for _, rr := range r.Answer {
|
|
|
|
if txt, ok := rr.(*dns.TXT); ok {
|
|
|
|
if strings.Join(txt.Txt, "") == value {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-01-22 03:25:27 +02:00
|
|
|
}
|
|
|
|
|
2016-02-03 06:03:03 +02:00
|
|
|
if !found {
|
2016-02-10 16:52:53 +02:00
|
|
|
return false, fmt.Errorf("NS %s did not return the expected TXT record", ns)
|
2016-02-03 06:03:03 +02:00
|
|
|
}
|
2016-01-22 03:25:27 +02:00
|
|
|
}
|
|
|
|
|
2016-02-03 06:03:03 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2016-04-12 00:59:59 +02:00
|
|
|
// dnsQuery will query a nameserver, iterating through the supplied servers as it retries
|
2016-02-28 22:09:05 +02:00
|
|
|
// The nameserver should include a port, to facilitate testing where we talk to a mock dns server.
|
2016-04-12 00:59:59 +02:00
|
|
|
func dnsQuery(fqdn string, rtype uint16, nameservers []string, recursive bool) (in *dns.Msg, err error) {
|
2016-02-03 06:03:03 +02:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(fqdn, rtype)
|
|
|
|
m.SetEdns0(4096, false)
|
2016-02-10 16:52:53 +02:00
|
|
|
|
2016-02-03 06:03:03 +02:00
|
|
|
if !recursive {
|
|
|
|
m.RecursionDesired = false
|
|
|
|
}
|
|
|
|
|
2016-04-12 08:30:00 +02:00
|
|
|
// Will retry the request based on the number of servers (n+1)
|
2016-04-12 00:59:59 +02:00
|
|
|
for i := 1; i <= len(nameservers)+1; i++ {
|
|
|
|
ns := nameservers[i%len(nameservers)]
|
2016-05-25 05:22:09 +02:00
|
|
|
udp := &dns.Client{Net: "udp", Timeout: DNSTimeout}
|
|
|
|
in, _, err = udp.Exchange(m, ns)
|
2016-04-12 00:59:59 +02:00
|
|
|
|
|
|
|
if err == dns.ErrTruncated {
|
2016-05-25 05:22:09 +02:00
|
|
|
tcp := &dns.Client{Net: "tcp", Timeout: DNSTimeout}
|
2017-08-10 19:47:37 +02:00
|
|
|
// If the TCP request succeeds, the err will reset to nil
|
2016-04-12 00:59:59 +02:00
|
|
|
in, _, err = tcp.Exchange(m, ns)
|
|
|
|
}
|
2016-02-03 06:03:03 +02:00
|
|
|
|
2016-04-12 00:59:59 +02:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 06:03:03 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-10 16:52:53 +02:00
|
|
|
// lookupNameservers returns the authoritative nameservers for the given fqdn.
|
2016-02-03 06:03:03 +02:00
|
|
|
func lookupNameservers(fqdn string) ([]string, error) {
|
|
|
|
var authoritativeNss []string
|
|
|
|
|
2016-04-12 00:59:59 +02:00
|
|
|
zone, err := FindZoneByFqdn(fqdn, RecursiveNameservers)
|
2016-02-28 22:09:05 +02:00
|
|
|
if err != nil {
|
2016-07-29 19:41:07 +02:00
|
|
|
return nil, fmt.Errorf("Could not determine the zone: %v", err)
|
2016-02-28 22:09:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 00:59:59 +02:00
|
|
|
r, err := dnsQuery(zone, dns.TypeNS, RecursiveNameservers, true)
|
2016-02-03 06:03:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rr := range r.Answer {
|
2016-02-10 16:52:53 +02:00
|
|
|
if ns, ok := rr.(*dns.NS); ok {
|
|
|
|
authoritativeNss = append(authoritativeNss, strings.ToLower(ns.Ns))
|
2016-02-03 06:03:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 16:52:53 +02:00
|
|
|
if len(authoritativeNss) > 0 {
|
|
|
|
return authoritativeNss, nil
|
|
|
|
}
|
2016-02-28 22:09:05 +02:00
|
|
|
return nil, fmt.Errorf("Could not determine authoritative nameservers")
|
|
|
|
}
|
2016-02-10 16:52:53 +02:00
|
|
|
|
2016-07-29 19:41:07 +02:00
|
|
|
// FindZoneByFqdn determines the zone apex for the given fqdn by recursing up the
|
|
|
|
// domain labels until the nameserver returns a SOA record in the answer section.
|
2016-04-12 00:59:59 +02:00
|
|
|
func FindZoneByFqdn(fqdn string, nameservers []string) (string, error) {
|
2016-02-28 22:09:05 +02:00
|
|
|
// Do we have it cached?
|
2016-02-29 09:46:15 +02:00
|
|
|
if zone, ok := fqdnToZone[fqdn]; ok {
|
2016-02-28 22:09:05 +02:00
|
|
|
return zone, nil
|
2016-02-03 06:03:03 +02:00
|
|
|
}
|
2016-02-10 16:52:53 +02:00
|
|
|
|
2016-07-29 19:41:07 +02:00
|
|
|
labelIndexes := dns.Split(fqdn)
|
|
|
|
for _, index := range labelIndexes {
|
|
|
|
domain := fqdn[index:]
|
|
|
|
|
|
|
|
in, err := dnsQuery(domain, dns.TypeSOA, nameservers, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-02-28 22:09:05 +02:00
|
|
|
}
|
2016-07-29 19:41:07 +02:00
|
|
|
|
|
|
|
// Any response code other than NOERROR and NXDOMAIN is treated as error
|
|
|
|
if in.Rcode != dns.RcodeNameError && in.Rcode != dns.RcodeSuccess {
|
|
|
|
return "", fmt.Errorf("Unexpected response code '%s' for %s",
|
|
|
|
dns.RcodeToString[in.Rcode], domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we got a SOA RR in the answer section
|
|
|
|
if in.Rcode == dns.RcodeSuccess {
|
2017-11-15 12:03:00 +02:00
|
|
|
|
|
|
|
// CNAME records cannot/should not exist at the root of a zone.
|
|
|
|
// So we skip a domain when a CNAME is found.
|
|
|
|
if dnsMsgContainsCNAME(in) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-07-29 19:41:07 +02:00
|
|
|
for _, ans := range in.Answer {
|
|
|
|
if soa, ok := ans.(*dns.SOA); ok {
|
|
|
|
zone := soa.Hdr.Name
|
|
|
|
fqdnToZone[fqdn] = zone
|
|
|
|
return zone, nil
|
|
|
|
}
|
|
|
|
}
|
2016-02-28 22:09:05 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-29 19:41:07 +02:00
|
|
|
|
|
|
|
return "", fmt.Errorf("Could not find the start of authority")
|
2016-01-22 03:25:27 +02:00
|
|
|
}
|
2016-01-26 17:37:50 +02:00
|
|
|
|
2017-11-15 12:03:00 +02:00
|
|
|
// dnsMsgContainsCNAME checks for a CNAME answer in msg
|
|
|
|
func dnsMsgContainsCNAME(msg *dns.Msg) bool {
|
|
|
|
for _, ans := range msg.Answer {
|
|
|
|
if _, ok := ans.(*dns.CNAME); ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-03-11 04:46:09 +02:00
|
|
|
// ClearFqdnCache clears the cache of fqdn to zone mappings. Primarily used in testing.
|
|
|
|
func ClearFqdnCache() {
|
2016-02-29 09:46:15 +02:00
|
|
|
fqdnToZone = map[string]string{}
|
|
|
|
}
|
|
|
|
|
2016-03-11 04:20:25 +02:00
|
|
|
// ToFqdn converts the name into a fqdn appending a trailing dot.
|
|
|
|
func ToFqdn(name string) string {
|
|
|
|
n := len(name)
|
|
|
|
if n == 0 || name[n-1] == '.' {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return name + "."
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnFqdn converts the fqdn into a name removing the trailing dot.
|
|
|
|
func UnFqdn(name string) string {
|
|
|
|
n := len(name)
|
|
|
|
if n != 0 && name[n-1] == '.' {
|
|
|
|
return name[:n-1]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|