| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	"path/filepath"
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2017-12-27 15:48:14 +00:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metrics"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/cache"
 | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | 	clog "github.com/coredns/coredns/plugin/pkg/log"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy"
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | var log = clog.NewWithPlugin("dnssec")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 08:02:30 +01:00
										 |  |  | func init() { plugin.Register("dnssec", setup) }
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | 	zones, keys, capacity, splitkeys, 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 {
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | 		return New(zones, keys, splitkeys, next, ca)
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-27 15:48:14 +00:00
										 |  |  | 	c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2018-09-19 05:11:24 -04:00
										 |  |  | 		metrics.MustRegister(c, cacheSize, cacheHits, cacheMisses)
 | 
					
						
							| 
									
										
										
										
											2017-12-27 15:48:14 +00:00
										 |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	return nil
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | func dnssecParse(c *caddy.Controller) ([]string, []*DNSKEY, int, bool, 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
 | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	i := 0
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	for c.Next() {
 | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 		if i > 0 {
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | 			return nil, nil, 0, false, plugin.ErrOnce
 | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		i++
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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() {
 | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			switch x := c.Val(); x {
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 			case "key":
 | 
					
						
							|  |  |  | 				k, e := keyParse(c)
 | 
					
						
							|  |  |  | 				if e != nil {
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | 					return nil, nil, 0, false, 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() {
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | 					return nil, nil, 0, false, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 				value := c.Val()
 | 
					
						
							|  |  |  | 				cacheCap, err := strconv.Atoi(value)
 | 
					
						
							|  |  |  | 				if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | 					return nil, nil, 0, false, err
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 				capacity = cacheCap
 | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 			default:
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | 				return nil, nil, 0, false, c.Errf("unknown property '%s'", x)
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | 	// Check if we have both KSKs and ZSKs.
 | 
					
						
							|  |  |  | 	zsk, ksk := 0, 0
 | 
					
						
							|  |  |  | 	for _, k := range keys {
 | 
					
						
							|  |  |  | 		if k.isKSK() {
 | 
					
						
							|  |  |  | 			ksk++
 | 
					
						
							|  |  |  | 		} else if k.isZSK() {
 | 
					
						
							|  |  |  | 			zsk++
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	splitkeys := zsk > 0 && ksk > 0
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 09:22:02 +01:00
										 |  |  | 	// Check if each keys owner name can actually sign the zones we want them to sign.
 | 
					
						
							| 
									
										
										
										
											2017-09-01 08:52:13 +02:00
										 |  |  | 	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 {
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | 			return zones, keys, capacity, splitkeys, fmt.Errorf("key %s (keyid: %d) can not sign any of the zones", string(kname), k.tag)
 | 
					
						
							| 
									
										
										
										
											2017-09-01 08:52:13 +02:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-20 17:35:59 +02:00
										 |  |  | 	return zones, keys, capacity, splitkeys, 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{}
 | 
					
						
							| 
									
										
										
										
											2018-10-13 19:49:55 +02:00
										 |  |  | 	config := dnsserver.GetConfig(c)
 | 
					
						
							| 
									
										
										
										
											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]
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 			if !filepath.IsAbs(base) && config.Root != "" {
 | 
					
						
							|  |  |  | 				base = filepath.Join(config.Root, base)
 | 
					
						
							| 
									
										
										
										
											2018-10-13 19:49:55 +02:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-10-18 13:33:23 -07:00
										 |  |  | 			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
 | 
					
						
							|  |  |  | }
 |