2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2016-01-30 01:08:11 +00:00
|
|
|
|
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2022-10-01 20:32:09 +00:00
|
|
|
"bytes"
|
2023-06-23 00:33:17 +00:00
|
|
|
"context"
|
2016-01-30 01:08:11 +00:00
|
|
|
"fmt"
|
2021-08-18 11:15:23 +00:00
|
|
|
"net"
|
2016-01-30 01:08:11 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2016-09-10 04:45:03 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-01-30 01:08:11 +00:00
|
|
|
|
2023-06-23 00:33:17 +00:00
|
|
|
"github.com/containerd/containerd/log"
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/etchosts"
|
|
|
|
"github.com/docker/docker/libnetwork/resolvconf"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2016-01-30 01:08:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultPrefix = "/var/lib/docker/network/files"
|
2022-10-01 23:02:22 +00:00
|
|
|
dirPerm = 0o755
|
|
|
|
filePerm = 0o644
|
2016-01-30 01:08:11 +00:00
|
|
|
)
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) startResolver(restore bool) {
|
2016-01-30 01:08:11 +00:00
|
|
|
sb.resolverOnce.Do(func() {
|
|
|
|
var err error
|
2022-10-31 21:19:33 +00:00
|
|
|
sb.resolver = NewResolver(resolverIPSandbox, true, sb)
|
2016-01-30 01:08:11 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
sb.resolver = nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-06-09 23:05:11 +00:00
|
|
|
// In the case of live restore container is already running with
|
|
|
|
// right resolv.conf contents created before. Just update the
|
|
|
|
// external DNS servers from the restored sandbox for embedded
|
|
|
|
// server to use.
|
|
|
|
if !restore {
|
|
|
|
err = sb.rebuildDNS()
|
|
|
|
if err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Errorf("Updating resolv.conf failed for container %s, %q", sb.ContainerID(), err)
|
2016-06-09 23:05:11 +00:00
|
|
|
return
|
|
|
|
}
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
sb.resolver.SetExtServers(sb.extDNS)
|
|
|
|
|
2016-09-21 20:38:29 +00:00
|
|
|
if err = sb.osSbox.InvokeFunc(sb.resolver.SetupFunc(0)); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Errorf("Resolver Setup function failed for container %s, %q", sb.ContainerID(), err)
|
2016-09-21 20:38:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:08:11 +00:00
|
|
|
if err = sb.resolver.Start(); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Errorf("Resolver Start failed for container %s, %q", sb.ContainerID(), err)
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) setupResolutionFiles() error {
|
2016-01-30 01:08:11 +00:00
|
|
|
if err := sb.buildHostsFile(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sb.updateParentHosts(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-20 15:57:02 +00:00
|
|
|
return sb.setupDNS()
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) buildHostsFile() error {
|
2016-01-30 01:08:11 +00:00
|
|
|
if sb.config.hostsPath == "" {
|
|
|
|
sb.config.hostsPath = defaultPrefix + "/" + sb.id + "/hosts"
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, _ := filepath.Split(sb.config.hostsPath)
|
|
|
|
if err := createBasePath(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is for the host mode networking
|
2018-07-17 00:35:03 +00:00
|
|
|
if sb.config.useDefaultSandBox && len(sb.config.extraHosts) == 0 {
|
|
|
|
// We are working under the assumption that the origin file option had been properly expressed by the upper layer
|
|
|
|
// if not here we are going to error out
|
2016-01-30 01:08:11 +00:00
|
|
|
if err := copyFile(sb.config.originHostsPath, sb.config.hostsPath); err != nil && !os.IsNotExist(err) {
|
|
|
|
return types.InternalErrorf("could not copy source hosts file %s to %s: %v", sb.config.originHostsPath, sb.config.hostsPath, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
extraContent := make([]etchosts.Record, 0, len(sb.config.extraHosts))
|
|
|
|
for _, extraHost := range sb.config.extraHosts {
|
|
|
|
extraContent = append(extraContent, etchosts.Record{Hosts: extraHost.name, IP: extraHost.IP})
|
|
|
|
}
|
|
|
|
|
|
|
|
return etchosts.Build(sb.config.hostsPath, "", sb.config.hostName, sb.config.domainName, extraContent)
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) updateHostsFile(ifaceIPs []string) error {
|
2021-05-28 00:15:56 +00:00
|
|
|
if len(ifaceIPs) == 0 {
|
2016-05-01 03:47:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:08:11 +00:00
|
|
|
if sb.config.originHostsPath != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 13:25:46 +00:00
|
|
|
// User might have provided a FQDN in hostname or split it across hostname
|
|
|
|
// and domainname. We want the FQDN and the bare hostname.
|
|
|
|
fqdn := sb.config.hostName
|
2016-01-30 01:08:11 +00:00
|
|
|
if sb.config.domainName != "" {
|
2022-11-01 11:50:18 +00:00
|
|
|
fqdn += "." + sb.config.domainName
|
2016-12-09 13:25:46 +00:00
|
|
|
}
|
2022-11-01 11:50:18 +00:00
|
|
|
hosts := fqdn
|
2016-12-09 13:25:46 +00:00
|
|
|
|
2022-11-01 11:50:18 +00:00
|
|
|
if hostName, _, ok := strings.Cut(fqdn, "."); ok {
|
|
|
|
hosts += " " + hostName
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 21:22:46 +00:00
|
|
|
var extraContent []etchosts.Record
|
|
|
|
for _, ip := range ifaceIPs {
|
2022-11-01 11:50:18 +00:00
|
|
|
extraContent = append(extraContent, etchosts.Record{Hosts: hosts, IP: ip})
|
2019-08-30 21:22:46 +00:00
|
|
|
}
|
2016-01-30 01:08:11 +00:00
|
|
|
|
|
|
|
sb.addHostsEntries(extraContent)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) addHostsEntries(recs []etchosts.Record) {
|
2016-01-30 01:08:11 +00:00
|
|
|
if err := etchosts.Add(sb.config.hostsPath, recs); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed adding service host entries to the running container: %v", err)
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) deleteHostsEntries(recs []etchosts.Record) {
|
2016-01-30 01:08:11 +00:00
|
|
|
if err := etchosts.Delete(sb.config.hostsPath, recs); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Warnf("Failed deleting service host entries to the running container: %v", err)
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) updateParentHosts() error {
|
|
|
|
var pSb *Sandbox
|
2016-01-30 01:08:11 +00:00
|
|
|
|
|
|
|
for _, update := range sb.config.parentUpdates {
|
|
|
|
sb.controller.WalkSandboxes(SandboxContainerWalker(&pSb, update.cid))
|
|
|
|
if pSb == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-01-12 01:10:09 +00:00
|
|
|
if err := etchosts.Update(pSb.config.hostsPath, update.ip, update.name); err != nil {
|
2016-01-30 01:08:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) restorePath() {
|
2016-06-11 00:32:19 +00:00
|
|
|
if sb.config.resolvConfPath == "" {
|
|
|
|
sb.config.resolvConfPath = defaultPrefix + "/" + sb.id + "/resolv.conf"
|
|
|
|
}
|
|
|
|
sb.config.resolvConfHashFile = sb.config.resolvConfPath + ".hash"
|
|
|
|
if sb.config.hostsPath == "" {
|
|
|
|
sb.config.hostsPath = defaultPrefix + "/" + sb.id + "/hosts"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) setExternalResolvers(content []byte, addrType int, checkLoopback bool) {
|
2016-12-19 02:27:13 +00:00
|
|
|
servers := resolvconf.GetNameservers(content, addrType)
|
|
|
|
for _, ip := range servers {
|
|
|
|
hostLoopback := false
|
2021-08-18 11:15:23 +00:00
|
|
|
if checkLoopback && isIPv4Loopback(ip) {
|
|
|
|
hostLoopback = true
|
2016-12-19 02:27:13 +00:00
|
|
|
}
|
|
|
|
sb.extDNS = append(sb.extDNS, extDNSEntry{
|
2017-01-19 22:25:26 +00:00
|
|
|
IPStr: ip,
|
|
|
|
HostLoopback: hostLoopback,
|
2016-12-19 02:27:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 11:15:23 +00:00
|
|
|
// isIPv4Loopback checks if the given IP address is an IPv4 loopback address.
|
|
|
|
// It's based on the logic in Go's net.IP.IsLoopback(), but only the IPv4 part:
|
|
|
|
// https://github.com/golang/go/blob/go1.16.6/src/net/ip.go#L120-L126
|
|
|
|
func isIPv4Loopback(ipAddress string) bool {
|
|
|
|
if ip := net.ParseIP(ipAddress); ip != nil {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
return ip4[0] == 127
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) setupDNS() error {
|
2016-01-30 01:08:11 +00:00
|
|
|
if sb.config.resolvConfPath == "" {
|
|
|
|
sb.config.resolvConfPath = defaultPrefix + "/" + sb.id + "/resolv.conf"
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.config.resolvConfHashFile = sb.config.resolvConfPath + ".hash"
|
|
|
|
|
|
|
|
dir, _ := filepath.Split(sb.config.resolvConfPath)
|
|
|
|
if err := createBasePath(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:35:03 +00:00
|
|
|
// When the user specify a conainter in the host namespace and do no have any dns option specified
|
|
|
|
// we just copy the host resolv.conf from the host itself
|
2022-09-23 20:25:41 +00:00
|
|
|
if sb.config.useDefaultSandBox && len(sb.config.dnsList) == 0 && len(sb.config.dnsSearchList) == 0 && len(sb.config.dnsOptionsList) == 0 {
|
2018-07-17 00:35:03 +00:00
|
|
|
// We are working under the assumption that the origin file option had been properly expressed by the upper layer
|
|
|
|
// if not here we are going to error out
|
2016-01-30 01:08:11 +00:00
|
|
|
if err := copyFile(sb.config.originResolvConfPath, sb.config.resolvConfPath); err != nil {
|
2017-08-04 04:13:14 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("could not copy source resolv.conf file %s to %s: %v", sb.config.originResolvConfPath, sb.config.resolvConfPath, err)
|
|
|
|
}
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Infof("%s does not exist, we create an empty resolv.conf for container", sb.config.originResolvConfPath)
|
2017-08-04 04:13:14 +00:00
|
|
|
if err := createFile(sb.config.resolvConfPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:35:03 +00:00
|
|
|
originResolvConfPath := sb.config.originResolvConfPath
|
|
|
|
if originResolvConfPath == "" {
|
2019-05-31 19:16:20 +00:00
|
|
|
// fallback if not specified
|
|
|
|
originResolvConfPath = resolvconf.Path()
|
2018-07-17 00:35:03 +00:00
|
|
|
}
|
2022-10-01 16:13:34 +00:00
|
|
|
currRC, err := os.ReadFile(originResolvConfPath)
|
2016-01-30 01:08:11 +00:00
|
|
|
if err != nil {
|
2017-08-04 04:13:14 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-01 16:13:34 +00:00
|
|
|
// No /etc/resolv.conf found: we'll use the default resolvers (Google's Public DNS).
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithField("path", originResolvConfPath).Infof("no resolv.conf found, falling back to defaults")
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 16:18:33 +00:00
|
|
|
var newRC *resolvconf.File
|
2016-01-30 01:08:11 +00:00
|
|
|
if len(sb.config.dnsList) > 0 || len(sb.config.dnsSearchList) > 0 || len(sb.config.dnsOptionsList) > 0 {
|
|
|
|
var (
|
2022-10-01 16:18:33 +00:00
|
|
|
dnsList = sb.config.dnsList
|
|
|
|
dnsSearchList = sb.config.dnsSearchList
|
|
|
|
dnsOptionsList = sb.config.dnsOptionsList
|
2016-01-30 01:08:11 +00:00
|
|
|
)
|
2022-10-01 16:18:33 +00:00
|
|
|
if len(sb.config.dnsList) == 0 {
|
|
|
|
dnsList = resolvconf.GetNameservers(currRC, resolvconf.IP)
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
2022-10-01 16:18:33 +00:00
|
|
|
if len(sb.config.dnsSearchList) == 0 {
|
|
|
|
dnsSearchList = resolvconf.GetSearchDomains(currRC)
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
2022-10-01 16:18:33 +00:00
|
|
|
if len(sb.config.dnsOptionsList) == 0 {
|
|
|
|
dnsOptionsList = resolvconf.GetOptions(currRC)
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
newRC, err = resolvconf.Build(sb.config.resolvConfPath, dnsList, dnsSearchList, dnsOptionsList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-19 02:27:13 +00:00
|
|
|
// After building the resolv.conf from the user config save the
|
|
|
|
// external resolvers in the sandbox. Note that --dns 127.0.0.x
|
|
|
|
// config refers to the loopback in the container namespace
|
2023-02-10 21:20:06 +00:00
|
|
|
sb.setExternalResolvers(newRC.Content, resolvconf.IPv4, len(sb.config.dnsList) == 0)
|
2016-01-30 01:08:11 +00:00
|
|
|
} else {
|
2016-12-19 02:27:13 +00:00
|
|
|
// If the host resolv.conf file has 127.0.0.x container should
|
2017-05-22 02:25:52 +00:00
|
|
|
// use the host resolver for queries. This is supported by the
|
2016-12-19 02:27:13 +00:00
|
|
|
// docker embedded DNS server. Hence save the external resolvers
|
|
|
|
// before filtering it out.
|
2022-10-01 16:13:34 +00:00
|
|
|
sb.setExternalResolvers(currRC, resolvconf.IPv4, true)
|
2016-12-19 02:27:13 +00:00
|
|
|
|
2016-01-30 01:08:11 +00:00
|
|
|
// Replace any localhost/127.* (at this point we have no info about ipv6, pass it as true)
|
2022-10-01 16:13:34 +00:00
|
|
|
newRC, err = resolvconf.FilterResolvDNS(currRC, true)
|
|
|
|
if err != nil {
|
2016-01-30 01:08:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// No contention on container resolv.conf file at sandbox creation
|
2022-10-01 16:13:34 +00:00
|
|
|
err = os.WriteFile(sb.config.resolvConfPath, newRC.Content, filePerm)
|
|
|
|
if err != nil {
|
2016-01-30 01:08:11 +00:00
|
|
|
return types.InternalErrorf("failed to write unhaltered resolv.conf file content when setting up dns for sandbox %s: %v", sb.ID(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write hash
|
2022-10-01 20:32:09 +00:00
|
|
|
err = os.WriteFile(sb.config.resolvConfHashFile, newRC.Hash, filePerm)
|
|
|
|
if err != nil {
|
2016-01-30 01:08:11 +00:00
|
|
|
return types.InternalErrorf("failed to write resolv.conf hash file when setting up dns for sandbox %s: %v", sb.ID(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) updateDNS(ipv6Enabled bool) error {
|
2016-01-30 01:08:11 +00:00
|
|
|
// This is for the host mode networking
|
2018-07-17 00:35:03 +00:00
|
|
|
if sb.config.useDefaultSandBox {
|
2016-01-30 01:08:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sb.config.dnsList) > 0 || len(sb.config.dnsSearchList) > 0 || len(sb.config.dnsOptionsList) > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-01 20:32:09 +00:00
|
|
|
var currHash []byte
|
2016-01-30 01:08:11 +00:00
|
|
|
currRC, err := resolvconf.GetSpecific(sb.config.resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-01 20:32:09 +00:00
|
|
|
currHash, err = os.ReadFile(sb.config.resolvConfHashFile)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 20:32:09 +00:00
|
|
|
if len(currHash) > 0 && !bytes.Equal(currHash, currRC.Hash) {
|
2016-01-30 01:08:11 +00:00
|
|
|
// Seems the user has changed the container resolv.conf since the last time
|
|
|
|
// we checked so return without doing anything.
|
2023-06-23 00:33:17 +00:00
|
|
|
// log.G(ctx).Infof("Skipping update of resolv.conf file with ipv6Enabled: %t because file was touched by user", ipv6Enabled)
|
2016-01-30 01:08:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace any localhost/127.* and remove IPv6 nameservers if IPv6 disabled.
|
|
|
|
newRC, err := resolvconf.FilterResolvDNS(currRC.Content, ipv6Enabled)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-01 23:02:22 +00:00
|
|
|
err = os.WriteFile(sb.config.resolvConfPath, newRC.Content, filePerm)
|
2016-01-30 01:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the new hash in a temp file and rename it to make the update atomic
|
|
|
|
dir := path.Dir(sb.config.resolvConfPath)
|
2021-08-24 10:10:50 +00:00
|
|
|
tmpHashFile, err := os.CreateTemp(dir, "hash")
|
2016-01-30 01:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-01 23:29:51 +00:00
|
|
|
if err = tmpHashFile.Chmod(filePerm); err != nil {
|
|
|
|
tmpHashFile.Close()
|
|
|
|
return err
|
|
|
|
}
|
2022-10-01 20:32:09 +00:00
|
|
|
_, err = tmpHashFile.Write(newRC.Hash)
|
2016-07-01 23:29:51 +00:00
|
|
|
if err1 := tmpHashFile.Close(); err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-01-30 01:08:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-10-01 20:32:09 +00:00
|
|
|
return os.Rename(tmpHashFile.Name(), sb.config.resolvConfHashFile)
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Embedded DNS server has to be enabled for this sandbox. Rebuild the container's
|
2016-06-22 06:20:30 +00:00
|
|
|
// resolv.conf by doing the following
|
2016-01-30 01:08:11 +00:00
|
|
|
// - Add only the embedded server's IP to container's resolv.conf
|
|
|
|
// - If the embedded server needs any resolv.conf options add it to the current list
|
2023-01-12 01:10:09 +00:00
|
|
|
func (sb *Sandbox) rebuildDNS() error {
|
2022-10-01 16:56:39 +00:00
|
|
|
currRC, err := os.ReadFile(sb.config.resolvConfPath)
|
2016-01-30 01:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-10 04:45:03 +00:00
|
|
|
// If the user config and embedded DNS server both have ndots option set,
|
|
|
|
// remember the user's config so that unqualified names not in the docker
|
|
|
|
// domain can be dropped.
|
|
|
|
resOptions := sb.resolver.ResolverOptions()
|
2022-10-01 17:13:04 +00:00
|
|
|
dnsOptionsList := resolvconf.GetOptions(currRC)
|
2016-09-10 04:45:03 +00:00
|
|
|
|
|
|
|
dnsOpt:
|
|
|
|
for _, resOpt := range resOptions {
|
|
|
|
if strings.Contains(resOpt, "ndots") {
|
2018-01-26 18:07:08 +00:00
|
|
|
for _, option := range dnsOptionsList {
|
2016-09-10 04:45:03 +00:00
|
|
|
if strings.Contains(option, "ndots") {
|
|
|
|
parts := strings.Split(option, ":")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return fmt.Errorf("invalid ndots option %v", option)
|
|
|
|
}
|
|
|
|
if num, err := strconv.Atoi(parts[1]); err != nil {
|
2018-06-28 23:01:26 +00:00
|
|
|
return fmt.Errorf("invalid number for ndots option: %v", parts[1])
|
2018-06-28 22:58:36 +00:00
|
|
|
} else if num >= 0 {
|
2018-01-26 18:07:08 +00:00
|
|
|
// if the user sets ndots, use the user setting
|
2016-09-10 04:45:03 +00:00
|
|
|
sb.ndotsSet = true
|
|
|
|
break dnsOpt
|
2018-06-28 23:22:17 +00:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("invalid number for ndots option: %v", num)
|
2016-09-10 04:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 18:07:08 +00:00
|
|
|
if !sb.ndotsSet {
|
|
|
|
// if the user did not set the ndots, set it to 0 to prioritize the service name resolution
|
|
|
|
// Ref: https://linux.die.net/man/5/resolv.conf
|
|
|
|
dnsOptionsList = append(dnsOptionsList, resOptions...)
|
|
|
|
}
|
2022-10-01 17:16:11 +00:00
|
|
|
if len(sb.extDNS) == 0 {
|
|
|
|
sb.setExternalResolvers(currRC, resolvconf.IPv4, false)
|
|
|
|
}
|
2016-01-30 01:08:11 +00:00
|
|
|
|
2022-10-01 17:13:04 +00:00
|
|
|
var (
|
|
|
|
// external v6 DNS servers have to be listed in resolv.conf
|
|
|
|
dnsList = append([]string{sb.resolver.NameServer()}, resolvconf.GetNameservers(currRC, resolvconf.IPv6)...)
|
|
|
|
dnsSearchList = resolvconf.GetSearchDomains(currRC)
|
|
|
|
)
|
2016-01-30 01:08:11 +00:00
|
|
|
|
|
|
|
_, err = resolvconf.Build(sb.config.resolvConfPath, dnsList, dnsSearchList, dnsOptionsList)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func createBasePath(dir string) error {
|
|
|
|
return os.MkdirAll(dir, dirPerm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFile(path string) error {
|
|
|
|
var f *os.File
|
|
|
|
|
|
|
|
dir, _ := filepath.Split(path)
|
|
|
|
err := createBasePath(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err = os.Create(path)
|
|
|
|
if err == nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFile(src, dst string) error {
|
2021-08-24 10:10:50 +00:00
|
|
|
sBytes, err := os.ReadFile(src)
|
2016-01-30 01:08:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-24 10:10:50 +00:00
|
|
|
return os.WriteFile(dst, sBytes, filePerm)
|
2016-01-30 01:08:11 +00:00
|
|
|
}
|