2016-08-22 18:31:50 -06:00
|
|
|
package namedotcom
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-11 05:23:59 -07:00
|
|
|
"regexp"
|
2017-04-19 15:20:26 -06:00
|
|
|
"strconv"
|
2017-07-19 15:53:40 -04:00
|
|
|
"strings"
|
2016-08-22 18:31:50 -06:00
|
|
|
|
2018-01-10 14:49:20 -07:00
|
|
|
"github.com/namedotcom/go/namecom"
|
|
|
|
|
2016-08-22 18:31:50 -06:00
|
|
|
"github.com/miekg/dns/dnsutil"
|
|
|
|
|
|
|
|
"github.com/StackExchange/dnscontrol/models"
|
|
|
|
"github.com/StackExchange/dnscontrol/providers/diff"
|
|
|
|
)
|
|
|
|
|
|
|
|
var defaultNameservers = []*models.Nameserver{
|
|
|
|
{Name: "ns1.name.com"},
|
|
|
|
{Name: "ns2.name.com"},
|
|
|
|
{Name: "ns3.name.com"},
|
|
|
|
{Name: "ns4.name.com"},
|
|
|
|
}
|
|
|
|
|
2018-01-10 14:49:20 -07:00
|
|
|
func (n *NameCom) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
|
2017-08-05 09:08:22 -06:00
|
|
|
dc.Punycode()
|
2016-08-22 18:31:50 -06:00
|
|
|
records, err := n.getRecords(dc.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-11 12:38:07 -07:00
|
|
|
actual := make([]*models.RecordConfig, len(records))
|
|
|
|
for i, r := range records {
|
2018-01-10 14:49:20 -07:00
|
|
|
actual[i] = toRecord(r)
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
|
|
|
|
2017-07-19 11:23:17 -06:00
|
|
|
for _, rec := range dc.Records {
|
|
|
|
if rec.Type == "ALIAS" {
|
|
|
|
rec.Type = "ANAME"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 12:38:07 -07:00
|
|
|
checkNSModifications(dc)
|
2016-08-22 18:31:50 -06:00
|
|
|
|
2017-11-07 14:12:17 -08:00
|
|
|
// Normalize
|
2018-01-04 19:19:35 -05:00
|
|
|
models.PostProcessRecords(actual)
|
2017-11-07 14:12:17 -08:00
|
|
|
|
2017-01-11 12:38:07 -07:00
|
|
|
differ := diff.New(dc)
|
|
|
|
_, create, del, mod := differ.IncrementalDiff(actual)
|
2016-08-22 18:31:50 -06:00
|
|
|
corrections := []*models.Correction{}
|
|
|
|
|
|
|
|
for _, d := range del {
|
2018-01-10 14:49:20 -07:00
|
|
|
rec := d.Existing.Original.(*namecom.Record)
|
|
|
|
c := &models.Correction{Msg: d.String(), F: func() error { return n.deleteRecord(rec.ID, dc.Name) }}
|
2016-08-22 18:31:50 -06:00
|
|
|
corrections = append(corrections, c)
|
|
|
|
}
|
|
|
|
for _, cre := range create {
|
2017-01-13 12:30:04 -07:00
|
|
|
rec := cre.Desired
|
2016-08-22 18:31:50 -06:00
|
|
|
c := &models.Correction{Msg: cre.String(), F: func() error { return n.createRecord(rec, dc.Name) }}
|
|
|
|
corrections = append(corrections, c)
|
|
|
|
}
|
|
|
|
for _, chng := range mod {
|
2018-01-10 14:49:20 -07:00
|
|
|
old := chng.Existing.Original.(*namecom.Record)
|
2017-01-11 12:38:07 -07:00
|
|
|
new := chng.Desired
|
2016-08-22 18:31:50 -06:00
|
|
|
c := &models.Correction{Msg: chng.String(), F: func() error {
|
2018-01-10 14:49:20 -07:00
|
|
|
err := n.deleteRecord(old.ID, dc.Name)
|
2016-08-22 18:31:50 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return n.createRecord(new, dc.Name)
|
|
|
|
}}
|
|
|
|
corrections = append(corrections, c)
|
|
|
|
}
|
|
|
|
return corrections, nil
|
|
|
|
}
|
|
|
|
|
2017-01-11 12:38:07 -07:00
|
|
|
func checkNSModifications(dc *models.DomainConfig) {
|
|
|
|
newList := make([]*models.RecordConfig, 0, len(dc.Records))
|
|
|
|
for _, rec := range dc.Records {
|
|
|
|
if rec.Type == "NS" && rec.NameFQDN == dc.Name {
|
2017-04-19 15:20:26 -06:00
|
|
|
continue // Apex NS records are automatically created for the domain's nameservers and cannot be managed otherwise via the name.com API.
|
2017-01-11 12:38:07 -07:00
|
|
|
}
|
|
|
|
newList = append(newList, rec)
|
|
|
|
}
|
|
|
|
dc.Records = newList
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
2017-01-11 12:38:07 -07:00
|
|
|
|
2018-01-11 05:23:59 -07:00
|
|
|
// finds a string surrounded by quotes that might contain an escaped quote charactor.
|
|
|
|
var quotedStringRegexp = regexp.MustCompile("\"((?:[^\"\\\\]|\\\\.)*)\"")
|
|
|
|
|
2018-01-10 14:49:20 -07:00
|
|
|
func toRecord(r *namecom.Record) *models.RecordConfig {
|
2017-07-19 15:53:40 -04:00
|
|
|
rc := &models.RecordConfig{
|
2018-01-24 16:41:10 -07:00
|
|
|
NameFQDN: strings.TrimSuffix(r.Fqdn, "."),
|
2017-01-11 12:38:07 -07:00
|
|
|
Type: r.Type,
|
2018-01-10 14:49:20 -07:00
|
|
|
Target: r.Answer,
|
|
|
|
TTL: r.TTL,
|
2017-01-11 12:38:07 -07:00
|
|
|
Original: r,
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
2017-08-04 12:26:29 -07:00
|
|
|
switch r.Type { // #rtype_variations
|
2018-01-11 05:23:59 -07:00
|
|
|
case "A", "AAAA", "ANAME", "CNAME", "NS":
|
2017-07-19 15:53:40 -04:00
|
|
|
// nothing additional.
|
2018-01-11 05:23:59 -07:00
|
|
|
case "TXT":
|
|
|
|
if r.Answer[0] == '"' && r.Answer[len(r.Answer)-1] == '"' {
|
|
|
|
txtStrings := []string{}
|
|
|
|
for _, t := range quotedStringRegexp.FindAllStringSubmatch(r.Answer, -1) {
|
|
|
|
txtStrings = append(txtStrings, t[1])
|
|
|
|
}
|
|
|
|
rc.SetTxts(txtStrings)
|
|
|
|
}
|
2017-07-19 15:53:40 -04:00
|
|
|
case "MX":
|
2018-01-10 14:49:20 -07:00
|
|
|
rc.MxPreference = uint16(r.Priority)
|
2017-07-19 15:53:40 -04:00
|
|
|
case "SRV":
|
2018-01-10 14:49:20 -07:00
|
|
|
parts := strings.Split(r.Answer, " ")
|
2017-07-19 15:53:40 -04:00
|
|
|
weight, _ := strconv.ParseInt(parts[0], 10, 32)
|
|
|
|
port, _ := strconv.ParseInt(parts[1], 10, 32)
|
|
|
|
rc.SrvWeight = uint16(weight)
|
|
|
|
rc.SrvPort = uint16(port)
|
2018-01-10 14:49:20 -07:00
|
|
|
rc.SrvPriority = uint16(r.Priority)
|
2017-07-19 15:53:40 -04:00
|
|
|
rc.MxPreference = 0
|
|
|
|
rc.Target = parts[2] + "."
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("toRecord unimplemented rtype %v", r.Type))
|
2017-08-04 12:26:29 -07:00
|
|
|
// We panic so that we quickly find any switch statements
|
|
|
|
// that have not been updated for a new RR type.
|
2017-07-19 15:53:40 -04:00
|
|
|
}
|
|
|
|
return rc
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
|
|
|
|
2018-01-10 14:49:20 -07:00
|
|
|
func (n *NameCom) getRecords(domain string) ([]*namecom.Record, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
records []*namecom.Record
|
|
|
|
response *namecom.ListRecordsResponse
|
|
|
|
)
|
2016-08-22 18:31:50 -06:00
|
|
|
|
2018-01-10 14:49:20 -07:00
|
|
|
request := &namecom.ListRecordsRequest{
|
|
|
|
DomainName: domain,
|
|
|
|
Page: 1,
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
2018-01-10 14:49:20 -07:00
|
|
|
|
|
|
|
for request.Page > 0 {
|
|
|
|
response, err = n.client.ListRecords(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records = append(records, response.Records...)
|
|
|
|
request.Page = response.NextPage
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
|
|
|
|
2018-01-10 14:49:20 -07:00
|
|
|
for _, rc := range records {
|
2017-07-20 14:39:40 -06:00
|
|
|
if rc.Type == "CNAME" || rc.Type == "ANAME" || rc.Type == "MX" || rc.Type == "NS" {
|
2018-01-10 14:49:20 -07:00
|
|
|
rc.Answer = rc.Answer + "."
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
|
|
|
}
|
2018-01-10 14:49:20 -07:00
|
|
|
return records, nil
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
|
|
|
|
2018-01-10 14:49:20 -07:00
|
|
|
func (n *NameCom) createRecord(rc *models.RecordConfig, domain string) error {
|
|
|
|
record := &namecom.Record{
|
|
|
|
DomainName: domain,
|
|
|
|
Host: dnsutil.TrimDomainName(rc.NameFQDN, domain),
|
|
|
|
Type: rc.Type,
|
|
|
|
Answer: rc.Target,
|
|
|
|
TTL: rc.TTL,
|
|
|
|
Priority: uint32(rc.MxPreference),
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
2018-01-10 14:49:20 -07:00
|
|
|
|
2017-08-04 12:26:29 -07:00
|
|
|
switch rc.Type { // #rtype_variations
|
2018-01-11 05:23:59 -07:00
|
|
|
case "A", "AAAA", "ANAME", "CNAME", "MX", "NS":
|
2017-07-19 15:53:40 -04:00
|
|
|
// nothing
|
2018-01-11 05:23:59 -07:00
|
|
|
case "TXT":
|
|
|
|
if len(rc.TxtStrings) > 1 {
|
|
|
|
record.Answer = ""
|
|
|
|
for _, t := range rc.TxtStrings {
|
|
|
|
record.Answer += "\"" + strings.Replace(t, "\"", "\\\"", -1) + "\""
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 15:53:40 -04:00
|
|
|
case "SRV":
|
2018-01-10 14:49:20 -07:00
|
|
|
record.Answer = fmt.Sprintf("%d %d %v", rc.SrvWeight, rc.SrvPort, rc.Target)
|
|
|
|
record.Priority = uint32(rc.SrvPriority)
|
2017-07-19 15:53:40 -04:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("createRecord rtype %v unimplemented", rc.Type))
|
2017-08-04 12:26:29 -07:00
|
|
|
// We panic so that we quickly find any switch statements
|
|
|
|
// that have not been updated for a new RR type.
|
2017-07-19 15:53:40 -04:00
|
|
|
}
|
2018-01-10 14:49:20 -07:00
|
|
|
_, err := n.client.CreateRecord(record)
|
|
|
|
return err
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
|
|
|
|
2018-01-10 14:49:20 -07:00
|
|
|
func (n *NameCom) deleteRecord(id int32, domain string) error {
|
|
|
|
request := &namecom.DeleteRecordRequest{
|
|
|
|
DomainName: domain,
|
|
|
|
ID: id,
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
2018-01-10 14:49:20 -07:00
|
|
|
|
|
|
|
_, err := n.client.DeleteRecord(request)
|
|
|
|
return err
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|