Debian Codegraph @

cmd/atomdns/handlers/acl/setup.go

raw
1 package acl
2
3 import (
4 "net/netip"
5 "strconv"
6 "strings"
7
8 "codeberg.org/miekg/dns"
9 "codeberg.org/miekg/dns/cmd/atomdns/internal/dnsctx"
10 "codeberg.org/miekg/dns/cmd/atomdns/internal/dnsserver"
11 "github.com/phemmer/go-iptrie"
12 )
13
14 const (
15 nettype = iota
16 contextype
17 )
18
19 func (a *Acl) Setup(co *dnsserver.Controller) error {
20 var IPv4All, _ = netip.ParsePrefix("0.0.0.0/0")
21 var IPv6All, _ = netip.ParsePrefix("::/0")
22
23 a.N = co.Global.MetricsN
24
25 for co.Next() {
26 r := rule{}
27 for co.NextBlock(0) {
28 p := policy{}
29
30 switch co.Val() {
31 case "allow":
32 p.action = dns.MsgAccept
33 case "block":
34 p.action = dns.MsgReject
35 case "filter":
36 p.action = MsgFilter
37 case "drop":
38 p.action = dns.MsgIgnore
39 default:
40 return co.Errf("unexpected token %s, expected 'allow', 'block', 'filter' or 'drop'", co.Val())
41 }
42
43 args := co.RemainingArgs()
44
45 if len(args) == 0 {
46 p.net = &policyNet{filter: iptrie.NewTrie()}
47 p.net.filter.Insert(IPv4All, nil)
48 p.net.filter.Insert(IPv6All, nil)
49 r.policies = append(r.policies, p)
50 continue
51 }
52
53 hasnet := false
54 // qtype, cidr of ctx key
55 tp := contextype
56 if dns.StringToType[args[0]] != 0 {
57 tp = nettype
58 }
59 if _, ok := dns.StringToOpcode[args[0]]; ok {
60 tp = nettype
61 }
62 if _, err := netip.ParsePrefix(normalize(args[0])); err == nil { // == nil
63 tp = nettype
64 }
65 if tp == contextype && !dnsctx.Valid(args[0]) {
66 return co.Errf("invalid context key: %s", args[0])
67 }
68 if tp == nettype {
69 p.net = &policyNet{filter: iptrie.NewTrie()}
70 }
71 for i, arg := range args {
72 switch tp {
73 case contextype:
74 if i == 0 {
75 p.ctx = new(policyCtx)
76 p.ctx.ctx = arg
77 } else {
78 p.ctx.values = append(p.ctx.values, argtotype(arg))
79 }
80 case nettype:
81 qtype := dns.StringToType[arg]
82 if qtype != 0 {
83 p.net.qtypes = append(p.net.qtypes, qtype)
84
85 } else {
86 opcode, ok := dns.StringToOpcode[arg]
87
88 if ok {
89 p.net.opcodes = append(p.net.opcodes, opcode)
90
91 } else {
92 source, err := netip.ParsePrefix(normalize(arg))
93 if err != nil {
94 return co.Errf("illegal CIDR notation %s", normalize(arg))
95 }
96 hasnet = true
97 p.net.filter.Insert(source, nil)
98 }
99 }
100 }
101 }
102 if tp == nettype && !hasnet {
103 p.net.filter.Insert(IPv4All, nil)
104 p.net.filter.Insert(IPv6All, nil)
105 hasnet = true
106 }
107 r.policies = append(r.policies, p)
108 }
109 a.Rules = append(a.Rules, r)
110 }
111 return nil
112 }
113
114 // normalize appends '/32' for any single IPv4 address and '/128' for IPv6.
115 func normalize(rawNet string) string {
116 if strings.Contains(rawNet, "/") {
117 return rawNet
118 }
119 if strings.Contains(rawNet, ":") {
120 return rawNet + "/128"
121 }
122 return rawNet + "/32"
123 }
124
125 // argtotype take the string arg and determines what the type could be and returns it. I.e. "true" will be
126 // parsed as a bool that is true. This functions handles strings, int, float64s and bools.
127 func argtotype(arg string) any {
128 if arg == "true" {
129 return true
130 }
131 if arg == "false" {
132 return false
133 }
134 if i, err := strconv.ParseInt(arg, 10, 64); err == nil {
135 return i
136 }
137 if f, err := strconv.ParseFloat(arg, 64); err == nil {
138 return f
139 }
140 return arg
141 }