DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 4f502ff6f53c7165f7064d30ed7d28d1a09ed328 felu/internal/dns/query.go -rw-r--r-- 3.9 KiB
4f502ff6Jonni Liljamo feat: config rearchitect, lower qName early 21 days ago
                                                                                
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
/*
 * Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */

package dns

import (
	"log/slog"
	"net"
	"strings"

	"git.src.quest/~liljamo/felu/internal/config"
	"git.src.quest/~liljamo/felu/internal/db"
	"github.com/miekg/dns"
)

func parseQuery(m *dns.Msg, r *dns.Msg) {
	for _, q := range m.Question {
		slog.Info("Got Query",
			slog.Any("id", r.Id),
			slog.String("type", dns.TypeToString[q.Qtype]),
			slog.String("qname", q.Name),
		)

		switch q.Qtype {
		case dns.TypeA:
			handleARecord(&q, m, r)
		case dns.TypeCNAME:
			// NOTE: This is stubbed like this to make things like lego not shit themselves if they get NOTIMP.
			m.SetRcode(r, dns.RcodeNameError)
		case dns.TypeNS:
			handleNSRecord(&q, m, r)
		case dns.TypeSOA:
			handleSOARecord(&q, m, r)
		case dns.TypeTXT:
			handleTXTRecord(&q, m, r)
		default:
			m.SetRcode(r, dns.RcodeNotImplemented)
		}

		slog.Info("Responding to Query",
			slog.Any("id", r.Id),
			slog.String("rcode", dns.RcodeToString[m.Rcode]),
		)
	}
}

func handleARecord(q *dns.Question, m *dns.Msg, r *dns.Msg) {
	if index := strings.IndexByte(q.Name, '.'); index >= 0 {
		aRecord, err := db.FetchDomainARecord(strings.ToLower(q.Name[:index]))
		if err != nil {
			m.SetRcode(r, dns.RcodeNameError)
		} else {
			m.Answer = append(m.Answer, &dns.A{
				Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60},
				A:   net.ParseIP(aRecord),
			})
		}
	} else {
		m.SetRcode(r, dns.RcodeNameError)
	}
}

func handleNSRecord(q *dns.Question, m *dns.Msg, r *dns.Msg) {
	qName := strings.ToLower(q.Name)

	ns := &dns.NS{
		Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeNS, Class: dns.ClassINET, Ttl: 86400},
		Ns:  config.FeluConfig.Domain,
	}

	// "Root" Domain NS.
	if qName == config.FeluConfig.Domain {
		m.Answer = append(m.Answer, ns)
		return
	}

	if index := strings.IndexByte(qName, '.'); index >= 0 {
		// FIXME: other way of checking that the domain exists
		_, err := db.FetchDomainARecord(qName[:index])
		if err != nil {
			m.SetRcode(r, dns.RcodeNameError)
		} else {
			m.Answer = append(m.Answer, ns)
		}
	} else {
		m.SetRcode(r, dns.RcodeNameError)
	}
}

func handleSOARecord(q *dns.Question, m *dns.Msg, r *dns.Msg) {
	qName := strings.ToLower(q.Name)

	soa := &dns.SOA{
		Hdr:     dns.RR_Header{Name: q.Name, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 0},
		Ns:      config.FeluConfig.Domain,
		Mbox:    config.FeluConfig.SOAEmail,
		Serial:  2024100301,
		Refresh: 86400,
		Retry:   7200,
		Expire:  3600000,
		Minttl:  172800,
	}

	// "Root" Domain SOA.
	if qName == config.FeluConfig.Domain {
		m.Answer = append(m.Answer, soa)
		return
	}

	if index := strings.IndexByte(qName, '.'); index >= 0 {
		// FIXME: other way of checking that the domain exists
		_, err := db.FetchDomainARecord(qName[:index])
		if err != nil {
			m.SetRcode(r, dns.RcodeNameError)
		} else {
			m.Answer = append(m.Answer, soa)
		}
	} else {
		m.SetRcode(r, dns.RcodeNameError)
	}
}

func handleTXTRecord(q *dns.Question, m *dns.Msg, r *dns.Msg) {
	qName := strings.ToLower(q.Name)

	// NOTE: This handles only _acme-challenge queries
	expectedPrefix := "_acme-challenge."
	if !strings.HasPrefix(qName, expectedPrefix) {
		m.SetRcode(r, dns.RcodeNameError)
		return
	}

	qNameWithoutPrefix, ok := strings.CutPrefix(qName, expectedPrefix)
	if !ok {
		m.SetRcode(r, dns.RcodeFormatError)
		return
	}

	var ddnsDomain string
	if index := strings.IndexByte(qNameWithoutPrefix, '.'); index >= 0 {
		ddnsDomain = qNameWithoutPrefix[:index]
	} else {
		m.SetRcode(r, dns.RcodeNameError)
		return
	}

	txtRecord, err := db.FetchDomainAcmeChallenge(ddnsDomain)
	if err != nil {
		m.SetRcode(r, dns.RcodeNameError)
	} else {
		m.Answer = append(m.Answer, &dns.TXT{
			Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 0},
			Txt: []string{txtRecord},
		})
	}
}