2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-07-14 13:12:00 +00:00
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
package dns
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-12-10 16:47:33 +00:00
|
|
|
"context"
|
2021-06-25 16:43:13 +00:00
|
|
|
"crypto/rand"
|
2022-02-23 17:25:14 +00:00
|
|
|
"errors"
|
2021-04-09 22:24:47 +00:00
|
|
|
"fmt"
|
2020-07-14 13:12:00 +00:00
|
|
|
"io"
|
2022-02-23 17:25:14 +00:00
|
|
|
"io/fs"
|
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
|
|
|
"net/netip"
|
2020-07-14 13:12:00 +00:00
|
|
|
"os"
|
2020-07-30 00:57:25 +00:00
|
|
|
"os/exec"
|
2021-06-25 23:36:36 +00:00
|
|
|
"path/filepath"
|
2020-07-30 00:57:25 +00:00
|
|
|
"runtime"
|
2024-02-16 18:09:37 +00:00
|
|
|
"slices"
|
2020-07-14 13:12:00 +00:00
|
|
|
"strings"
|
2022-11-13 05:54:10 +00:00
|
|
|
"sync"
|
2021-12-10 16:47:33 +00:00
|
|
|
"time"
|
2020-07-14 13:12:00 +00:00
|
|
|
|
2024-04-26 17:12:46 +00:00
|
|
|
"tailscale.com/health"
|
2022-02-15 04:17:51 +00:00
|
|
|
"tailscale.com/net/dns/resolvconffile"
|
2024-02-16 18:09:37 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2021-10-07 00:49:32 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-04-09 22:24:47 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2022-02-23 17:25:14 +00:00
|
|
|
"tailscale.com/version/distro"
|
2020-07-14 13:12:00 +00:00
|
|
|
)
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// writeResolvConf writes DNS configuration in resolv.conf format to the given writer.
|
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
|
|
|
func writeResolvConf(w io.Writer, servers []netip.Addr, domains []dnsname.FQDN) error {
|
2022-02-15 04:17:51 +00:00
|
|
|
c := &resolvconffile.Config{
|
|
|
|
Nameservers: servers,
|
|
|
|
SearchDomains: domains,
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
2022-02-15 04:17:51 +00:00
|
|
|
return c.Write(w)
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 04:17:51 +00:00
|
|
|
func readResolv(r io.Reader) (OSConfig, error) {
|
|
|
|
c, err := resolvconffile.Parse(r)
|
|
|
|
if err != nil {
|
|
|
|
return OSConfig{}, err
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
2022-02-15 04:17:51 +00:00
|
|
|
return OSConfig{
|
|
|
|
Nameservers: c.Nameservers,
|
|
|
|
SearchDomains: c.SearchDomains,
|
|
|
|
}, nil
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 22:34:59 +00:00
|
|
|
// resolvOwner returns the apparent owner of the resolv.conf
|
|
|
|
// configuration in bs - one of "resolvconf", "systemd-resolved" or
|
|
|
|
// "NetworkManager", or "" if no known owner was found.
|
2023-12-22 01:40:03 +00:00
|
|
|
//
|
|
|
|
//lint:ignore U1000 used in linux and freebsd code
|
2021-04-14 22:34:59 +00:00
|
|
|
func resolvOwner(bs []byte) string {
|
2021-10-08 13:51:48 +00:00
|
|
|
likely := ""
|
2021-04-14 22:34:59 +00:00
|
|
|
b := bytes.NewBuffer(bs)
|
|
|
|
for {
|
|
|
|
line, err := b.ReadString('\n')
|
|
|
|
if err != nil {
|
2021-10-08 13:51:48 +00:00
|
|
|
return likely
|
2021-04-14 22:34:59 +00:00
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if line[0] != '#' {
|
|
|
|
// First non-empty, non-comment line. Assume the owner
|
|
|
|
// isn't hiding further down.
|
2021-10-08 13:51:48 +00:00
|
|
|
return likely
|
2021-04-14 22:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(line, "systemd-resolved") {
|
2021-10-08 13:51:48 +00:00
|
|
|
likely = "systemd-resolved"
|
2021-04-14 22:34:59 +00:00
|
|
|
} else if strings.Contains(line, "NetworkManager") {
|
2021-10-08 13:51:48 +00:00
|
|
|
likely = "NetworkManager"
|
2021-04-14 22:34:59 +00:00
|
|
|
} else if strings.Contains(line, "resolvconf") {
|
2021-10-08 13:51:48 +00:00
|
|
|
likely = "resolvconf"
|
2021-04-14 22:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-10 16:47:33 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err = exec.CommandContext(ctx, "systemctl", "is-active", "systemd-resolved.service").Run()
|
2020-07-30 00:57:25 +00:00
|
|
|
|
2021-12-10 16:47:33 +00:00
|
|
|
// is-active exits with code 3 if the service is not active.
|
2020-07-30 00:57:25 +00:00
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 16:47:33 +00:00
|
|
|
func restartResolved() error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
return exec.CommandContext(ctx, "systemctl", "restart", "systemd-resolved.service").Run()
|
|
|
|
}
|
|
|
|
|
2021-06-25 14:02:10 +00:00
|
|
|
// directManager is an OSConfigurator which replaces /etc/resolv.conf with a file
|
2020-07-31 20:27:09 +00:00
|
|
|
// 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.
|
2021-06-25 16:43:13 +00:00
|
|
|
type directManager struct {
|
2024-04-26 17:12:46 +00:00
|
|
|
logf logger.Logf
|
|
|
|
health *health.Tracker
|
|
|
|
fs wholeFileFS
|
2021-10-07 00:49:32 +00:00
|
|
|
// renameBroken is set if fs.Rename to or from /etc/resolv.conf
|
|
|
|
// fails. This can happen in some container runtimes, where
|
|
|
|
// /etc/resolv.conf is bind-mounted from outside the container,
|
|
|
|
// and therefore /etc and /etc/resolv.conf are different
|
|
|
|
// filesystems as far as rename(2) is concerned.
|
|
|
|
//
|
|
|
|
// In those situations, we fall back to emulating rename with file
|
|
|
|
// copies and truncations, which is not as good (opens up a race
|
|
|
|
// where a reader can see an empty or partial /etc/resolv.conf),
|
|
|
|
// but is better than having non-functioning DNS.
|
|
|
|
renameBroken bool
|
2022-11-13 05:54:10 +00:00
|
|
|
|
|
|
|
ctx context.Context // valid until Close
|
|
|
|
ctxClose context.CancelFunc // closes ctx
|
|
|
|
|
2023-12-22 01:40:03 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
wantResolvConf []byte // if non-nil, what we expect /etc/resolv.conf to contain
|
|
|
|
//lint:ignore U1000 used in direct_linux.go
|
2022-11-13 05:54:10 +00:00
|
|
|
lastWarnContents []byte // last resolv.conf contents that we warned about
|
2021-06-25 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 01:40:03 +00:00
|
|
|
//lint:ignore U1000 used in manager_{freebsd,openbsd}.go
|
2024-04-26 17:12:46 +00:00
|
|
|
func newDirectManager(logf logger.Logf, health *health.Tracker) *directManager {
|
|
|
|
return newDirectManagerOnFS(logf, health, directFS{})
|
2021-06-25 16:43:13 +00:00
|
|
|
}
|
2020-07-31 20:27:09 +00:00
|
|
|
|
2024-04-26 17:12:46 +00:00
|
|
|
func newDirectManagerOnFS(logf logger.Logf, health *health.Tracker, fs wholeFileFS) *directManager {
|
2022-11-13 05:54:10 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
m := &directManager{
|
|
|
|
logf: logf,
|
2024-04-26 17:12:46 +00:00
|
|
|
health: health,
|
2022-11-13 05:54:10 +00:00
|
|
|
fs: fs,
|
|
|
|
ctx: ctx,
|
|
|
|
ctxClose: cancel,
|
2021-10-07 00:49:32 +00:00
|
|
|
}
|
2022-11-13 05:54:10 +00:00
|
|
|
go m.runFileWatcher()
|
|
|
|
return m
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 00:49:32 +00:00
|
|
|
func (m *directManager) readResolvFile(path string) (OSConfig, error) {
|
2021-10-07 00:00:47 +00:00
|
|
|
b, err := m.fs.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return OSConfig{}, err
|
|
|
|
}
|
|
|
|
return readResolv(bytes.NewReader(b))
|
|
|
|
}
|
|
|
|
|
2021-04-09 09:52:21 +00:00
|
|
|
// ownedByTailscale reports whether /etc/resolv.conf seems to be a
|
|
|
|
// tailscale-managed file.
|
2021-10-07 00:49:32 +00:00
|
|
|
func (m *directManager) ownedByTailscale() (bool, error) {
|
2021-06-25 16:43:13 +00:00
|
|
|
isRegular, err := m.fs.Stat(resolvConf)
|
2021-04-09 09:52:21 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
2021-06-25 16:43:13 +00:00
|
|
|
if !isRegular {
|
2021-04-09 09:52:21 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2021-06-25 16:43:13 +00:00
|
|
|
bs, err := m.fs.ReadFile(resolvConf)
|
2021-04-09 09:52:21 +00:00
|
|
|
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.
|
2021-10-07 00:49:32 +00:00
|
|
|
func (m *directManager) backupConfig() error {
|
2021-06-25 16:43:13 +00:00
|
|
|
if _, err := m.fs.Stat(resolvConf); err != nil {
|
2021-04-09 09:52:21 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// No resolv.conf, nothing to back up. Also get rid of any
|
|
|
|
// existing backup file, to avoid restoring something old.
|
2021-06-25 16:43:13 +00:00
|
|
|
m.fs.Remove(backupConf)
|
2021-04-09 09:52:21 +00:00
|
|
|
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-10-07 00:49:32 +00:00
|
|
|
return m.rename(resolvConf, backupConf)
|
2021-04-09 09:52:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 00:49:32 +00:00
|
|
|
func (m *directManager) restoreBackup() (restored bool, err error) {
|
2021-06-25 16:43:13 +00:00
|
|
|
if _, err := m.fs.Stat(backupConf); err != nil {
|
2021-04-20 22:14:11 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// No backup, nothing we can do.
|
2021-09-05 06:46:15 +00:00
|
|
|
return false, nil
|
2021-04-20 22:14:11 +00:00
|
|
|
}
|
2021-09-05 06:46:15 +00:00
|
|
|
return false, err
|
2021-04-20 22:14:11 +00:00
|
|
|
}
|
|
|
|
owned, err := m.ownedByTailscale()
|
|
|
|
if err != nil {
|
2021-09-05 06:46:15 +00:00
|
|
|
return false, err
|
2021-04-20 22:14:11 +00:00
|
|
|
}
|
2021-06-25 16:51:49 +00:00
|
|
|
_, err = m.fs.Stat(resolvConf)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2021-09-05 06:46:15 +00:00
|
|
|
return false, err
|
2021-04-09 09:52:21 +00:00
|
|
|
}
|
2021-04-20 22:14:11 +00:00
|
|
|
resolvConfExists := !os.IsNotExist(err)
|
2021-04-09 09:52:21 +00:00
|
|
|
|
2021-04-20 22:14:11 +00:00
|
|
|
if resolvConfExists && !owned {
|
|
|
|
// There's already a non-tailscale config in place, get rid of
|
|
|
|
// our backup.
|
2021-06-25 16:43:13 +00:00
|
|
|
m.fs.Remove(backupConf)
|
2021-09-05 06:46:15 +00:00
|
|
|
return false, nil
|
2021-04-20 22:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We own resolv.conf, and a backup exists.
|
2021-10-07 00:49:32 +00:00
|
|
|
if err := m.rename(backupConf, resolvConf); err != nil {
|
2021-09-05 06:46:15 +00:00
|
|
|
return false, err
|
2020-07-14 13:12:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 06:46:15 +00:00
|
|
|
return true, nil
|
2021-04-20 22:14:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 00:49:32 +00:00
|
|
|
// rename tries to rename old to new using m.fs.Rename, and falls back
|
|
|
|
// to hand-copying bytes and truncating old if that fails.
|
|
|
|
//
|
|
|
|
// This is a workaround to /etc/resolv.conf being a bind-mounted file
|
|
|
|
// some container environments, which cannot be moved elsewhere in
|
|
|
|
// /etc (because that would be a cross-filesystem move) or deleted
|
|
|
|
// (because that would break the bind in surprising ways).
|
|
|
|
func (m *directManager) rename(old, new string) error {
|
|
|
|
if !m.renameBroken {
|
|
|
|
err := m.fs.Rename(old, new)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-23 17:25:14 +00:00
|
|
|
if runtime.GOOS == "linux" && distro.Get() == distro.Synology {
|
|
|
|
// Fail fast. The fallback case below won't work anyway.
|
|
|
|
return err
|
|
|
|
}
|
2021-10-07 00:49:32 +00:00
|
|
|
m.logf("rename of %q to %q failed (%v), falling back to copy+delete", old, new, err)
|
|
|
|
m.renameBroken = true
|
|
|
|
}
|
|
|
|
|
|
|
|
bs, err := m.fs.ReadFile(old)
|
|
|
|
if err != nil {
|
2022-02-23 17:25:14 +00:00
|
|
|
return fmt.Errorf("reading %q to rename: %w", old, err)
|
2021-10-07 00:49:32 +00:00
|
|
|
}
|
|
|
|
if err := m.fs.WriteFile(new, bs, 0644); err != nil {
|
2022-02-23 17:25:14 +00:00
|
|
|
return fmt.Errorf("writing to %q in rename of %q: %w", new, old, err)
|
2021-10-07 00:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.fs.Remove(old); err != nil {
|
|
|
|
err2 := m.fs.Truncate(old)
|
|
|
|
if err2 != nil {
|
2022-02-23 17:25:14 +00:00
|
|
|
return fmt.Errorf("remove of %q failed (%w) and so did truncate: %v", old, err, err2)
|
2021-10-07 00:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 05:54:10 +00:00
|
|
|
// setWant sets the expected contents of /etc/resolv.conf, if any.
|
|
|
|
//
|
|
|
|
// A value of nil means no particular value is expected.
|
|
|
|
//
|
|
|
|
// m takes ownership of want.
|
|
|
|
func (m *directManager) setWant(want []byte) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.wantResolvConf = want
|
|
|
|
}
|
|
|
|
|
2021-10-07 00:49:32 +00:00
|
|
|
func (m *directManager) SetDNS(config OSConfig) (err error) {
|
2022-02-23 17:25:14 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil && errors.Is(err, fs.ErrPermission) && runtime.GOOS == "linux" &&
|
|
|
|
distro.Get() == distro.Synology && os.Geteuid() != 0 {
|
|
|
|
// On Synology (notably DSM7 where we don't run as root), ignore all
|
|
|
|
// DNS configuration errors for now. We don't have permission.
|
|
|
|
// See https://github.com/tailscale/tailscale/issues/4017
|
|
|
|
m.logf("ignoring SetDNS permission error on Synology (Issue 4017); was: %v", err)
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}()
|
2022-11-13 05:54:10 +00:00
|
|
|
m.setWant(nil) // reset our expectations before any work
|
2021-09-05 06:46:15 +00:00
|
|
|
var changed bool
|
2021-04-20 22:51:14 +00:00
|
|
|
if config.IsZero() {
|
2021-09-05 06:46:15 +00:00
|
|
|
changed, err = m.restoreBackup()
|
|
|
|
if err != nil {
|
2021-04-20 22:14:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-05 06:46:15 +00:00
|
|
|
changed = true
|
2021-04-20 22:14:11 +00:00
|
|
|
if err := m.backupConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
writeResolvConf(buf, config.Nameservers, config.SearchDomains)
|
2021-10-07 00:49:32 +00:00
|
|
|
if err := m.atomicWriteFile(m.fs, resolvConf, buf.Bytes(), 0644); err != nil {
|
2021-04-20 22:14:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-13 05:54:10 +00:00
|
|
|
|
|
|
|
// Now that we've successfully written to the file, lock it in.
|
|
|
|
// If we see /etc/resolv.conf with different contents, we know somebody
|
|
|
|
// else trampled on it.
|
|
|
|
m.setWant(buf.Bytes())
|
2021-04-20 22:14:11 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2021-09-05 06:46:15 +00:00
|
|
|
//
|
|
|
|
// We take care to only kick systemd-resolved if we've made some
|
|
|
|
// change to the system's DNS configuration, because this codepath
|
|
|
|
// can end up running in cases where the user has manually
|
|
|
|
// configured /etc/resolv.conf to point to systemd-resolved (but
|
|
|
|
// it's not managed explicitly by systemd-resolved), *and* has
|
|
|
|
// --accept-dns=false, meaning we pass an empty configuration to
|
|
|
|
// the running DNS manager. In that very edge-case scenario, we
|
|
|
|
// cause a disruptive DNS outage each time we reset an empty
|
|
|
|
// OS configuration.
|
|
|
|
if changed && isResolvedRunning() && !runningAsGUIDesktopUser() {
|
2021-12-28 17:58:57 +00:00
|
|
|
t0 := time.Now()
|
|
|
|
err := restartResolved()
|
|
|
|
d := time.Since(t0).Round(time.Millisecond)
|
|
|
|
if err != nil {
|
|
|
|
m.logf("error restarting resolved after %v: %v", d, err)
|
|
|
|
} else {
|
|
|
|
m.logf("restarted resolved after %v", d)
|
|
|
|
}
|
2020-07-30 00:57:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 13:12:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-07 00:49:32 +00:00
|
|
|
func (m *directManager) SupportsSplitDNS() bool {
|
2021-04-05 20:05:47 +00:00
|
|
|
return false
|
2021-04-02 09:17:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 00:49:32 +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
|
|
|
|
}
|
|
|
|
|
2024-02-16 18:09:37 +00:00
|
|
|
oscfg, err := m.readResolvFile(fileToRead)
|
|
|
|
if err != nil {
|
|
|
|
return OSConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// On some systems, the backup configuration file is actually a
|
|
|
|
// symbolic link to something owned by another DNS service (commonly,
|
|
|
|
// resolved). Thus, it can be updated out from underneath us to contain
|
|
|
|
// the Tailscale service IP, which results in an infinite loop of us
|
|
|
|
// trying to send traffic to resolved, which sends back to us, and so
|
|
|
|
// on. To solve this, drop the Tailscale service IP from the base
|
|
|
|
// configuration; we do this in all situations since there's
|
|
|
|
// essentially no world where we want to forward to ourselves.
|
|
|
|
//
|
|
|
|
// See: https://github.com/tailscale/tailscale/issues/7816
|
|
|
|
var removed bool
|
|
|
|
oscfg.Nameservers = slices.DeleteFunc(oscfg.Nameservers, func(ip netip.Addr) bool {
|
|
|
|
if ip == tsaddr.TailscaleServiceIP() || ip == tsaddr.TailscaleServiceIPv6() {
|
|
|
|
removed = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if removed {
|
|
|
|
m.logf("[v1] dropped Tailscale IP from base config that was a symlink")
|
|
|
|
}
|
|
|
|
return oscfg, nil
|
2021-04-07 07:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 00:49:32 +00:00
|
|
|
func (m *directManager) Close() error {
|
2024-03-29 09:57:20 +00:00
|
|
|
m.ctxClose()
|
|
|
|
|
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.
|
2021-06-25 16:43:13 +00:00
|
|
|
m.fs.Remove("/etc/resolv.tailscale.conf")
|
2021-04-09 09:52:21 +00:00
|
|
|
|
2021-06-25 16:43:13 +00:00
|
|
|
if _, err := m.fs.Stat(backupConf); err != nil {
|
2020-07-14 13:12:00 +00:00
|
|
|
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-06-25 16:43:13 +00:00
|
|
|
_, err = m.fs.Stat(resolvConf)
|
2021-04-09 09:52:21 +00:00
|
|
|
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.
|
2021-06-25 16:43:13 +00:00
|
|
|
m.fs.Remove(backupConf)
|
2021-04-09 09:52:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We own resolv.conf, and a backup exists.
|
2021-10-07 00:49:32 +00:00
|
|
|
if err := m.rename(backupConf, resolvConf); err != nil {
|
2020-07-14 13:12:00 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-30 00:57:25 +00:00
|
|
|
|
2021-08-19 15:58:47 +00:00
|
|
|
if isResolvedRunning() && !runningAsGUIDesktopUser() {
|
2021-12-10 16:47:33 +00:00
|
|
|
m.logf("restarting systemd-resolved...")
|
|
|
|
if err := restartResolved(); err != nil {
|
|
|
|
m.logf("restart of systemd-resolved failed: %v", err)
|
|
|
|
} else {
|
|
|
|
m.logf("restarted systemd-resolved")
|
|
|
|
}
|
2020-07-30 00:57:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 13:12:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-06-25 16:43:13 +00:00
|
|
|
|
2021-10-07 00:49:32 +00:00
|
|
|
func (m *directManager) atomicWriteFile(fs wholeFileFS, filename string, data []byte, perm os.FileMode) error {
|
2021-06-25 16:43:13 +00:00
|
|
|
var randBytes [12]byte
|
|
|
|
if _, err := rand.Read(randBytes[:]); err != nil {
|
|
|
|
return fmt.Errorf("atomicWriteFile: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpName := fmt.Sprintf("%s.%x.tmp", filename, randBytes[:])
|
|
|
|
defer fs.Remove(tmpName)
|
|
|
|
|
|
|
|
if err := fs.WriteFile(tmpName, data, perm); err != nil {
|
|
|
|
return fmt.Errorf("atomicWriteFile: %w", err)
|
|
|
|
}
|
2021-10-07 00:49:32 +00:00
|
|
|
return m.rename(tmpName, filename)
|
2021-06-25 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 23:36:36 +00:00
|
|
|
// wholeFileFS is a high-level file system abstraction designed just for use
|
2021-06-25 16:43:13 +00:00
|
|
|
// by directManager, with the goal that it is easy to implement over wsl.exe.
|
2021-06-25 23:36:36 +00:00
|
|
|
//
|
|
|
|
// All name parameters are absolute paths.
|
|
|
|
type wholeFileFS interface {
|
2021-06-25 16:43:13 +00:00
|
|
|
Stat(name string) (isRegular bool, err error)
|
|
|
|
Rename(oldName, newName string) error
|
|
|
|
Remove(name string) error
|
|
|
|
ReadFile(name string) ([]byte, error)
|
2021-10-07 00:49:32 +00:00
|
|
|
Truncate(name string) error
|
2021-06-25 16:43:13 +00:00
|
|
|
WriteFile(name string, contents []byte, perm os.FileMode) error
|
|
|
|
}
|
|
|
|
|
2021-06-25 23:36:36 +00:00
|
|
|
// directFS is a wholeFileFS implemented directly on the OS.
|
2021-06-25 16:43:13 +00:00
|
|
|
type directFS struct {
|
2021-06-25 23:36:36 +00:00
|
|
|
// prefix is file path prefix.
|
|
|
|
//
|
|
|
|
// All name parameters are absolute paths so this is typically a
|
|
|
|
// testing temporary directory like "/tmp".
|
|
|
|
prefix string
|
2021-06-25 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 23:36:36 +00:00
|
|
|
func (fs directFS) path(name string) string { return filepath.Join(fs.prefix, name) }
|
|
|
|
|
2021-06-25 16:43:13 +00:00
|
|
|
func (fs directFS) Stat(name string) (isRegular bool, err error) {
|
2021-06-25 23:36:36 +00:00
|
|
|
fi, err := os.Stat(fs.path(name))
|
2021-06-25 16:43:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return fi.Mode().IsRegular(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs directFS) Rename(oldName, newName string) error {
|
2021-06-25 23:36:36 +00:00
|
|
|
return os.Rename(fs.path(oldName), fs.path(newName))
|
2021-06-25 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 23:36:36 +00:00
|
|
|
func (fs directFS) Remove(name string) error { return os.Remove(fs.path(name)) }
|
2021-06-25 16:43:13 +00:00
|
|
|
|
|
|
|
func (fs directFS) ReadFile(name string) ([]byte, error) {
|
2022-09-15 12:06:59 +00:00
|
|
|
return os.ReadFile(fs.path(name))
|
2021-06-25 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 00:49:32 +00:00
|
|
|
func (fs directFS) Truncate(name string) error {
|
|
|
|
return os.Truncate(fs.path(name), 0)
|
|
|
|
}
|
|
|
|
|
2021-06-25 16:43:13 +00:00
|
|
|
func (fs directFS) WriteFile(name string, contents []byte, perm os.FileMode) error {
|
2022-09-15 12:06:59 +00:00
|
|
|
return os.WriteFile(fs.path(name), contents, perm)
|
2021-06-25 16:43:13 +00:00
|
|
|
}
|
2021-08-19 15:58:47 +00:00
|
|
|
|
|
|
|
// runningAsGUIDesktopUser reports whether it seems that this code is
|
|
|
|
// being run as a regular user on a Linux desktop. This is a quick
|
|
|
|
// hack to fix Issue 2672 where PolicyKit pops up a GUI dialog asking
|
|
|
|
// to proceed we do a best effort attempt to restart
|
|
|
|
// systemd-resolved.service. There's surely a better way.
|
|
|
|
func runningAsGUIDesktopUser() bool {
|
|
|
|
return os.Getuid() != 0 && os.Getenv("DISPLAY") != ""
|
|
|
|
}
|