-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsupdater.go
216 lines (198 loc) · 5.8 KB
/
nsupdater.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"net"
"strings"
"time"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
)
type NSUpdater struct {
config Config
}
func (u *NSUpdater) Init(config Config) *NSUpdater {
u.config = config
return u
}
func Reverse4(ip string) string {
spip := strings.Split(ip, ".")
for i, j := 0, len(spip)-1; i < j; i, j = i+1, j-1 {
spip[i], spip[j] = spip[j], spip[i]
}
return strings.Join(spip, ".") + ".in-addr.arpa."
}
func (u *NSUpdater) ensureReverse(hostname string, ip string, just_clean bool) {
full_ptr := Reverse4(ip)
dnssrvcnf, zonename, err := u.GetServerConfigForHostname(full_ptr)
if err != nil {
log.Warningln(err)
return
}
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion(full_ptr, dns.TypePTR)
m.RecursionDesired = true
result, _, err := c.Exchange(m, net.JoinHostPort(dnssrvcnf.Server, "53"))
if err != nil {
log.Fatal(err)
}
if result.Rcode != dns.RcodeSuccess && result.Rcode != dns.RcodeNameError {
log.Fatalf("query %s failed against server %s with code %s", full_ptr, dnssrvcnf.Server, dns.RcodeToString[result.Rcode])
}
hostname_found := false
var rr_to_cleanup []dns.RR
if result.Rcode == dns.RcodeSuccess {
// Check and/or cleanup time
for _, resrec := range result.Answer {
if resrec.(*dns.PTR).Ptr != hostname || just_clean {
log.Debugf("set to remove %v from %v", resrec.(*dns.PTR).Ptr, full_ptr)
rr_to_cleanup = append(rr_to_cleanup, resrec)
} else {
log.Debugf("%v in %v exists", resrec.(*dns.PTR).Ptr, full_ptr)
hostname_found = true
}
}
}
rev_msg := new(dns.Msg)
rev_msg.SetUpdate(zonename) // zone name
if len(rr_to_cleanup) != 0 {
rev_msg.RemoveName(rr_to_cleanup)
}
if !hostname_found && !just_clean {
log.Debugf("create %v in %v", hostname, full_ptr)
rev_msg.Insert([]dns.RR{&dns.PTR{
Hdr: dns.RR_Header{
Name: full_ptr,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: 60,
},
Ptr: hostname,
}})
}
if len(rev_msg.Ns) > 0 {
rev_msg.SetTsig(dns.Fqdn(dnssrvcnf.KeyName), dns.Fqdn(dnssrvcnf.Algo), 300, time.Now().Unix())
// perform
c := new(dns.Client)
c.TsigSecret = map[string]string{dns.Fqdn(dnssrvcnf.KeyName): dnssrvcnf.Secret}
rsp, _, err := c.Exchange(rev_msg, dnssrvcnf.Server+":53")
if err != nil {
log.Errorln("dns:Client.Exchange ... %v: %v", dnssrvcnf.Server, err)
return
}
if rsp.Rcode == dns.RcodeSuccess {
log.Debugf("Update ok %+v", rev_msg.Ns)
} else {
log.Errorln("error updating rcode=%v for %+v", dns.RcodeToString[rsp.Rcode], rev_msg.Ns)
}
} else {
log.Debugf("%v already ok in %v", hostname, full_ptr)
}
}
func (u *NSUpdater) ensureStraight(hostname string, ip string, just_clean bool) {
dnssrvcnf, zonename, err := u.GetServerConfigForHostname(hostname)
if err != nil {
log.Warningln(err)
return
}
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion(hostname, dns.TypeA)
m.RecursionDesired = true
result, _, err := c.Exchange(m, net.JoinHostPort(dnssrvcnf.Server, "53"))
if err != nil {
log.Fatal(err)
}
if result.Rcode != dns.RcodeSuccess && result.Rcode != dns.RcodeNameError {
log.Fatalf("query %s failed against server %s with code %s", hostname, dnssrvcnf.Server, dns.RcodeToString[result.Rcode])
}
hostname_found := false
var rr_to_cleanup []dns.RR
if result.Rcode == dns.RcodeSuccess {
// Check and/or cleanup time
for _, resrec := range result.Answer {
if resrec.(*dns.A).A.String() != ip || just_clean {
log.Debugf("set to remove %v from %v", resrec.(*dns.A).A.String(), hostname)
rr_to_cleanup = append(rr_to_cleanup, resrec.(*dns.A))
} else {
log.Debugf("%v in %v exists", resrec.(*dns.A).A.String(), hostname)
hostname_found = true
}
}
}
rev_msg := new(dns.Msg)
rev_msg.SetUpdate(zonename) // zone name
if len(rr_to_cleanup) != 0 {
rev_msg.Remove(rr_to_cleanup)
}
if !hostname_found && !just_clean {
log.Debugf("create %v to %v", hostname, ip)
rev_msg.Insert([]dns.RR{&dns.A{
Hdr: dns.RR_Header{
Name: hostname,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 60,
},
A: net.ParseIP(ip),
}})
}
if len(rev_msg.Ns) > 0 {
rev_msg.SetTsig(dns.Fqdn(dnssrvcnf.KeyName), dns.Fqdn(dnssrvcnf.Algo), 300, time.Now().Unix())
// perform
c := new(dns.Client)
c.TsigSecret = map[string]string{dns.Fqdn(dnssrvcnf.KeyName): dnssrvcnf.Secret}
rsp, _, err := c.Exchange(rev_msg, dnssrvcnf.Server+":53")
if err != nil {
log.Errorf("dns:Client.Exchange ... %v: %v", dnssrvcnf.Server, err)
return
}
if rsp.Rcode == dns.RcodeSuccess {
log.Debugf("Update ok %+v", rev_msg.Ns)
} else {
log.Errorln("error updating rcode=%v for %+v", dns.RcodeToString[rsp.Rcode], rev_msg.Ns)
}
} else {
log.Debugf("%v already ok in %v", hostname, ip)
}
}
func (u *NSUpdater) Ensure(hostname string, ip string) {
hostname = dns.Fqdn(hostname)
// Reverse
u.ensureReverse(hostname, ip, false)
// Straight
u.ensureStraight(hostname, ip, false)
}
func (u *NSUpdater) Delete(hostname string, ip string) {
hostname = dns.Fqdn(hostname)
// Reverse
u.ensureReverse(hostname, ip, true)
// Straight
if hostname != "." {
u.ensureStraight(hostname, ip, true)
}
}
func (s *NSUpdater) GetServerConfigForHostname(hostname string) (*ServerDefinition, string, error) {
var elected_serverdef ServerDefinition
var elected_zonename string
matchlen := 0
for _, definitionholder := range []map[string]ServerDefinition{
s.config.Forward,
s.config.Reverse} {
for zonename, serverdef := range definitionholder {
if strings.HasSuffix(hostname, "."+zonename) {
clen := len(zonename)
if clen > matchlen {
matchlen = clen
elected_serverdef = serverdef
elected_zonename = zonename
}
}
}
}
if matchlen == 0 {
return nil, "", fmt.Errorf("no server found to handle hostname %v", hostname)
} else {
return &elected_serverdef, elected_zonename, nil
}
}