2021-04-11 07:31:00 +01:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
2020-07-14 14:12:00 +01:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-07-31 21:27:09 +01:00
|
|
|
package dns
|
2020-07-14 14:12:00 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-04-02 07:26:52 +01:00
|
|
|
|
|
|
|
"tailscale.com/types/logger"
|
2020-07-14 14:12:00 +01:00
|
|
|
)
|
|
|
|
|
2020-07-31 21:27:09 +01:00
|
|
|
// isResolvconfActive indicates whether the system appears to be using resolvconf.
|
|
|
|
// If this is true, then directManager should be avoided:
|
2020-07-14 14:12:00 +01:00
|
|
|
// resolvconf has exclusive ownership of /etc/resolv.conf.
|
2020-07-31 21:27:09 +01:00
|
|
|
func isResolvconfActive() bool {
|
2020-07-14 14:12:00 +01:00
|
|
|
_, err := exec.LookPath("resolvconf")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open("/etc/resolv.conf")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Bytes()
|
|
|
|
// Look for the word "resolvconf" until comments end.
|
|
|
|
if len(line) > 0 && line[0] != '#' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if bytes.Contains(line, []byte("resolvconf")) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-11 07:31:00 +01:00
|
|
|
func newResolvconfManager(logf logger.Logf) OSConfigurator {
|
|
|
|
_, err := exec.Command("resolvconf", "--version").CombinedOutput()
|
2020-07-14 14:12:00 +01:00
|
|
|
if err != nil {
|
2021-04-11 07:31:00 +01:00
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 99 {
|
|
|
|
// Debian resolvconf doesn't understand --version, and
|
|
|
|
// exits with a specific error code.
|
|
|
|
return newDebianResolvconfManager(logf)
|
2021-04-11 06:37:13 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-11 07:31:00 +01:00
|
|
|
// If --version works, or we got some surprising error while
|
|
|
|
// probing, use openresolv. It's the more common implementation,
|
|
|
|
// so in cases where we can't figure things out, it's the least
|
|
|
|
// likely to misbehave.
|
|
|
|
return newOpenresolvManager()
|
2020-07-14 14:12:00 +01:00
|
|
|
}
|