2014-05-05 22:51:32 +00:00
|
|
|
package resolvconf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2014-12-10 05:55:09 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/utils"
|
2014-05-05 22:51:32 +00:00
|
|
|
)
|
|
|
|
|
2014-09-16 03:30:10 +00:00
|
|
|
var (
|
2015-01-09 17:06:48 +00:00
|
|
|
// Note: the default IPv4 & IPv6 resolvers are set to Google's Public DNS
|
|
|
|
defaultIPv4Dns = []string{"nameserver 8.8.8.8", "nameserver 8.8.4.4"}
|
|
|
|
defaultIPv6Dns = []string{"nameserver 2001:4860:4860::8888", "nameserver 2001:4860:4860::8844"}
|
|
|
|
ipv4NumBlock = `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`
|
|
|
|
ipv4Address = `(` + ipv4NumBlock + `\.){3}` + ipv4NumBlock
|
|
|
|
// This is not an IPv6 address verifier as it will accept a super-set of IPv6, and also
|
|
|
|
// will *not match* IPv4-Embedded IPv6 Addresses (RFC6052), but that and other variants
|
|
|
|
// -- e.g. other link-local types -- either won't work in containers or are unnecessary.
|
|
|
|
// For readability and sufficiency for Docker purposes this seemed more reasonable than a
|
|
|
|
// 1000+ character regexp with exact and complete IPv6 validation
|
|
|
|
ipv6Address = `([0-9A-Fa-f]{0,4}:){2,7}([0-9A-Fa-f]{0,4})`
|
|
|
|
|
|
|
|
localhostRegexp = regexp.MustCompile(`(?m)^nameserver\s+((127\.([0-9]{1,3}.){2}[0-9]{1,3})|(::1))\s*\n*`)
|
|
|
|
nsIPv6Regexp = regexp.MustCompile(`(?m)^nameserver\s+` + ipv6Address + `\s*\n*`)
|
|
|
|
nsRegexp = regexp.MustCompile(`^\s*nameserver\s*((` + ipv4Address + `)|(` + ipv6Address + `))\s*$`)
|
2014-12-10 05:55:09 +00:00
|
|
|
searchRegexp = regexp.MustCompile(`^\s*search\s*(([^\s]+\s*)*)$`)
|
2014-09-16 03:30:10 +00:00
|
|
|
)
|
|
|
|
|
2014-12-10 05:55:09 +00:00
|
|
|
var lastModified struct {
|
|
|
|
sync.Mutex
|
|
|
|
sha256 string
|
|
|
|
contents []byte
|
|
|
|
}
|
|
|
|
|
2014-05-05 22:51:32 +00:00
|
|
|
func Get() ([]byte, error) {
|
|
|
|
resolv, err := ioutil.ReadFile("/etc/resolv.conf")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resolv, nil
|
|
|
|
}
|
|
|
|
|
2014-12-10 05:55:09 +00:00
|
|
|
// Retrieves the host /etc/resolv.conf file, checks against the last hash
|
|
|
|
// and, if modified since last check, returns the bytes and new hash.
|
|
|
|
// This feature is used by the resolv.conf updater for containers
|
|
|
|
func GetIfChanged() ([]byte, string, error) {
|
|
|
|
lastModified.Lock()
|
|
|
|
defer lastModified.Unlock()
|
|
|
|
|
|
|
|
resolv, err := ioutil.ReadFile("/etc/resolv.conf")
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
newHash, err := utils.HashData(bytes.NewReader(resolv))
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
if lastModified.sha256 != newHash {
|
|
|
|
lastModified.sha256 = newHash
|
|
|
|
lastModified.contents = resolv
|
|
|
|
return resolv, newHash, nil
|
|
|
|
}
|
|
|
|
// nothing changed, so return no data
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve the last used contents and hash of the host resolv.conf
|
|
|
|
// Used by containers updating on restart
|
|
|
|
func GetLastModified() ([]byte, string) {
|
|
|
|
lastModified.Lock()
|
|
|
|
defer lastModified.Unlock()
|
|
|
|
|
|
|
|
return lastModified.contents, lastModified.sha256
|
|
|
|
}
|
|
|
|
|
2015-01-09 17:06:48 +00:00
|
|
|
// FilterResolvDns has two main jobs:
|
|
|
|
// 1. It looks for localhost (127.*|::1) entries in the provided
|
|
|
|
// resolv.conf, removing local nameserver entries, and, if the resulting
|
|
|
|
// cleaned config has no defined nameservers left, adds default DNS entries
|
|
|
|
// 2. Given the caller provides the enable/disable state of IPv6, the filter
|
|
|
|
// code will remove all IPv6 nameservers if it is not enabled for containers
|
|
|
|
//
|
2014-12-10 05:55:09 +00:00
|
|
|
// It also returns a boolean to notify the caller if changes were made at all
|
2015-01-09 17:06:48 +00:00
|
|
|
func FilterResolvDns(resolvConf []byte, ipv6Enabled bool) ([]byte, bool) {
|
2014-12-10 05:55:09 +00:00
|
|
|
changed := false
|
2015-01-09 17:06:48 +00:00
|
|
|
cleanedResolvConf := localhostRegexp.ReplaceAll(resolvConf, []byte{})
|
|
|
|
// if IPv6 is not enabled, also clean out any IPv6 address nameserver
|
|
|
|
if !ipv6Enabled {
|
|
|
|
cleanedResolvConf = nsIPv6Regexp.ReplaceAll(cleanedResolvConf, []byte{})
|
|
|
|
}
|
|
|
|
// if the resulting resolvConf has no more nameservers defined, add appropriate
|
|
|
|
// default DNS servers for IPv4 and (optionally) IPv6
|
|
|
|
if len(GetNameservers(cleanedResolvConf)) == 0 {
|
|
|
|
log.Infof("No non-localhost DNS nameservers are left in resolv.conf. Using default external servers : %v", defaultIPv4Dns)
|
|
|
|
dns := defaultIPv4Dns
|
|
|
|
if ipv6Enabled {
|
|
|
|
log.Infof("IPv6 enabled; Adding default IPv6 external servers : %v", defaultIPv6Dns)
|
|
|
|
dns = append(dns, defaultIPv6Dns...)
|
|
|
|
}
|
|
|
|
cleanedResolvConf = append(cleanedResolvConf, []byte("\n"+strings.Join(dns, "\n"))...)
|
2014-12-10 05:55:09 +00:00
|
|
|
}
|
|
|
|
if !bytes.Equal(resolvConf, cleanedResolvConf) {
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
return cleanedResolvConf, changed
|
|
|
|
}
|
|
|
|
|
2014-05-05 22:51:32 +00:00
|
|
|
// getLines parses input into lines and strips away comments.
|
|
|
|
func getLines(input []byte, commentMarker []byte) [][]byte {
|
|
|
|
lines := bytes.Split(input, []byte("\n"))
|
|
|
|
var output [][]byte
|
|
|
|
for _, currentLine := range lines {
|
|
|
|
var commentIndex = bytes.Index(currentLine, commentMarker)
|
|
|
|
if commentIndex == -1 {
|
|
|
|
output = append(output, currentLine)
|
|
|
|
} else {
|
|
|
|
output = append(output, currentLine[:commentIndex])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNameservers returns nameservers (if any) listed in /etc/resolv.conf
|
|
|
|
func GetNameservers(resolvConf []byte) []string {
|
|
|
|
nameservers := []string{}
|
|
|
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
2014-09-16 03:30:10 +00:00
|
|
|
var ns = nsRegexp.FindSubmatch(line)
|
2014-05-05 22:51:32 +00:00
|
|
|
if len(ns) > 0 {
|
|
|
|
nameservers = append(nameservers, string(ns[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nameservers
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNameserversAsCIDR returns nameservers (if any) listed in
|
|
|
|
// /etc/resolv.conf as CIDR blocks (e.g., "1.2.3.4/32")
|
|
|
|
// This function's output is intended for net.ParseCIDR
|
|
|
|
func GetNameserversAsCIDR(resolvConf []byte) []string {
|
|
|
|
nameservers := []string{}
|
|
|
|
for _, nameserver := range GetNameservers(resolvConf) {
|
|
|
|
nameservers = append(nameservers, nameserver+"/32")
|
|
|
|
}
|
|
|
|
return nameservers
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSearchDomains returns search domains (if any) listed in /etc/resolv.conf
|
|
|
|
// If more than one search line is encountered, only the contents of the last
|
|
|
|
// one is returned.
|
|
|
|
func GetSearchDomains(resolvConf []byte) []string {
|
|
|
|
domains := []string{}
|
|
|
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
2014-09-16 03:30:10 +00:00
|
|
|
match := searchRegexp.FindSubmatch(line)
|
2014-05-05 22:51:32 +00:00
|
|
|
if match == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
domains = strings.Fields(string(match[1]))
|
|
|
|
}
|
|
|
|
return domains
|
|
|
|
}
|
|
|
|
|
|
|
|
func Build(path string, dns, dnsSearch []string) error {
|
|
|
|
content := bytes.NewBuffer(nil)
|
|
|
|
for _, dns := range dns {
|
|
|
|
if _, err := content.WriteString("nameserver " + dns + "\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(dnsSearch) > 0 {
|
2014-06-26 11:03:23 +00:00
|
|
|
if searchString := strings.Join(dnsSearch, " "); strings.Trim(searchString, " ") != "." {
|
|
|
|
if _, err := content.WriteString("search " + searchString + "\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-05 22:51:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(path, content.Bytes(), 0644)
|
|
|
|
}
|