2020-07-31 20:27:09 +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.
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
2021-04-14 00:10:30 +00:00
|
|
|
import (
|
2021-09-05 06:40:48 +00:00
|
|
|
"bytes"
|
2021-04-14 00:10:30 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/godbus/dbus/v5"
|
2021-06-15 23:39:21 +00:00
|
|
|
"inet.af/netaddr"
|
2021-04-14 00:10:30 +00:00
|
|
|
"tailscale.com/types/logger"
|
2021-04-24 03:57:35 +00:00
|
|
|
"tailscale.com/util/cmpver"
|
2021-04-14 00:10:30 +00:00
|
|
|
)
|
2021-04-02 06:26:52 +00:00
|
|
|
|
2021-04-14 22:52:41 +00:00
|
|
|
type kv struct {
|
|
|
|
k, v string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kv kv) String() string {
|
|
|
|
return fmt.Sprintf("%s=%s", kv.k, kv.v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOSConfigurator(logf logger.Logf, interfaceName string) (ret OSConfigurator, err error) {
|
2021-09-05 06:40:48 +00:00
|
|
|
env := newOSConfigEnv{
|
|
|
|
fs: directFS{},
|
|
|
|
dbusPing: dbusPing,
|
|
|
|
nmIsUsingResolved: nmIsUsingResolved,
|
|
|
|
nmVersionBetween: nmVersionBetween,
|
|
|
|
resolvconfStyle: resolvconfStyle,
|
|
|
|
}
|
|
|
|
mode, err := dnsMode(logf, env)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch mode {
|
|
|
|
case "direct":
|
2021-10-07 00:49:32 +00:00
|
|
|
return newDirectManagerOnFS(logf, env.fs), nil
|
2021-09-05 06:40:48 +00:00
|
|
|
case "systemd-resolved":
|
|
|
|
return newResolvedManager(logf, interfaceName)
|
|
|
|
case "network-manager":
|
|
|
|
return newNMManager(interfaceName)
|
|
|
|
case "debian-resolvconf":
|
|
|
|
return newDebianResolvconfManager(logf)
|
|
|
|
case "openresolv":
|
|
|
|
return newOpenresolvManager()
|
|
|
|
default:
|
|
|
|
logf("[unexpected] detected unknown DNS mode %q, using direct manager as last resort", mode)
|
2021-10-07 00:49:32 +00:00
|
|
|
return newDirectManagerOnFS(logf, env.fs), nil
|
2021-09-05 06:40:48 +00:00
|
|
|
}
|
2021-08-30 20:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newOSConfigEnv are the funcs newOSConfigurator needs, pulled out for testing.
|
|
|
|
type newOSConfigEnv struct {
|
2021-09-05 06:40:48 +00:00
|
|
|
fs wholeFileFS
|
|
|
|
dbusPing func(string, string) error
|
|
|
|
nmIsUsingResolved func() error
|
|
|
|
nmVersionBetween func(v1, v2 string) (safe bool, err error)
|
|
|
|
resolvconfStyle func() string
|
|
|
|
isResolvconfDebianVersion func() bool
|
2021-08-30 20:48:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 06:40:48 +00:00
|
|
|
func dnsMode(logf logger.Logf, env newOSConfigEnv) (ret string, err error) {
|
2021-04-14 22:52:41 +00:00
|
|
|
var debug []kv
|
|
|
|
dbg := func(k, v string) {
|
|
|
|
debug = append(debug, kv{k, v})
|
|
|
|
}
|
|
|
|
defer func() {
|
2021-09-05 06:40:48 +00:00
|
|
|
if ret != "" {
|
|
|
|
dbg("ret", ret)
|
2021-04-14 22:52:41 +00:00
|
|
|
}
|
|
|
|
logf("dns: %v", debug)
|
|
|
|
}()
|
|
|
|
|
2021-08-30 21:16:12 +00:00
|
|
|
bs, err := env.fs.ReadFile(resolvConf)
|
2021-04-14 00:10:30 +00:00
|
|
|
if os.IsNotExist(err) {
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("rc", "missing")
|
2021-09-05 06:40:48 +00:00
|
|
|
return "direct", nil
|
2021-04-14 00:10:30 +00:00
|
|
|
}
|
2021-04-14 22:35:32 +00:00
|
|
|
if err != nil {
|
2021-09-05 06:40:48 +00:00
|
|
|
return "", fmt.Errorf("reading /etc/resolv.conf: %w", err)
|
2021-04-14 22:35:32 +00:00
|
|
|
}
|
2021-04-14 00:10:30 +00:00
|
|
|
|
2021-09-05 06:40:48 +00:00
|
|
|
switch resolvOwner(bs) {
|
2021-04-14 00:10:30 +00:00
|
|
|
case "systemd-resolved":
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("rc", "resolved")
|
2021-06-15 23:39:21 +00:00
|
|
|
// Some systems, for reasons known only to them, have a
|
|
|
|
// resolv.conf that has the word "systemd-resolved" in its
|
|
|
|
// header, but doesn't actually point to resolved. We mustn't
|
|
|
|
// try to program resolved in that case.
|
|
|
|
// https://github.com/tailscale/tailscale/issues/2136
|
2021-09-05 06:40:48 +00:00
|
|
|
if err := resolvedIsActuallyResolver(bs); err != nil {
|
2021-06-15 23:39:21 +00:00
|
|
|
dbg("resolved", "not-in-use")
|
2021-09-05 06:40:48 +00:00
|
|
|
return "direct", nil
|
2021-06-15 23:39:21 +00:00
|
|
|
}
|
2021-08-30 20:48:35 +00:00
|
|
|
if err := env.dbusPing("org.freedesktop.resolve1", "/org/freedesktop/resolve1"); err != nil {
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("resolved", "no")
|
2021-09-05 06:40:48 +00:00
|
|
|
return "direct", nil
|
2021-04-14 00:10:30 +00:00
|
|
|
}
|
2021-08-30 20:48:35 +00:00
|
|
|
if err := env.dbusPing("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/DnsManager"); err != nil {
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("nm", "no")
|
2021-09-05 06:40:48 +00:00
|
|
|
return "systemd-resolved", nil
|
2021-04-14 00:10:30 +00:00
|
|
|
}
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("nm", "yes")
|
2021-08-30 20:48:35 +00:00
|
|
|
if err := env.nmIsUsingResolved(); err != nil {
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("nm-resolved", "no")
|
2021-09-05 06:40:48 +00:00
|
|
|
return "systemd-resolved", nil
|
2021-04-14 00:10:30 +00:00
|
|
|
}
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("nm-resolved", "yes")
|
2021-04-24 03:57:35 +00:00
|
|
|
|
|
|
|
// Version of NetworkManager before 1.26.6 programmed resolved
|
|
|
|
// incorrectly, such that NM's settings would always take
|
|
|
|
// precedence over other settings set by other resolved
|
|
|
|
// clients.
|
|
|
|
//
|
|
|
|
// If we're dealing with such a version, we have to set our
|
|
|
|
// DNS settings through NM to have them take.
|
|
|
|
//
|
|
|
|
// However, versions 1.26.6 later both fixed the resolved
|
|
|
|
// programming issue _and_ started ignoring DNS settings for
|
|
|
|
// "unmanaged" interfaces - meaning NM 1.26.6 and later
|
|
|
|
// actively ignore DNS configuration we give it. So, for those
|
|
|
|
// NM versions, we can and must use resolved directly.
|
2021-06-10 14:46:08 +00:00
|
|
|
//
|
2021-06-15 22:34:35 +00:00
|
|
|
// Even more fun, even-older versions of NM won't let us set
|
|
|
|
// DNS settings if the interface isn't managed by NM, with a
|
|
|
|
// hard failure on DBus requests. Empirically, NM 1.22 does
|
|
|
|
// this. Based on the versions popular distros shipped, we
|
|
|
|
// conservatively decree that only 1.26.0 through 1.26.5 are
|
|
|
|
// "safe" to use for our purposes. This roughly matches
|
|
|
|
// distros released in the latter half of 2020.
|
|
|
|
//
|
2021-06-10 14:46:08 +00:00
|
|
|
// In a perfect world, we'd avoid this by replacing
|
|
|
|
// configuration out from under NM entirely (e.g. using
|
|
|
|
// directManager to overwrite resolv.conf), but in a world
|
|
|
|
// where resolved runs, we need to get correct configuration
|
|
|
|
// into resolved regardless of what's in resolv.conf (because
|
|
|
|
// resolved can also be queried over dbus, or via an NSS
|
|
|
|
// module that bypasses /etc/resolv.conf). Given that we must
|
|
|
|
// get correct configuration into resolved, we have no choice
|
|
|
|
// but to use NM, and accept the loss of IPv6 configuration
|
|
|
|
// that comes with it (see
|
2021-06-15 22:34:35 +00:00
|
|
|
// https://github.com/tailscale/tailscale/issues/1699,
|
|
|
|
// https://github.com/tailscale/tailscale/pull/1945)
|
2021-09-05 06:40:48 +00:00
|
|
|
safe, err := env.nmVersionBetween("1.26.0", "1.26.5")
|
2021-04-24 03:57:35 +00:00
|
|
|
if err != nil {
|
|
|
|
// Failed to figure out NM's version, can't make a correct
|
|
|
|
// decision.
|
2021-09-05 06:40:48 +00:00
|
|
|
return "", fmt.Errorf("checking NetworkManager version: %v", err)
|
2021-04-24 03:57:35 +00:00
|
|
|
}
|
2021-06-15 22:34:35 +00:00
|
|
|
if safe {
|
|
|
|
dbg("nm-safe", "yes")
|
2021-09-05 06:40:48 +00:00
|
|
|
return "network-manager", nil
|
2021-04-24 03:57:35 +00:00
|
|
|
}
|
2021-06-15 22:34:35 +00:00
|
|
|
dbg("nm-safe", "no")
|
2021-09-05 06:40:48 +00:00
|
|
|
return "systemd-resolved", nil
|
2021-04-14 00:10:30 +00:00
|
|
|
case "resolvconf":
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("rc", "resolvconf")
|
2021-09-05 06:40:48 +00:00
|
|
|
style := env.resolvconfStyle()
|
|
|
|
switch style {
|
|
|
|
case "":
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("resolvconf", "no")
|
2021-09-05 06:40:48 +00:00
|
|
|
return "direct", nil
|
|
|
|
case "debian":
|
|
|
|
dbg("resolvconf", "debian")
|
|
|
|
return "debian-resolvconf", nil
|
|
|
|
case "openresolv":
|
|
|
|
dbg("resolvconf", "openresolv")
|
|
|
|
return "openresolv", nil
|
|
|
|
default:
|
|
|
|
// Shouldn't happen, that means we updated flavors of
|
|
|
|
// resolvconf without updating here.
|
|
|
|
dbg("resolvconf", style)
|
|
|
|
logf("[unexpected] got unknown flavor of resolvconf %q, falling back to direct manager", env.resolvconfStyle())
|
|
|
|
return "direct", nil
|
2021-04-14 00:10:30 +00:00
|
|
|
}
|
|
|
|
case "NetworkManager":
|
2021-06-10 14:46:08 +00:00
|
|
|
// You'd think we would use newNMManager somewhere in
|
|
|
|
// here. However, as explained in
|
|
|
|
// https://github.com/tailscale/tailscale/issues/1699 , using
|
|
|
|
// NetworkManager for DNS configuration carries with it the
|
|
|
|
// cost of losing IPv6 configuration on the Tailscale network
|
|
|
|
// interface. So, when we can avoid it, we bypass
|
|
|
|
// NetworkManager by replacing resolv.conf directly.
|
|
|
|
//
|
|
|
|
// If you ever try to put NMManager back here, keep in mind
|
|
|
|
// that versions >=1.26.6 will ignore DNS configuration
|
|
|
|
// anyway, so you still need a fallback path that uses
|
|
|
|
// directManager.
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("rc", "nm")
|
2021-09-05 06:40:48 +00:00
|
|
|
return "direct", nil
|
2020-07-31 20:27:09 +00:00
|
|
|
default:
|
2021-04-14 22:52:41 +00:00
|
|
|
dbg("rc", "unknown")
|
2021-09-05 06:40:48 +00:00
|
|
|
return "direct", nil
|
2020-07-31 20:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 00:10:30 +00:00
|
|
|
|
2021-06-15 22:34:35 +00:00
|
|
|
func nmVersionBetween(first, last string) (bool, error) {
|
2021-04-24 03:57:35 +00:00
|
|
|
conn, err := dbus.SystemBus()
|
|
|
|
if err != nil {
|
|
|
|
// DBus probably not running.
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nm := conn.Object("org.freedesktop.NetworkManager", dbus.ObjectPath("/org/freedesktop/NetworkManager"))
|
|
|
|
v, err := nm.GetProperty("org.freedesktop.NetworkManager.Version")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
version, ok := v.Value().(string)
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("unexpected type %T for NM version", v.Value())
|
|
|
|
}
|
|
|
|
|
2021-06-16 03:53:03 +00:00
|
|
|
outside := cmpver.Compare(version, first) < 0 || cmpver.Compare(version, last) > 0
|
|
|
|
return !outside, nil
|
2021-04-24 03:57:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 00:10:30 +00:00
|
|
|
func nmIsUsingResolved() error {
|
|
|
|
conn, err := dbus.SystemBus()
|
|
|
|
if err != nil {
|
|
|
|
// DBus probably not running.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nm := conn.Object("org.freedesktop.NetworkManager", dbus.ObjectPath("/org/freedesktop/NetworkManager/DnsManager"))
|
|
|
|
v, err := nm.GetProperty("org.freedesktop.NetworkManager.DnsManager.Mode")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting NM mode: %w", err)
|
|
|
|
}
|
|
|
|
mode, ok := v.Value().(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected type %T for NM DNS mode", v.Value())
|
|
|
|
}
|
|
|
|
if mode != "systemd-resolved" {
|
|
|
|
return errors.New("NetworkManager is not using systemd-resolved for DNS")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-05 06:40:48 +00:00
|
|
|
func resolvedIsActuallyResolver(bs []byte) error {
|
|
|
|
cfg, err := readResolv(bytes.NewBuffer(bs))
|
2021-06-15 23:39:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-05 05:32:28 +00:00
|
|
|
// We've encountered at least one system where the line
|
|
|
|
// "nameserver 127.0.0.53" appears twice, so we look exhaustively
|
|
|
|
// through all of them and allow any number of repeated mentions
|
|
|
|
// of the systemd-resolved stub IP.
|
|
|
|
if len(cfg.Nameservers) == 0 {
|
|
|
|
return errors.New("resolv.conf has no nameservers")
|
|
|
|
}
|
|
|
|
for _, ns := range cfg.Nameservers {
|
|
|
|
if ns != netaddr.IPv4(127, 0, 0, 53) {
|
|
|
|
return errors.New("resolv.conf doesn't point to systemd-resolved")
|
|
|
|
}
|
2021-06-15 23:39:21 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:10:30 +00:00
|
|
|
func dbusPing(name, objectPath string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
conn, err := dbus.SystemBus()
|
|
|
|
if err != nil {
|
|
|
|
// DBus probably not running.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := conn.Object(name, dbus.ObjectPath(objectPath))
|
|
|
|
call := obj.CallWithContext(ctx, "org.freedesktop.DBus.Peer.Ping", 0)
|
|
|
|
return call.Err
|
|
|
|
}
|