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