2016-08-19 17:14:17 -07:00
|
|
|
package dnssec
|
2016-04-26 17:57:11 +01:00
|
|
|
|
|
|
|
|
import (
|
2017-09-01 08:52:13 +02:00
|
|
|
"fmt"
|
2016-10-18 13:33:23 -07:00
|
|
|
"strconv"
|
2016-04-27 10:48:22 +00:00
|
|
|
"strings"
|
2016-04-26 17:57:11 +01:00
|
|
|
|
2017-02-21 22:51:47 -08:00
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin"
|
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/cache"
|
2016-08-19 17:14:17 -07:00
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
2016-04-26 17:57:11 +01:00
|
|
|
)
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
func init() {
|
|
|
|
|
caddy.RegisterPlugin("dnssec", caddy.Plugin{
|
|
|
|
|
ServerType: "dns",
|
|
|
|
|
Action: setup,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setup(c *caddy.Controller) error {
|
2016-10-18 13:33:23 -07:00
|
|
|
zones, keys, capacity, err := dnssecParse(c)
|
2016-04-26 17:57:11 +01:00
|
|
|
if err != nil {
|
2017-09-14 09:36:06 +01:00
|
|
|
return plugin.Error("dnssec", err)
|
2016-04-26 17:57:11 +01:00
|
|
|
}
|
|
|
|
|
|
2017-06-13 12:39:10 -07:00
|
|
|
ca := cache.New(capacity)
|
2017-09-14 09:36:06 +01:00
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
2017-06-13 12:39:10 -07:00
|
|
|
return New(zones, keys, next, ca)
|
2016-08-19 17:14:17 -07:00
|
|
|
})
|
|
|
|
|
|
2016-10-26 10:01:52 +01:00
|
|
|
// Export the capacity for the metrics. This only happens once, because this is a re-load change only.
|
|
|
|
|
cacheCapacity.WithLabelValues("signature").Set(float64(capacity))
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
return nil
|
2016-04-26 17:57:11 +01:00
|
|
|
}
|
|
|
|
|
|
2016-10-18 13:33:23 -07:00
|
|
|
func dnssecParse(c *caddy.Controller) ([]string, []*DNSKEY, int, error) {
|
2016-04-26 17:57:11 +01:00
|
|
|
zones := []string{}
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
keys := []*DNSKEY{}
|
2016-10-18 13:33:23 -07:00
|
|
|
|
|
|
|
|
capacity := defaultCap
|
2016-04-26 17:57:11 +01:00
|
|
|
for c.Next() {
|
2017-08-10 05:30:18 -07:00
|
|
|
// dnssec [zones...]
|
|
|
|
|
zones = make([]string, len(c.ServerBlockKeys))
|
|
|
|
|
copy(zones, c.ServerBlockKeys)
|
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
|
zones = args
|
|
|
|
|
}
|
2016-04-26 17:57:11 +01:00
|
|
|
|
2017-08-10 05:30:18 -07:00
|
|
|
for c.NextBlock() {
|
|
|
|
|
switch c.Val() {
|
|
|
|
|
case "key":
|
|
|
|
|
k, e := keyParse(c)
|
|
|
|
|
if e != nil {
|
|
|
|
|
return nil, nil, 0, e
|
2016-04-26 17:57:11 +01:00
|
|
|
}
|
2017-08-10 05:30:18 -07:00
|
|
|
keys = append(keys, k...)
|
|
|
|
|
case "cache_capacity":
|
|
|
|
|
if !c.NextArg() {
|
|
|
|
|
return nil, nil, 0, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
value := c.Val()
|
|
|
|
|
cacheCap, err := strconv.Atoi(value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
capacity = cacheCap
|
2016-04-26 17:57:11 +01:00
|
|
|
}
|
2017-08-10 05:30:18 -07:00
|
|
|
|
2016-04-26 17:57:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
2016-09-23 09:14:12 +01:00
|
|
|
for i := range zones {
|
2017-09-14 09:36:06 +01:00
|
|
|
zones[i] = plugin.Host(zones[i]).Normalize()
|
2016-04-26 17:57:11 +01:00
|
|
|
}
|
2017-09-01 08:52:13 +02:00
|
|
|
|
|
|
|
|
// Check if each keys owner name can actually sign the zones we want them to sign
|
|
|
|
|
for _, k := range keys {
|
2017-09-14 09:36:06 +01:00
|
|
|
kname := plugin.Name(k.K.Header().Name)
|
2017-09-01 08:52:13 +02:00
|
|
|
ok := false
|
|
|
|
|
for i := range zones {
|
|
|
|
|
if kname.Matches(zones[i]) {
|
|
|
|
|
ok = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !ok {
|
|
|
|
|
return zones, keys, capacity, fmt.Errorf("key %s (keyid: %d) can not sign any of the zones", string(kname), k.keytag)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 13:33:23 -07:00
|
|
|
return zones, keys, capacity, nil
|
2016-04-26 17:57:11 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
func keyParse(c *caddy.Controller) ([]*DNSKEY, error) {
|
|
|
|
|
keys := []*DNSKEY{}
|
2016-04-26 17:57:11 +01:00
|
|
|
|
|
|
|
|
if !c.NextArg() {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
value := c.Val()
|
2016-10-18 13:33:23 -07:00
|
|
|
if value == "file" {
|
|
|
|
|
ks := c.RemainingArgs()
|
2017-09-01 08:52:13 +02:00
|
|
|
if len(ks) == 0 {
|
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 13:33:23 -07:00
|
|
|
for _, k := range ks {
|
|
|
|
|
base := k
|
|
|
|
|
// Kmiek.nl.+013+26205.key, handle .private or without extension: Kmiek.nl.+013+26205
|
|
|
|
|
if strings.HasSuffix(k, ".key") {
|
|
|
|
|
base = k[:len(k)-4]
|
|
|
|
|
}
|
|
|
|
|
if strings.HasSuffix(k, ".private") {
|
|
|
|
|
base = k[:len(k)-8]
|
|
|
|
|
}
|
|
|
|
|
k, err := ParseKeyFile(base+".key", base+".private")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2016-04-26 17:57:11 +01:00
|
|
|
}
|
2016-10-18 13:33:23 -07:00
|
|
|
keys = append(keys, k)
|
2016-04-26 17:57:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return keys, nil
|
|
|
|
|
}
|