Debian Codegraph @

dnsconf/clientconfig.go

raw
1 // Package dnsconf is used to get the DNS system configuration, typically stored in /etc/resolv.conf on Unix
2 // systems. There are many possible options, only a few are supported.
3 package dnsconf
4
5 import (
6 "bufio"
7 "io"
8 "os"
9 "slices"
10 "strconv"
11 "strings"
12
13 "codeberg.org/miekg/dns/dnsutil"
14 )
15
16 // Config wraps the contents of the /etc/resolv.conf file.
17 type Config struct {
18 Servers []string // Servers to use.
19 Search []string // Suffixes to append to local name.
20 Port string // Port to use.
21 Ndots int // Number of dots in name to trigger absolute lookup.
22 Timeout int // Seconds before giving up on packet.
23 Attempts int // Lost packets before giving up on server.
24 Rotate bool // Rotate between servers.
25 Network string // Set to "tcp" when "use-vc" is set.
26 }
27
28 // FromFile parses a resolv.conf(5) like file and returns a [*Config].
29 func FromFile(resolvconf string) (*Config, error) {
30 file, err := os.Open(resolvconf)
31 if err != nil {
32 return nil, err
33 }
34 defer file.Close()
35 return FromReader(file)
36 }
37
38 // FromReader works like [FromFile] but takes an io.Reader as argument.
39 func FromReader(resolvconf io.Reader) (*Config, error) {
40 c := &Config{
41 Port: "53",
42 Ndots: 1,
43 Timeout: 5,
44 Attempts: 2,
45 }
46 c.Servers = make([]string, 0)
47 c.Search = make([]string, 0)
48
49 scanner := bufio.NewScanner(resolvconf)
50 for scanner.Scan() {
51 line := scanner.Text()
52 f := strings.Fields(line)
53 if len(f) < 1 {
54 continue
55 }
56 switch f[0] {
57 case "nameserver": // add one name server
58 if len(f) > 1 {
59 // One more check: make sure server name is just an IP address. Otherwise we need DNS
60 // to look it up.
61 name := f[1]
62 c.Servers = append(c.Servers, name)
63 }
64
65 case "domain": // set search path to just this domain
66 if len(f) > 1 {
67 c.Search = make([]string, 1)
68 c.Search[0] = f[1]
69 } else {
70 c.Search = make([]string, 0)
71 }
72
73 case "search": // set search path to given servers
74 c.Search = slices.Clone(f[1:])
75
76 case "options": // magic options
77 for _, s := range f[1:] {
78 switch {
79 case len(s) >= 6 && s[:6] == "ndots:":
80 n, _ := strconv.Atoi(s[6:])
81 if n < 0 {
82 n = 0
83 } else if n > 15 {
84 n = 15
85 }
86 c.Ndots = n
87 case len(s) >= 8 && s[:8] == "timeout:":
88 n, _ := strconv.Atoi(s[8:])
89 if n < 1 {
90 n = 1
91 }
92 c.Timeout = n
93 case len(s) >= 9 && s[:9] == "attempts:":
94 n, _ := strconv.Atoi(s[9:])
95 if n < 1 {
96 n = 1
97 }
98 c.Attempts = n
99 case s == "rotate":
100 c.Rotate = true
101 case s == "use-vc":
102 c.Network = "tcp"
103 }
104 }
105 }
106 }
107 if err := scanner.Err(); err != nil {
108 return nil, err
109 }
110 return c, nil
111 }
112
113 // NameList returns all of the names that should be queried based on the
114 // config. It is based off of go's net/dns name building, but it does not
115 // check the length of the resulting names.
116 func (c *Config) NameList(name string) []string {
117 // if this domain is already fully qualified, no append needed.
118 if dnsutil.IsFqdn(name) {
119 return []string{name}
120 }
121
122 // Check to see if the name has more labels than Ndots. Do this before making
123 // the domain fully qualified.
124 hasNdots := dnsutil.Labels(name) > c.Ndots
125 // Make the domain fully qualified.
126 name = dnsutil.Fqdn(name)
127
128 // Make a list of names based off search.
129 names := []string{}
130
131 // If name has enough dots, try that first.
132 if hasNdots {
133 names = append(names, name)
134 }
135 for _, s := range c.Search {
136 names = append(names, dnsutil.Fqdn(name+s))
137 }
138 // If we didn't have enough dots, try after suffixes.
139 if !hasNdots {
140 names = append(names, name)
141 }
142 return names
143 }