-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstatus_cmd.go
More file actions
393 lines (340 loc) · 13 KB
/
status_cmd.go
File metadata and controls
393 lines (340 loc) · 13 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package main
import (
"fmt"
"github.com/miekg/dns"
)
func init() {
Command["status"] = StatusCmd
CommandHelp["status"] = "Check status of a signer group, requires <fqdn>"
}
func StatusCmd(args []string, remote bool, output *[]string) error {
//
// This function checks the status of all steps and can be a bit
// misleading when reporting missing or needs removal for things
// that shouldn't be done. But the relevant states for each automation
// stage is correct.
//
// TODO: could be split into many different functions that only checks
// a specific state
//
if len(args) < 1 {
return fmt.Errorf("requires <fqdn>")
}
if !Config.Exists("signers:" + args[0]) {
return fmt.Errorf("group %s has no signers", args[0])
}
signers := Config.ListGet("signers:" + args[0])
dnskeys := make(map[string][]*dns.DNSKEY)
cdnskeys := make(map[string][]*dns.CDNSKEY)
cdses := make(map[string][]*dns.CDS)
nses := make(map[string][]*dns.NS)
for _, signer := range signers {
ip := Config.Get("signer:"+signer, "")
if ip == "" {
return fmt.Errorf("No ip|host for signer %s", signer)
}
m := new(dns.Msg)
m.SetQuestion(args[0], dns.TypeDNSKEY)
c := new(dns.Client)
r, _, err := c.Exchange(m, ip)
if err != nil {
return err
}
dnskeys[signer] = []*dns.DNSKEY{}
for _, a := range r.Answer {
dnskey, ok := a.(*dns.DNSKEY)
if !ok {
continue
}
owner := Config.Get("dnskey-origin:"+fmt.Sprintf("%d-%d-%s", dnskey.Protocol, dnskey.Algorithm, dnskey.PublicKey), "")
if owner != "" {
owner = " (owner: " + owner + ")"
}
*output = append(*output, fmt.Sprintf("%s: found DNSKEY %d %d %d %s%s", signer, dnskey.Flags, dnskey.Protocol, dnskey.Algorithm, dnskey.PublicKey, owner))
dnskeys[signer] = append(dnskeys[signer], dnskey)
}
m = new(dns.Msg)
m.SetQuestion(args[0], dns.TypeCDS)
r, _, err = c.Exchange(m, ip)
if err != nil {
return err
}
cdses[signer] = []*dns.CDS{}
for _, a := range r.Answer {
cds, ok := a.(*dns.CDS)
if !ok {
continue
}
*output = append(*output, fmt.Sprintf("%s: found CDS %d %d %d %s", signer, cds.KeyTag, cds.Algorithm, cds.DigestType, cds.Digest))
cdses[signer] = append(cdses[signer], cds)
}
m = new(dns.Msg)
m.SetQuestion(args[0], dns.TypeCDNSKEY)
r, _, err = c.Exchange(m, ip)
if err != nil {
return err
}
cdnskeys[signer] = []*dns.CDNSKEY{}
for _, a := range r.Answer {
cdnskey, ok := a.(*dns.CDNSKEY)
if !ok {
continue
}
*output = append(*output, fmt.Sprintf("%s: found CDNSKEY %d %d %d %s", signer, cdnskey.Flags, cdnskey.Protocol, cdnskey.Algorithm, cdnskey.PublicKey))
cdnskeys[signer] = append(cdnskeys[signer], cdnskey)
}
m = new(dns.Msg)
m.SetQuestion(args[0], dns.TypeNS)
r, _, err = c.Exchange(m, ip)
if err != nil {
return err
}
nses[signer] = []*dns.NS{}
for _, a := range r.Answer {
ns, ok := a.(*dns.NS)
if !ok {
continue
}
owner := Config.Get("ns-origin:"+ns.Ns, "")
if owner != "" {
owner = " (owner: " + owner + ")"
}
*output = append(*output, fmt.Sprintf("%s: found NS %s%s", signer, ns.Ns, owner))
nses[signer] = append(nses[signer], ns)
}
}
group_dnskeys_synced := true
for signer, keys := range dnskeys {
if Config.Get("signer-leaving:"+signer, "") != "" {
*output = append(*output, fmt.Sprintf("Skipping sync status of %s DNSKEYs: leaving signer", signer))
continue
}
*output = append(*output, fmt.Sprintf("Check sync status of %s DNSKEYs", signer))
for _, key := range keys {
if f := key.Flags & 0x101; f == 256 { // only process ZSK's
for osigner, okeys := range dnskeys {
if osigner == signer {
continue
}
found := false
for _, okey := range okeys {
if f := okey.Flags & 0x101; f == 256 && okey.PublicKey == key.PublicKey {
if okey.Protocol != key.Protocol {
*output = append(*output, fmt.Sprintf("Found DNSKEY in %s but missmatch Protocol: %s", osigner, key.PublicKey))
break
}
if okey.Algorithm != key.Algorithm {
*output = append(*output, fmt.Sprintf("Found DNSKEY in %s but missmatch Protocol: %s", osigner, key.PublicKey))
break
}
found = true
break
}
}
if found {
// key was found, check if it's owner is leaving
owner := Config.Get("dnskey-origin:"+fmt.Sprintf("%d-%d-%s", key.Protocol, key.Algorithm, key.PublicKey), "")
if owner != "" && Config.Get("signer-leaving:"+owner, "") != "" {
*output = append(*output, fmt.Sprintf("DNSKEY needs removal for %s: %s", osigner, key.PublicKey))
group_dnskeys_synced = false
}
} else {
*output = append(*output, fmt.Sprintf("DNSKEY missing in %s: %s", osigner, key.PublicKey))
group_dnskeys_synced = false
}
}
}
}
}
if group_dnskeys_synced {
Config.Set("group-dnskeys-synced:"+args[0], "yes")
} else {
Config.Remove("group-dnskeys-synced:" + args[0])
}
ksks := []*dns.DNSKEY{}
for signer, keys := range dnskeys {
if Config.Get("signer-leaving:"+signer, "") != "" {
continue
}
for _, key := range keys {
if f := key.Flags & 0x101; f == 257 {
ksks = append(ksks, key)
}
}
}
group_cdscdnskeys_synced := true
for signer, keys := range cdses {
if Config.Get("signer-leaving:"+signer, "") != "" {
*output = append(*output, fmt.Sprintf("Skipping sync status of %s CDSes: leaving signer", signer))
continue
}
*output = append(*output, fmt.Sprintf("Check sync status of %s CDSes", signer))
for _, ksk := range ksks {
found := false
for _, key := range keys {
cds := ksk.ToDS(key.DigestType).ToCDS()
if cds.KeyTag == key.KeyTag && cds.Algorithm == key.Algorithm && cds.Digest == key.Digest {
found = true
break
}
}
if !found {
*output = append(*output, fmt.Sprintf("CDS missing for KSK: %s", ksk.PublicKey))
group_cdscdnskeys_synced = false
}
}
}
for signer, keys := range cdnskeys {
if Config.Get("signer-leaving:"+signer, "") != "" {
*output = append(*output, fmt.Sprintf("Skipping sync status of %s CDNSKEYs: leaving signer", signer))
continue
}
*output = append(*output, fmt.Sprintf("Check sync status of %s CDNSKEYs", signer))
for _, ksk := range ksks {
found := false
for _, key := range keys {
cdnskey := ksk.ToCDNSKEY()
if cdnskey.Flags == key.Flags && cdnskey.Protocol == key.Protocol && cdnskey.Algorithm == key.Algorithm && cdnskey.PublicKey == key.PublicKey {
found = true
break
}
}
if !found {
*output = append(*output, fmt.Sprintf("CDNSKEY missing for KSK: %s", ksk.PublicKey))
group_cdscdnskeys_synced = false
}
}
}
if group_cdscdnskeys_synced {
Config.Set("group-cdscdnskeys-synced:"+args[0], "yes")
} else {
Config.Remove("group-cdscdnskeys-synced:" + args[0])
}
group_nses_synced := true
nsmap := make(map[string]*dns.NS)
for _, rrs := range nses {
for _, rr := range rrs {
nsmap[rr.Ns] = rr
}
}
nsset := []*dns.NS{}
for _, rr := range nsmap {
nsset = append(nsset, rr)
}
for signer, keys := range nses {
*output = append(*output, fmt.Sprintf("Check sync status of %s NSes", signer))
for _, ns := range nsset {
found := false
for _, key := range keys {
if ns.Ns == key.Ns {
found = true
break
}
}
if !found {
*output = append(*output, fmt.Sprintf("NS missing: %s", ns.Ns))
group_nses_synced = false
}
}
}
*output = append(*output, "Check sync status of NSes for leaving signers")
for _, signer := range signers {
if Config.Get("signer-leaving:"+signer, "") != "" {
leave_ns := Config.Get("signer-ns:"+signer, "")
for _, ns := range nsset {
if ns.Ns == leave_ns {
*output = append(*output, fmt.Sprintf(" need removal of leaving %s NS: %s", signer, leave_ns))
group_nses_synced = false
}
}
}
}
if group_nses_synced {
Config.Set("group-nses-synced:"+args[0], "yes")
} else {
Config.Remove("group-nses-synced:" + args[0])
}
parent := Config.Get("parent:"+args[0], "")
if parent == "" {
return fmt.Errorf("No ip|host for parent of %s", args[0])
}
*output = append(*output, fmt.Sprintf("Check sync status of parent %s", parent))
m := new(dns.Msg)
m.SetQuestion(args[0], dns.TypeDS)
c := new(dns.Client)
r, _, err := c.Exchange(m, parent)
if err != nil {
return err
}
dses := []*dns.DS{}
removedses := make(map[string]*dns.DS)
for _, a := range r.Answer {
ds, ok := a.(*dns.DS)
if !ok {
continue
}
*output = append(*output, fmt.Sprintf(" found DS %d %d %d %s", ds.KeyTag, ds.Algorithm, ds.DigestType, ds.Digest))
dses = append(dses, ds)
removedses[fmt.Sprintf("%d %d %d %s", ds.KeyTag, ds.Algorithm, ds.DigestType, ds.Digest)] = ds
}
group_parent_ds_synced := group_cdscdnskeys_synced
cdsmap := make(map[string]*dns.CDS)
for _, keys := range cdses {
for _, key := range keys {
cdsmap[fmt.Sprintf("%d %d %d %s", key.KeyTag, key.Algorithm, key.DigestType, key.Digest)] = key
delete(removedses, fmt.Sprintf("%d %d %d %s", key.KeyTag, key.Algorithm, key.DigestType, key.Digest))
}
}
for _, ds := range dses {
delete(cdsmap, fmt.Sprintf("%d %d %d %s", ds.KeyTag, ds.Algorithm, ds.DigestType, ds.Digest))
}
for _, cds := range cdsmap {
*output = append(*output, fmt.Sprintf(" Missing DS for CDS: %d %d %d %s", cds.KeyTag, cds.Algorithm, cds.DigestType, cds.Digest))
group_parent_ds_synced = false
}
for _, ds := range removedses {
*output = append(*output, fmt.Sprintf(" DS needs removal: %d %d %d %s", ds.KeyTag, ds.Algorithm, ds.DigestType, ds.Digest))
group_parent_ds_synced = false
}
if group_parent_ds_synced {
Config.Set("group-parent-ds-synced:"+args[0], "yes")
} else {
Config.Remove("group-parent-ds-synced:" + args[0])
}
m = new(dns.Msg)
m.SetQuestion(args[0], dns.TypeNS)
r, _, err = c.Exchange(m, parent)
if err != nil {
return err
}
group_parent_ns_synced := group_nses_synced
leavingns := make(map[string]bool)
for _, signer := range signers {
if Config.Get("signer-leaving:"+signer, "") != "" {
leavingns[Config.Get("signer-ns:"+signer, "")] = true
}
}
for _, a := range r.Ns {
ns, ok := a.(*dns.NS)
if !ok {
continue
}
if _, ok := leavingns[ns.Ns]; ok {
*output = append(*output, fmt.Sprintf(" found leaving NS %s, need removal", ns.Ns))
group_parent_ns_synced = false
} else {
*output = append(*output, fmt.Sprintf(" found NS %s", ns.Ns))
}
delete(nsmap, ns.Ns)
}
for ns, _ := range nsmap {
*output = append(*output, fmt.Sprintf(" Missing NS: %s", ns))
group_parent_ns_synced = false
}
if group_parent_ns_synced {
Config.Set("group-parent-ns-synced:"+args[0], "yes")
} else {
Config.Remove("group-parent-ns-synced:" + args[0])
}
return nil
}