2020-07-14 13:12:00 +00:00
|
|
|
// Copyright (c) 2020 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.
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
package dns
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-04-09 22:24:47 +00:00
|
|
|
"fmt"
|
2020-07-14 13:12:00 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-07-30 00:57:25 +00:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
2020-07-14 13:12:00 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"inet.af/netaddr"
|
|
|
|
"tailscale.com/atomicfile"
|
2021-04-09 22:24:47 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2020-07-14 13:12:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
backupConf = "/etc/resolv.pre-tailscale-backup.conf"
|
|
|
|
resolvConf = "/etc/resolv.conf"
|
|
|
|
)
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// writeResolvConf writes DNS configuration in resolv.conf format to the given writer.
|
2021-04-09 22:24:47 +00:00
|
|
|
func writeResolvConf(w io.Writer, servers []netaddr.IP, domains []dnsname.FQDN) {
|
2020-07-14 13:12:00 +00:00
|
|
|
io.WriteString(w, "# resolv.conf(5) file generated by tailscale\n")
|
|
|
|
io.WriteString(w, "# DO NOT EDIT THIS FILE BY HAND -- CHANGES WILL BE OVERWRITTEN\n\n")
|
|
|
|
for _, ns := range servers {
|
|
|
|
io.WriteString(w, "nameserver ")
|
|
|
|
io.WriteString(w, ns.String())
|
|
|
|
io.WriteString(w, "\n")
|
|
|
|
}
|
|
|
|
if len(domains) > 0 {
|
|
|
|
io.WriteString(w, "search")
|
|
|
|
for _, domain := range domains {
|
|
|
|
io.WriteString(w, " ")
|
2021-04-09 22:24:47 +00:00
|
|
|
io.WriteString(w, domain.WithoutTrailingDot())
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
|
|
|
io.WriteString(w, "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 02:37:11 +00:00
|
|
|
func readResolv(r io.Reader) (config OSConfig, err error) {
|
|
|
|
scanner := bufio.NewScanner(r)
|
2020-07-14 13:12:00 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "nameserver") {
|
|
|
|
nameserver := strings.TrimPrefix(line, "nameserver")
|
|
|
|
nameserver = strings.TrimSpace(nameserver)
|
|
|
|
ip, err := netaddr.ParseIP(nameserver)
|
|
|
|
if err != nil {
|
2021-04-11 02:37:11 +00:00
|
|
|
return OSConfig{}, err
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
|
|
|
config.Nameservers = append(config.Nameservers, ip)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "search") {
|
|
|
|
domain := strings.TrimPrefix(line, "search")
|
|
|
|
domain = strings.TrimSpace(domain)
|
2021-04-09 22:24:47 +00:00
|
|
|
fqdn, err := dnsname.ToFQDN(domain)
|
|
|
|
if err != nil {
|
|
|
|
return OSConfig{}, fmt.Errorf("parsing search domains %q: %w", line, err)
|
|
|
|
}
|
|
|
|
config.SearchDomains = append(config.SearchDomains, fqdn)
|
2020-07-14 13:12:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2021-04-11 02:37:11 +00:00
|
|
|
func readResolvFile(path string) (OSConfig, error) {
|
|
|
|
var config OSConfig
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return readResolv(f)
|
|
|
|
}
|
|
|
|
|
2021-04-09 09:52:21 +00:00
|
|
|
// readResolvConf reads DNS configuration from /etc/resolv.conf.
|
|
|
|
func readResolvConf() (OSConfig, error) {
|
|
|
|
return readResolvFile(resolvConf)
|
|
|
|
}
|
|
|
|
|
2020-07-30 00:57:25 +00:00
|
|
|
// isResolvedRunning reports whether systemd-resolved is running on the system,
|
|
|
|
// even if it is not managing the system DNS settings.
|
|
|
|
func isResolvedRunning() bool {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// systemd-resolved is never installed without systemd.
|
|
|
|
_, err := exec.LookPath("systemctl")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// is-active exits with code 3 if the service is not active.
|
|
|
|
err = exec.Command("systemctl", "is-active", "systemd-resolved.service").Run()
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// directManager is a managerImpl which replaces /etc/resolv.conf with a file
|
|
|
|
// generated from the given configuration, creating a backup of its old state.
|
2020-07-14 13:12:00 +00:00
|
|
|
//
|
|
|
|
// This way of configuring DNS is precarious, since it does not react
|
|
|
|
// to the disappearance of the Tailscale interface.
|
2020-07-31 20:27:09 +00:00
|
|
|
// The caller must call Down before program shutdown
|
|
|
|
// or as cleanup if the program terminates unexpectedly.
|
|
|
|
type directManager struct{}
|
|
|
|
|
2021-04-02 09:17:50 +00:00
|
|
|
func newDirectManager() directManager {
|
2020-07-31 20:27:09 +00:00
|
|
|
return directManager{}
|
|
|
|
}
|
|
|
|
|
2021-04-09 09:52:21 +00:00
|
|
|
// ownedByTailscale reports whether /etc/resolv.conf seems to be a
|
|
|
|
// tailscale-managed file.
|
|
|
|
func (m directManager) ownedByTailscale() (bool, error) {
|
|
|
|
st, err := os.Stat(resolvConf)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
2021-04-09 09:52:21 +00:00
|
|
|
if !st.Mode().IsRegular() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
bs, err := ioutil.ReadFile(resolvConf)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if bytes.Contains(bs, []byte("generated by tailscale")) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-07-14 13:12:00 +00:00
|
|
|
|
2021-04-09 09:52:21 +00:00
|
|
|
// backupConfig creates or updates a backup of /etc/resolv.conf, if
|
|
|
|
// resolv.conf does not currently contain a Tailscale-managed config.
|
|
|
|
func (m directManager) backupConfig() error {
|
|
|
|
if _, err := os.Stat(resolvConf); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// No resolv.conf, nothing to back up. Also get rid of any
|
|
|
|
// existing backup file, to avoid restoring something old.
|
|
|
|
os.Remove(backupConf)
|
|
|
|
return nil
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
2021-04-09 09:52:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
owned, err := m.ownedByTailscale()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if owned {
|
2020-07-14 13:12:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 09:52:21 +00:00
|
|
|
return os.Rename(resolvConf, backupConf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m directManager) SetDNS(config OSConfig) error {
|
|
|
|
if err := m.backupConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
writeResolvConf(buf, config.Nameservers, config.SearchDomains)
|
|
|
|
if err := atomicfile.WriteFile(resolvConf, buf.Bytes(), 0644); err != nil {
|
2020-07-14 13:12:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-09 09:52:21 +00:00
|
|
|
// We might have taken over a configuration managed by resolved,
|
|
|
|
// in which case it will notice this on restart and gracefully
|
|
|
|
// start using our configuration. This shouldn't happen because we
|
|
|
|
// try to manage DNS through resolved when it's around, but as a
|
|
|
|
// best-effort fallback if we messed up the detection, try to
|
|
|
|
// restart resolved to make the system configuration consistent.
|
2020-07-30 00:57:25 +00:00
|
|
|
if isResolvedRunning() {
|
2021-04-09 09:52:21 +00:00
|
|
|
exec.Command("systemctl", "restart", "systemd-resolved.service").Run()
|
2020-07-30 00:57:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 13:12:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:05:47 +00:00
|
|
|
func (m directManager) SupportsSplitDNS() bool {
|
|
|
|
return false
|
2021-04-02 09:17:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 07:31:31 +00:00
|
|
|
func (m directManager) GetBaseConfig() (OSConfig, error) {
|
2021-04-09 09:52:21 +00:00
|
|
|
owned, err := m.ownedByTailscale()
|
|
|
|
if err != nil {
|
|
|
|
return OSConfig{}, err
|
|
|
|
}
|
|
|
|
fileToRead := resolvConf
|
|
|
|
if owned {
|
|
|
|
fileToRead = backupConf
|
|
|
|
}
|
|
|
|
|
|
|
|
return readResolvFile(fileToRead)
|
2021-04-07 07:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 09:17:50 +00:00
|
|
|
func (m directManager) Close() error {
|
2021-04-09 09:52:21 +00:00
|
|
|
// We used to keep a file for the tailscale config and symlinked
|
|
|
|
// to it, but then we stopped because /etc/resolv.conf being a
|
|
|
|
// symlink to surprising places breaks snaps and other sandboxing
|
|
|
|
// things. Clean it up if it's still there.
|
|
|
|
os.Remove("/etc/resolv.tailscale.conf")
|
|
|
|
|
2020-07-14 13:12:00 +00:00
|
|
|
if _, err := os.Stat(backupConf); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2021-04-09 09:52:21 +00:00
|
|
|
// No backup, nothing we can do.
|
2020-07-14 13:12:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2021-04-09 09:52:21 +00:00
|
|
|
owned, err := m.ownedByTailscale()
|
|
|
|
if err != nil {
|
2020-07-14 13:12:00 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-09 09:52:21 +00:00
|
|
|
_, err = os.Stat(resolvConf)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resolvConfExists := !os.IsNotExist(err)
|
|
|
|
|
|
|
|
if resolvConfExists && !owned {
|
|
|
|
// There's already a non-tailscale config in place, get rid of
|
|
|
|
// our backup.
|
|
|
|
os.Remove(backupConf)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We own resolv.conf, and a backup exists.
|
2020-07-14 13:12:00 +00:00
|
|
|
if err := os.Rename(backupConf, resolvConf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-30 00:57:25 +00:00
|
|
|
|
|
|
|
if isResolvedRunning() {
|
|
|
|
exec.Command("systemctl", "restart", "systemd-resolved.service").Run() // Best-effort.
|
|
|
|
}
|
|
|
|
|
2020-07-14 13:12:00 +00:00
|
|
|
return nil
|
|
|
|
}
|