mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-26 11:35:35 +00:00
71029cea2d
This updates all source files to use a new standard header for copyright and license declaration. Notably, copyright no longer includes a date, and we now use the standard SPDX-License-Identifier header. This commit was done almost entirely mechanically with perl, and then some minimal manual fixes. Updates #6865 Signed-off-by: Will Norris <will@tailscale.com>
126 lines
3.1 KiB
Go
126 lines
3.1 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package dns
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
|
|
"go4.org/mem"
|
|
"tailscale.com/types/logger"
|
|
"tailscale.com/util/mak"
|
|
)
|
|
|
|
func NewOSConfigurator(logf logger.Logf, ifName string) (OSConfigurator, error) {
|
|
return &darwinConfigurator{logf: logf, ifName: ifName}, nil
|
|
}
|
|
|
|
// darwinConfigurator is the tailscaled-on-macOS DNS OS configurator that
|
|
// maintains the Split DNS nameserver entries pointing MagicDNS DNS suffixes
|
|
// to 100.100.100.100 using the macOS /etc/resolver/$SUFFIX files.
|
|
type darwinConfigurator struct {
|
|
logf logger.Logf
|
|
ifName string
|
|
}
|
|
|
|
func (c *darwinConfigurator) Close() error {
|
|
c.removeResolverFiles(func(domain string) bool { return true })
|
|
return nil
|
|
}
|
|
|
|
func (c *darwinConfigurator) SupportsSplitDNS() bool {
|
|
return true
|
|
}
|
|
|
|
func (c *darwinConfigurator) SetDNS(cfg OSConfig) error {
|
|
var buf bytes.Buffer
|
|
buf.WriteString(macResolverFileHeader)
|
|
for i, ip := range cfg.Nameservers {
|
|
if i == 0 {
|
|
buf.WriteString("nameserver ")
|
|
} else {
|
|
buf.WriteString(" ")
|
|
}
|
|
buf.WriteString(ip.String())
|
|
}
|
|
buf.WriteString("\n")
|
|
|
|
if err := os.MkdirAll("/etc/resolver", 0755); err != nil {
|
|
return err
|
|
}
|
|
|
|
var keep map[string]bool
|
|
|
|
// Add a dummy file to /etc/resolver with a "search ..." directive if we have
|
|
// search suffixes to add.
|
|
if len(cfg.SearchDomains) > 0 {
|
|
const searchFile = "search.tailscale" // fake DNS suffix+TLD to put our search
|
|
mak.Set(&keep, searchFile, true)
|
|
var sbuf bytes.Buffer
|
|
sbuf.WriteString(macResolverFileHeader)
|
|
sbuf.WriteString("search")
|
|
for _, d := range cfg.SearchDomains {
|
|
sbuf.WriteString(" ")
|
|
sbuf.WriteString(string(d.WithoutTrailingDot()))
|
|
}
|
|
sbuf.WriteString("\n")
|
|
if err := os.WriteFile("/etc/resolver/"+searchFile, sbuf.Bytes(), 0644); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for _, d := range cfg.MatchDomains {
|
|
fileBase := string(d.WithoutTrailingDot())
|
|
mak.Set(&keep, fileBase, true)
|
|
fullPath := "/etc/resolver/" + fileBase
|
|
|
|
if err := os.WriteFile(fullPath, buf.Bytes(), 0644); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return c.removeResolverFiles(func(domain string) bool { return !keep[domain] })
|
|
}
|
|
|
|
func (c *darwinConfigurator) GetBaseConfig() (OSConfig, error) {
|
|
return OSConfig{}, ErrGetBaseConfigNotSupported
|
|
}
|
|
|
|
const macResolverFileHeader = "# Added by tailscaled\n"
|
|
|
|
// removeResolverFiles deletes all files in /etc/resolver for which the shouldDelete
|
|
// func returns true.
|
|
func (c *darwinConfigurator) removeResolverFiles(shouldDelete func(domain string) bool) error {
|
|
dents, err := os.ReadDir("/etc/resolver")
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, de := range dents {
|
|
if !de.Type().IsRegular() {
|
|
continue
|
|
}
|
|
name := de.Name()
|
|
if !shouldDelete(name) {
|
|
continue
|
|
}
|
|
fullPath := "/etc/resolver/" + name
|
|
contents, err := os.ReadFile(fullPath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) { // race?
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
if !mem.HasPrefix(mem.B(contents), mem.S(macResolverFileHeader)) {
|
|
continue
|
|
}
|
|
if err := os.Remove(fullPath); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|