2019-03-11 16:56:48 +00:00
|
|
|
package dns01
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2024-09-26 19:02:39 +00:00
|
|
|
"time"
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PreCheckFunc checks DNS propagation before notifying ACME that the DNS challenge is ready.
|
|
|
|
type PreCheckFunc func(fqdn, value string) (bool, error)
|
|
|
|
|
2019-02-12 16:36:44 +00:00
|
|
|
// WrapPreCheckFunc wraps a PreCheckFunc in order to do extra operations before or after
|
|
|
|
// the main check, put it in a loop, etc.
|
|
|
|
type WrapPreCheckFunc func(domain, fqdn, value string, check PreCheckFunc) (bool, error)
|
|
|
|
|
|
|
|
// WrapPreCheck Allow to define checks before notifying ACME that the DNS challenge is ready.
|
|
|
|
func WrapPreCheck(wrap WrapPreCheckFunc) ChallengeOption {
|
|
|
|
return func(chlg *Challenge) error {
|
|
|
|
chlg.preCheck.checkFunc = wrap
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-26 19:02:39 +00:00
|
|
|
// DisableCompletePropagationRequirement obsolete.
|
|
|
|
// Deprecated: use DisableAuthoritativeNssPropagationRequirement instead.
|
2018-12-06 21:50:17 +00:00
|
|
|
func DisableCompletePropagationRequirement() ChallengeOption {
|
2024-09-26 19:02:39 +00:00
|
|
|
return DisableAuthoritativeNssPropagationRequirement()
|
|
|
|
}
|
|
|
|
|
|
|
|
func DisableAuthoritativeNssPropagationRequirement() ChallengeOption {
|
|
|
|
return func(chlg *Challenge) error {
|
|
|
|
chlg.preCheck.requireAuthoritativeNssPropagation = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RecursiveNSsPropagationRequirement() ChallengeOption {
|
2018-12-06 21:50:17 +00:00
|
|
|
return func(chlg *Challenge) error {
|
2024-09-26 19:02:39 +00:00
|
|
|
chlg.preCheck.requireRecursiveNssPropagation = true
|
2018-12-06 21:50:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-02 17:31:52 +00:00
|
|
|
func PropagationWait(wait time.Duration, skipCheck bool) ChallengeOption {
|
2024-09-26 19:02:39 +00:00
|
|
|
return WrapPreCheck(func(domain, fqdn, value string, check PreCheckFunc) (bool, error) {
|
|
|
|
time.Sleep(wait)
|
2024-10-02 17:31:52 +00:00
|
|
|
|
|
|
|
if skipCheck {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return check(fqdn, value)
|
2024-09-26 19:02:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
type preCheck struct {
|
|
|
|
// checks DNS propagation before notifying ACME that the DNS challenge is ready.
|
2019-02-12 16:36:44 +00:00
|
|
|
checkFunc WrapPreCheckFunc
|
2024-09-26 19:02:39 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
// require the TXT record to be propagated to all authoritative name servers
|
2024-09-26 19:02:39 +00:00
|
|
|
requireAuthoritativeNssPropagation bool
|
|
|
|
|
|
|
|
// require the TXT record to be propagated to all recursive name servers
|
|
|
|
requireRecursiveNssPropagation bool
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPreCheck() preCheck {
|
|
|
|
return preCheck{
|
2024-09-26 19:02:39 +00:00
|
|
|
requireAuthoritativeNssPropagation: true,
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 16:36:44 +00:00
|
|
|
func (p preCheck) call(domain, fqdn, value string) (bool, error) {
|
2018-12-06 21:50:17 +00:00
|
|
|
if p.checkFunc == nil {
|
|
|
|
return p.checkDNSPropagation(fqdn, value)
|
|
|
|
}
|
2019-02-12 16:36:44 +00:00
|
|
|
|
|
|
|
return p.checkFunc(domain, fqdn, value, p.checkDNSPropagation)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkDNSPropagation checks if the expected TXT record has been propagated to all authoritative nameservers.
|
|
|
|
func (p preCheck) checkDNSPropagation(fqdn, value string) (bool, error) {
|
2024-09-26 19:02:39 +00:00
|
|
|
// Initial attempt to resolve at the recursive NS (require to get CNAME)
|
2018-12-06 21:50:17 +00:00
|
|
|
r, err := dnsQuery(fqdn, dns.TypeTXT, recursiveNameservers, true)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Rcode == dns.RcodeSuccess {
|
2019-02-09 04:02:58 +00:00
|
|
|
fqdn = updateDomainWithCName(r, fqdn)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 19:02:39 +00:00
|
|
|
if p.requireRecursiveNssPropagation {
|
|
|
|
_, err = checkNameserversPropagation(fqdn, value, recursiveNameservers, false)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.requireAuthoritativeNssPropagation {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
authoritativeNss, err := lookupNameservers(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2024-09-26 19:02:39 +00:00
|
|
|
return checkNameserversPropagation(fqdn, value, authoritativeNss, true)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 19:02:39 +00:00
|
|
|
// checkNameserversPropagation queries each of the given nameservers for the expected TXT record.
|
|
|
|
func checkNameserversPropagation(fqdn, value string, nameservers []string, addPort bool) (bool, error) {
|
2018-12-06 21:50:17 +00:00
|
|
|
for _, ns := range nameservers {
|
2024-09-26 19:02:39 +00:00
|
|
|
if addPort {
|
|
|
|
ns = net.JoinHostPort(ns, "53")
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := dnsQuery(fqdn, dns.TypeTXT, []string{ns}, false)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Rcode != dns.RcodeSuccess {
|
|
|
|
return false, fmt.Errorf("NS %s returned %s for %s", ns, dns.RcodeToString[r.Rcode], fqdn)
|
|
|
|
}
|
|
|
|
|
2018-12-21 23:53:05 +00:00
|
|
|
var records []string
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
var found bool
|
|
|
|
for _, rr := range r.Answer {
|
|
|
|
if txt, ok := rr.(*dns.TXT); ok {
|
2018-12-21 23:53:05 +00:00
|
|
|
record := strings.Join(txt.Txt, "")
|
|
|
|
records = append(records, record)
|
|
|
|
if record == value {
|
2018-12-06 21:50:17 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2018-12-21 23:53:05 +00:00
|
|
|
return false, fmt.Errorf("NS %s did not return the expected TXT record [fqdn: %s, value: %s]: %s", ns, fqdn, value, strings.Join(records, " ,"))
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|