2018-11-06 10:49:50 +00:00
|
|
|
// Package inwx implements a DNS provider for solving the DNS-01 challenge using inwx dom robot
|
2019-03-11 16:56:48 +00:00
|
|
|
package inwx
|
2018-11-06 10:49:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/log"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2019-02-04 21:43:10 +00:00
|
|
|
"github.com/nrdcg/goinwx"
|
2020-05-31 13:18:43 +00:00
|
|
|
"github.com/pquerna/otp/totp"
|
2018-11-06 10:49:50 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "INWX_"
|
|
|
|
|
2020-05-31 13:18:43 +00:00
|
|
|
EnvUsername = envNamespace + "USERNAME"
|
|
|
|
EnvPassword = envNamespace + "PASSWORD"
|
|
|
|
EnvSharedSecret = envNamespace + "SHARED_SECRET"
|
|
|
|
EnvSandbox = envNamespace + "SANDBOX"
|
2020-03-11 22:51:10 +00:00
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-11-06 10:49:50 +00:00
|
|
|
type Config struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
2020-05-31 13:18:43 +00:00
|
|
|
SharedSecret string
|
2018-11-06 10:49:50 +00:00
|
|
|
Sandbox bool
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-11-06 10:49:50 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-12-14 21:57:19 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 300),
|
|
|
|
// INWX has rather unstable propagation delays, thus using a larger default value
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 360*time.Second),
|
2020-03-11 22:51:10 +00:00
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
|
|
|
Sandbox: env.GetOrDefaultBool(EnvSandbox, false),
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2018-11-06 10:49:50 +00:00
|
|
|
type DNSProvider struct {
|
2024-01-18 20:51:57 +00:00
|
|
|
config *Config
|
|
|
|
client *goinwx.Client
|
|
|
|
previousUnlock time.Time
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Dyn DNS.
|
|
|
|
// Credentials must be passed in the environment variables:
|
2020-05-31 13:18:43 +00:00
|
|
|
// INWX_USERNAME, INWX_PASSWORD, and INWX_SHARED_SECRET.
|
2018-11-06 10:49:50 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvUsername, EnvPassword)
|
2018-11-06 10:49:50 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("inwx: %w", err)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.Username = values[EnvUsername]
|
|
|
|
config.Password = values[EnvPassword]
|
2020-05-31 13:18:43 +00:00
|
|
|
config.SharedSecret = env.GetOrFile(EnvSharedSecret)
|
2018-11-06 10:49:50 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Dyn DNS.
|
2018-11-06 10:49:50 +00:00
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("inwx: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Username == "" || config.Password == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("inwx: credentials missing")
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Sandbox {
|
|
|
|
log.Infof("inwx: sandbox mode is enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := goinwx.NewClient(config.Username, config.Password, &goinwx.ClientOptions{Sandbox: config.Sandbox})
|
|
|
|
|
|
|
|
return &DNSProvider{config: config, client: client}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Present creates a TXT record using the specified parameters.
|
2018-11-06 10:49:50 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
challengeInfo := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-11-06 10:49:50 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(challengeInfo.EffectiveFQDN)
|
2018-11-06 10:49:50 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("inwx: could not find zone for domain %q (%s): %w", domain, challengeInfo.EffectiveFQDN, err)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 13:18:43 +00:00
|
|
|
info, err := d.client.Account.Login()
|
2018-11-06 10:49:50 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("inwx: %w", err)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
errL := d.client.Account.Logout()
|
|
|
|
if errL != nil {
|
2023-07-29 10:59:24 +00:00
|
|
|
log.Infof("inwx: failed to log out: %v", errL)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-31 13:18:43 +00:00
|
|
|
err = d.twoFactorAuth(info)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("inwx: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:48:18 +00:00
|
|
|
request := &goinwx.NameserverRecordRequest{
|
2018-12-06 21:50:17 +00:00
|
|
|
Domain: dns01.UnFqdn(authZone),
|
2023-03-07 08:39:05 +00:00
|
|
|
Name: dns01.UnFqdn(challengeInfo.EffectiveFQDN),
|
2018-11-06 10:49:50 +00:00
|
|
|
Type: "TXT",
|
2023-03-07 08:39:05 +00:00
|
|
|
Content: challengeInfo.Value,
|
2019-02-04 21:43:10 +00:00
|
|
|
TTL: d.config.TTL,
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.client.Nameservers.CreateRecord(request)
|
|
|
|
if err != nil {
|
2020-10-27 11:01:05 +00:00
|
|
|
var er *goinwx.ErrorResponse
|
|
|
|
if errors.As(err, &er) {
|
2018-12-06 21:50:17 +00:00
|
|
|
if er.Message == "Object exists" {
|
2018-11-06 10:49:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("inwx: %w", err)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
2020-10-27 11:01:05 +00:00
|
|
|
|
|
|
|
return fmt.Errorf("inwx: %w", err)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-11-06 10:49:50 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
challengeInfo := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-11-06 10:49:50 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(challengeInfo.EffectiveFQDN)
|
2018-11-06 10:49:50 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("inwx: could not find zone for domain %q (%s): %w", domain, challengeInfo.EffectiveFQDN, err)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 13:18:43 +00:00
|
|
|
info, err := d.client.Account.Login()
|
2018-11-06 10:49:50 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("inwx: %w", err)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
errL := d.client.Account.Logout()
|
|
|
|
if errL != nil {
|
2023-07-29 10:59:24 +00:00
|
|
|
log.Infof("inwx: failed to log out: %v", errL)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-31 13:18:43 +00:00
|
|
|
err = d.twoFactorAuth(info)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("inwx: %w", err)
|
|
|
|
}
|
|
|
|
|
2018-11-06 10:49:50 +00:00
|
|
|
response, err := d.client.Nameservers.Info(&goinwx.NameserverInfoRequest{
|
2018-12-06 21:50:17 +00:00
|
|
|
Domain: dns01.UnFqdn(authZone),
|
2023-03-07 08:39:05 +00:00
|
|
|
Name: dns01.UnFqdn(challengeInfo.EffectiveFQDN),
|
2018-11-06 10:49:50 +00:00
|
|
|
Type: "TXT",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("inwx: %w", err)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var lastErr error
|
|
|
|
for _, record := range response.Records {
|
2019-02-04 21:43:10 +00:00
|
|
|
err = d.client.Nameservers.DeleteRecord(record.ID)
|
2018-11-06 10:49:50 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
lastErr = fmt.Errorf("inwx: %w", err)
|
2018-11-06 10:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
|
|
|
}
|
2020-05-31 13:18:43 +00:00
|
|
|
|
|
|
|
func (d *DNSProvider) twoFactorAuth(info *goinwx.LoginResponse) error {
|
|
|
|
if info.TFA != "GOOGLE-AUTH" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.config.SharedSecret == "" {
|
2023-07-29 10:59:24 +00:00
|
|
|
return errors.New("two-factor authentication but no shared secret is given")
|
2020-05-31 13:18:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 20:51:57 +00:00
|
|
|
// INWX forbids re-authentication with a previously used TAN.
|
|
|
|
// To avoid using the same TAN twice, we wait until the next TOTP period.
|
|
|
|
sleep := d.computeSleep(time.Now())
|
|
|
|
if sleep != 0 {
|
|
|
|
log.Infof("inwx: waiting %s for next TOTP token", sleep)
|
|
|
|
time.Sleep(sleep)
|
|
|
|
}
|
|
|
|
|
2024-01-19 12:55:57 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
tan, err := totp.GenerateCode(d.config.SharedSecret, now)
|
2020-05-31 13:18:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-19 12:55:57 +00:00
|
|
|
d.previousUnlock = now.Truncate(30 * time.Second)
|
2024-01-18 20:51:57 +00:00
|
|
|
|
2020-05-31 13:18:43 +00:00
|
|
|
return d.client.Account.Unlock(tan)
|
|
|
|
}
|
2024-01-18 20:51:57 +00:00
|
|
|
|
|
|
|
func (d *DNSProvider) computeSleep(now time.Time) time.Duration {
|
|
|
|
if d.previousUnlock.IsZero() {
|
2024-01-19 12:55:57 +00:00
|
|
|
return 0
|
2024-01-18 20:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
endPeriod := d.previousUnlock.Add(30 * time.Second)
|
|
|
|
if endPeriod.After(now) {
|
|
|
|
return endPeriod.Sub(now)
|
|
|
|
}
|
|
|
|
|
2024-01-19 12:55:57 +00:00
|
|
|
return 0
|
2024-01-18 20:51:57 +00:00
|
|
|
}
|