2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2022-12-20 14:59:11 -07:00
|
|
|
"strings"
|
2019-11-19 10:00:20 -07:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2021-03-26 08:46:30 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// This whole thing should be rewritten to use context
|
|
|
|
|
|
|
|
var dnsR *dnsRecords
|
2019-12-11 18:42:55 -07:00
|
|
|
var dnsServer *dns.Server
|
|
|
|
var dnsAddr string
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
type dnsRecords struct {
|
|
|
|
sync.RWMutex
|
|
|
|
dnsMap map[string]string
|
|
|
|
hostMap *HostMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDnsRecords(hostMap *HostMap) *dnsRecords {
|
|
|
|
return &dnsRecords{
|
|
|
|
dnsMap: make(map[string]string),
|
|
|
|
hostMap: hostMap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dnsRecords) Query(data string) string {
|
|
|
|
d.RLock()
|
2022-12-20 14:59:11 -07:00
|
|
|
defer d.RUnlock()
|
|
|
|
if r, ok := d.dnsMap[strings.ToLower(data)]; ok {
|
2019-11-19 10:00:20 -07:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dnsRecords) QueryCert(data string) string {
|
|
|
|
ip := net.ParseIP(data[:len(data)-1])
|
|
|
|
if ip == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2021-11-03 19:54:04 -06:00
|
|
|
iip := iputil.Ip2VpnIp(ip)
|
2023-07-24 11:37:52 -06:00
|
|
|
hostinfo := d.hostMap.QueryVpnIp(iip)
|
|
|
|
if hostinfo == nil {
|
2019-11-19 10:00:20 -07:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
q := hostinfo.GetCert()
|
|
|
|
if q == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
cert := q.Details
|
|
|
|
c := fmt.Sprintf("\"Name: %s\" \"Ips: %s\" \"Subnets %s\" \"Groups %s\" \"NotBefore %s\" \"NotAFter %s\" \"PublicKey %x\" \"IsCA %t\" \"Issuer %s\"", cert.Name, cert.Ips, cert.Subnets, cert.Groups, cert.NotBefore, cert.NotAfter, cert.PublicKey, cert.IsCA, cert.Issuer)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dnsRecords) Add(host, data string) {
|
|
|
|
d.Lock()
|
2022-12-20 14:59:11 -07:00
|
|
|
defer d.Unlock()
|
|
|
|
d.dnsMap[strings.ToLower(host)] = data
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
func parseQuery(l *logrus.Logger, m *dns.Msg, w dns.ResponseWriter) {
|
2019-11-19 10:00:20 -07:00
|
|
|
for _, q := range m.Question {
|
|
|
|
switch q.Qtype {
|
|
|
|
case dns.TypeA:
|
|
|
|
l.Debugf("Query for A %s", q.Name)
|
|
|
|
ip := dnsR.Query(q.Name)
|
|
|
|
if ip != "" {
|
|
|
|
rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, ip))
|
|
|
|
if err == nil {
|
|
|
|
m.Answer = append(m.Answer, rr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case dns.TypeTXT:
|
|
|
|
a, _, _ := net.SplitHostPort(w.RemoteAddr().String())
|
|
|
|
b := net.ParseIP(a)
|
|
|
|
// We don't answer these queries from non nebula nodes or localhost
|
|
|
|
//l.Debugf("Does %s contain %s", b, dnsR.hostMap.vpnCIDR)
|
|
|
|
if !dnsR.hostMap.vpnCIDR.Contains(b) && a != "127.0.0.1" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l.Debugf("Query for TXT %s", q.Name)
|
|
|
|
ip := dnsR.QueryCert(q.Name)
|
|
|
|
if ip != "" {
|
|
|
|
rr, err := dns.NewRR(fmt.Sprintf("%s TXT %s", q.Name, ip))
|
|
|
|
if err == nil {
|
|
|
|
m.Answer = append(m.Answer, rr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
func handleDnsRequest(l *logrus.Logger, w dns.ResponseWriter, r *dns.Msg) {
|
2019-11-19 10:00:20 -07:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(r)
|
|
|
|
m.Compress = false
|
|
|
|
|
|
|
|
switch r.Opcode {
|
|
|
|
case dns.OpcodeQuery:
|
2021-03-26 08:46:30 -06:00
|
|
|
parseQuery(l, m, w)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteMsg(m)
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func dnsMain(l *logrus.Logger, hostMap *HostMap, c *config.C) func() {
|
2019-11-19 10:00:20 -07:00
|
|
|
dnsR = newDnsRecords(hostMap)
|
|
|
|
|
|
|
|
// attach request handler func
|
2021-03-26 08:46:30 -06:00
|
|
|
dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
|
|
|
|
handleDnsRequest(l, w, r)
|
|
|
|
})
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
c.RegisterReloadCallback(func(c *config.C) {
|
2021-03-26 08:46:30 -06:00
|
|
|
reloadDns(l, c)
|
|
|
|
})
|
2021-04-16 09:34:28 -06:00
|
|
|
|
|
|
|
return func() {
|
|
|
|
startDns(l, c)
|
|
|
|
}
|
2019-12-11 18:42:55 -07:00
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func getDnsServerAddr(c *config.C) string {
|
2024-04-11 20:44:36 -06:00
|
|
|
dnsHost := strings.TrimSpace(c.GetString("lighthouse.dns.host", ""))
|
|
|
|
// Old guidance was to provide the literal `[::]` in `lighthouse.dns.host` but that won't resolve.
|
|
|
|
if dnsHost == "[::]" {
|
|
|
|
dnsHost = "::"
|
|
|
|
}
|
|
|
|
return net.JoinHostPort(dnsHost, strconv.Itoa(c.GetInt("lighthouse.dns.port", 53)))
|
2019-12-11 18:42:55 -07:00
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func startDns(l *logrus.Logger, c *config.C) {
|
2019-12-11 18:42:55 -07:00
|
|
|
dnsAddr = getDnsServerAddr(c)
|
|
|
|
dnsServer = &dns.Server{Addr: dnsAddr, Net: "udp"}
|
2022-06-21 12:35:23 -06:00
|
|
|
l.WithField("dnsListener", dnsAddr).Info("Starting DNS responder")
|
2019-12-11 18:42:55 -07:00
|
|
|
err := dnsServer.ListenAndServe()
|
|
|
|
defer dnsServer.Shutdown()
|
2019-11-19 10:00:20 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Errorf("Failed to start server: %s\n ", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 18:42:55 -07:00
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func reloadDns(l *logrus.Logger, c *config.C) {
|
2019-12-11 18:42:55 -07:00
|
|
|
if dnsAddr == getDnsServerAddr(c) {
|
|
|
|
l.Debug("No DNS server config change detected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Debug("Restarting DNS server")
|
|
|
|
dnsServer.Shutdown()
|
2021-03-26 08:46:30 -06:00
|
|
|
go startDns(l, c)
|
2019-12-11 18:42:55 -07:00
|
|
|
}
|