2022-02-15 04:17:51 +00:00
|
|
|
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package resolvconffile parses & serializes /etc/resolv.conf-style files.
|
|
|
|
//
|
|
|
|
// It's a leaf package so both net/dns and net/dns/resolver can depend
|
|
|
|
// on it and we can unify a handful of implementations.
|
|
|
|
//
|
|
|
|
// The package is verbosely named to disambiguate it from resolvconf
|
|
|
|
// the daemon, which Tailscale also supports.
|
|
|
|
package resolvconffile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2022-02-15 04:17:51 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"tailscale.com/util/dnsname"
|
2022-11-18 18:13:14 +00:00
|
|
|
"tailscale.com/util/strs"
|
2022-02-15 04:17:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Path is the canonical location of resolv.conf.
|
|
|
|
const Path = "/etc/resolv.conf"
|
|
|
|
|
|
|
|
// Config represents a resolv.conf(5) file.
|
|
|
|
type Config struct {
|
|
|
|
// Nameservers are the IP addresses of the nameservers to use.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
Nameservers []netip.Addr
|
2022-02-15 04:17:51 +00:00
|
|
|
|
|
|
|
// SearchDomains are the domain suffixes to use when expanding
|
|
|
|
// single-label name queries. SearchDomains is additive to
|
|
|
|
// whatever non-Tailscale search domains the OS has.
|
|
|
|
SearchDomains []dnsname.FQDN
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes c to w. It does so in one Write call.
|
|
|
|
func (c *Config) Write(w io.Writer) error {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
io.WriteString(buf, "# resolv.conf(5) file generated by tailscale\n")
|
|
|
|
io.WriteString(buf, "# DO NOT EDIT THIS FILE BY HAND -- CHANGES WILL BE OVERWRITTEN\n\n")
|
|
|
|
for _, ns := range c.Nameservers {
|
|
|
|
io.WriteString(buf, "nameserver ")
|
|
|
|
io.WriteString(buf, ns.String())
|
|
|
|
io.WriteString(buf, "\n")
|
|
|
|
}
|
|
|
|
if len(c.SearchDomains) > 0 {
|
|
|
|
io.WriteString(buf, "search")
|
|
|
|
for _, domain := range c.SearchDomains {
|
|
|
|
io.WriteString(buf, " ")
|
|
|
|
io.WriteString(buf, domain.WithoutTrailingDot())
|
|
|
|
}
|
|
|
|
io.WriteString(buf, "\n")
|
|
|
|
}
|
|
|
|
_, err := w.Write(buf.Bytes())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses a resolv.conf file from r.
|
|
|
|
func Parse(r io.Reader) (*Config, error) {
|
|
|
|
config := new(Config)
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2022-03-19 19:42:46 +00:00
|
|
|
line, _, _ = strings.Cut(line, "#") // remove any comments
|
2022-02-15 04:17:51 +00:00
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
2022-11-18 18:13:14 +00:00
|
|
|
if s, ok := strs.CutPrefix(line, "nameserver"); ok {
|
2022-02-15 04:17:51 +00:00
|
|
|
nameserver := strings.TrimSpace(s)
|
|
|
|
if len(nameserver) == len(s) {
|
|
|
|
return nil, fmt.Errorf("missing space after \"nameserver\" in %q", line)
|
|
|
|
}
|
2022-07-26 03:55:44 +00:00
|
|
|
ip, err := netip.ParseAddr(nameserver)
|
2022-02-15 04:17:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Nameservers = append(config.Nameservers, ip)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-18 18:13:14 +00:00
|
|
|
if s, ok := strs.CutPrefix(line, "search"); ok {
|
2022-02-15 04:17:51 +00:00
|
|
|
domain := strings.TrimSpace(s)
|
|
|
|
if len(domain) == len(s) {
|
|
|
|
// No leading space?!
|
|
|
|
return nil, fmt.Errorf("missing space after \"domain\" in %q", line)
|
|
|
|
}
|
|
|
|
fqdn, err := dnsname.ToFQDN(domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing search domains %q: %w", line, err)
|
|
|
|
}
|
|
|
|
config.SearchDomains = append(config.SearchDomains, fqdn)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseFile parses the named resolv.conf file.
|
|
|
|
func ParseFile(name string) (*Config, error) {
|
|
|
|
fi, err := os.Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if n := fi.Size(); n > 10<<10 {
|
|
|
|
return nil, fmt.Errorf("unexpectedly large %q file: %d bytes", name, n)
|
|
|
|
}
|
|
|
|
all, err := os.ReadFile(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return Parse(bytes.NewReader(all))
|
|
|
|
}
|