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.
|
|
|
|
|
|
|
|
// +build linux freebsd openbsd
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
package dns
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2020-11-02 16:33:34 +00:00
|
|
|
"errors"
|
2020-07-14 13:12:00 +00:00
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tsConf = "/etc/resolv.tailscale.conf"
|
|
|
|
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.
|
|
|
|
func writeResolvConf(w io.Writer, servers []netaddr.IP, domains []string) {
|
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, " ")
|
|
|
|
io.WriteString(w, domain)
|
|
|
|
}
|
|
|
|
io.WriteString(w, "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 20:27:09 +00:00
|
|
|
// readResolvConf reads DNS configuration from /etc/resolv.conf.
|
2021-04-02 06:42:58 +00:00
|
|
|
func readResolvConf() (OSConfig, error) {
|
|
|
|
var config OSConfig
|
2020-07-14 13:12:00 +00:00
|
|
|
|
|
|
|
f, err := os.Open("/etc/resolv.conf")
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
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 {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
config.Nameservers = append(config.Nameservers, ip)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "search") {
|
|
|
|
domain := strings.TrimPrefix(line, "search")
|
|
|
|
domain = strings.TrimSpace(domain)
|
|
|
|
config.Domains = append(config.Domains, domain)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
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-03 03:15:54 +00:00
|
|
|
func (m directManager) SetDNS(config OSConfig) error {
|
2020-07-14 13:12:00 +00:00
|
|
|
// Write the tsConf file.
|
|
|
|
buf := new(bytes.Buffer)
|
2020-07-31 20:27:09 +00:00
|
|
|
writeResolvConf(buf, config.Nameservers, config.Domains)
|
2020-07-14 13:12:00 +00:00
|
|
|
if err := atomicfile.WriteFile(tsConf, buf.Bytes(), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if linkPath, err := os.Readlink(resolvConf); err != nil {
|
|
|
|
// Remove any old backup that may exist.
|
|
|
|
os.Remove(backupConf)
|
|
|
|
|
|
|
|
// Backup the existing /etc/resolv.conf file.
|
|
|
|
contents, err := ioutil.ReadFile(resolvConf)
|
|
|
|
// If the original did not exist, still back up an empty file.
|
|
|
|
// The presence of a backup file is the way we know that Up ran.
|
2020-11-02 16:33:34 +00:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
2020-07-14 13:12:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := atomicfile.WriteFile(backupConf, contents, 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if linkPath != tsConf {
|
|
|
|
// Backup the existing symlink.
|
|
|
|
os.Remove(backupConf)
|
|
|
|
if err := os.Symlink(linkPath, backupConf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Nothing to do, resolvConf already points to tsConf.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove(resolvConf)
|
|
|
|
if err := os.Symlink(tsConf, 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
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:05:47 +00:00
|
|
|
func (m directManager) SupportsSplitDNS() bool {
|
|
|
|
return false
|
2021-04-02 09:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m directManager) Close() error {
|
2020-07-14 13:12:00 +00:00
|
|
|
if _, err := os.Stat(backupConf); err != nil {
|
|
|
|
// If the backup file does not exist, then Up never ran successfully.
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ln, err := os.Readlink(resolvConf); err != nil {
|
|
|
|
return err
|
|
|
|
} else if ln != tsConf {
|
|
|
|
return fmt.Errorf("resolv.conf is not a symlink to %s", tsConf)
|
|
|
|
}
|
|
|
|
if err := os.Rename(backupConf, resolvConf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
os.Remove(tsConf)
|
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
|
|
|
|
}
|