sandbox_dns_unix.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //go:build !windows
  2. // +build !windows
  3. package libnetwork
  4. import (
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "github.com/docker/libnetwork/etchosts"
  13. "github.com/docker/libnetwork/resolvconf"
  14. "github.com/docker/libnetwork/resolvconf/dns"
  15. "github.com/docker/libnetwork/types"
  16. "github.com/sirupsen/logrus"
  17. )
  18. const (
  19. defaultPrefix = "/var/lib/docker/network/files"
  20. dirPerm = 0755
  21. filePerm = 0644
  22. )
  23. func (sb *sandbox) startResolver(restore bool) {
  24. sb.resolverOnce.Do(func() {
  25. var err error
  26. sb.resolver = NewResolver(resolverIPSandbox, true, sb.Key(), sb)
  27. defer func() {
  28. if err != nil {
  29. sb.resolver = nil
  30. }
  31. }()
  32. // In the case of live restore container is already running with
  33. // right resolv.conf contents created before. Just update the
  34. // external DNS servers from the restored sandbox for embedded
  35. // server to use.
  36. if !restore {
  37. err = sb.rebuildDNS()
  38. if err != nil {
  39. logrus.Errorf("Updating resolv.conf failed for container %s, %q", sb.ContainerID(), err)
  40. return
  41. }
  42. }
  43. sb.resolver.SetExtServers(sb.extDNS)
  44. if err = sb.osSbox.InvokeFunc(sb.resolver.SetupFunc(0)); err != nil {
  45. logrus.Errorf("Resolver Setup function failed for container %s, %q", sb.ContainerID(), err)
  46. return
  47. }
  48. if err = sb.resolver.Start(); err != nil {
  49. logrus.Errorf("Resolver Start failed for container %s, %q", sb.ContainerID(), err)
  50. }
  51. })
  52. }
  53. func (sb *sandbox) setupResolutionFiles() error {
  54. if err := sb.buildHostsFile(); err != nil {
  55. return err
  56. }
  57. if err := sb.updateParentHosts(); err != nil {
  58. return err
  59. }
  60. return sb.setupDNS()
  61. }
  62. func (sb *sandbox) buildHostsFile() error {
  63. if sb.config.hostsPath == "" {
  64. sb.config.hostsPath = defaultPrefix + "/" + sb.id + "/hosts"
  65. }
  66. dir, _ := filepath.Split(sb.config.hostsPath)
  67. if err := createBasePath(dir); err != nil {
  68. return err
  69. }
  70. // This is for the host mode networking
  71. if sb.config.useDefaultSandBox && len(sb.config.extraHosts) == 0 {
  72. // We are working under the assumption that the origin file option had been properly expressed by the upper layer
  73. // if not here we are going to error out
  74. if err := copyFile(sb.config.originHostsPath, sb.config.hostsPath); err != nil && !os.IsNotExist(err) {
  75. return types.InternalErrorf("could not copy source hosts file %s to %s: %v", sb.config.originHostsPath, sb.config.hostsPath, err)
  76. }
  77. return nil
  78. }
  79. extraContent := make([]etchosts.Record, 0, len(sb.config.extraHosts))
  80. for _, extraHost := range sb.config.extraHosts {
  81. extraContent = append(extraContent, etchosts.Record{Hosts: extraHost.name, IP: extraHost.IP})
  82. }
  83. return etchosts.Build(sb.config.hostsPath, "", sb.config.hostName, sb.config.domainName, extraContent)
  84. }
  85. func (sb *sandbox) updateHostsFile(ifaceIPs []string) error {
  86. if ifaceIPs == nil || len(ifaceIPs) == 0 {
  87. return nil
  88. }
  89. if sb.config.originHostsPath != "" {
  90. return nil
  91. }
  92. // User might have provided a FQDN in hostname or split it across hostname
  93. // and domainname. We want the FQDN and the bare hostname.
  94. fqdn := sb.config.hostName
  95. mhost := sb.config.hostName
  96. if sb.config.domainName != "" {
  97. fqdn = fmt.Sprintf("%s.%s", fqdn, sb.config.domainName)
  98. }
  99. parts := strings.SplitN(fqdn, ".", 2)
  100. if len(parts) == 2 {
  101. mhost = fmt.Sprintf("%s %s", fqdn, parts[0])
  102. }
  103. var extraContent []etchosts.Record
  104. for _, ip := range ifaceIPs {
  105. extraContent = append(extraContent, etchosts.Record{Hosts: mhost, IP: ip})
  106. }
  107. sb.addHostsEntries(extraContent)
  108. return nil
  109. }
  110. func (sb *sandbox) addHostsEntries(recs []etchosts.Record) {
  111. if err := etchosts.Add(sb.config.hostsPath, recs); err != nil {
  112. logrus.Warnf("Failed adding service host entries to the running container: %v", err)
  113. }
  114. }
  115. func (sb *sandbox) deleteHostsEntries(recs []etchosts.Record) {
  116. if err := etchosts.Delete(sb.config.hostsPath, recs); err != nil {
  117. logrus.Warnf("Failed deleting service host entries to the running container: %v", err)
  118. }
  119. }
  120. func (sb *sandbox) updateParentHosts() error {
  121. var pSb Sandbox
  122. for _, update := range sb.config.parentUpdates {
  123. sb.controller.WalkSandboxes(SandboxContainerWalker(&pSb, update.cid))
  124. if pSb == nil {
  125. continue
  126. }
  127. if err := etchosts.Update(pSb.(*sandbox).config.hostsPath, update.ip, update.name); err != nil {
  128. return err
  129. }
  130. }
  131. return nil
  132. }
  133. func (sb *sandbox) restorePath() {
  134. if sb.config.resolvConfPath == "" {
  135. sb.config.resolvConfPath = defaultPrefix + "/" + sb.id + "/resolv.conf"
  136. }
  137. sb.config.resolvConfHashFile = sb.config.resolvConfPath + ".hash"
  138. if sb.config.hostsPath == "" {
  139. sb.config.hostsPath = defaultPrefix + "/" + sb.id + "/hosts"
  140. }
  141. }
  142. func (sb *sandbox) setExternalResolvers(content []byte, addrType int, checkLoopback bool) {
  143. servers := resolvconf.GetNameservers(content, addrType)
  144. for _, ip := range servers {
  145. hostLoopback := false
  146. if checkLoopback {
  147. hostLoopback = dns.IsIPv4Localhost(ip)
  148. }
  149. sb.extDNS = append(sb.extDNS, extDNSEntry{
  150. IPStr: ip,
  151. HostLoopback: hostLoopback,
  152. })
  153. }
  154. }
  155. func (sb *sandbox) setupDNS() error {
  156. var newRC *resolvconf.File
  157. if sb.config.resolvConfPath == "" {
  158. sb.config.resolvConfPath = defaultPrefix + "/" + sb.id + "/resolv.conf"
  159. }
  160. sb.config.resolvConfHashFile = sb.config.resolvConfPath + ".hash"
  161. dir, _ := filepath.Split(sb.config.resolvConfPath)
  162. if err := createBasePath(dir); err != nil {
  163. return err
  164. }
  165. // When the user specify a conainter in the host namespace and do no have any dns option specified
  166. // we just copy the host resolv.conf from the host itself
  167. if sb.config.useDefaultSandBox &&
  168. len(sb.config.dnsList) == 0 && len(sb.config.dnsSearchList) == 0 && len(sb.config.dnsOptionsList) == 0 {
  169. // We are working under the assumption that the origin file option had been properly expressed by the upper layer
  170. // if not here we are going to error out
  171. if err := copyFile(sb.config.originResolvConfPath, sb.config.resolvConfPath); err != nil {
  172. if !os.IsNotExist(err) {
  173. return fmt.Errorf("could not copy source resolv.conf file %s to %s: %v", sb.config.originResolvConfPath, sb.config.resolvConfPath, err)
  174. }
  175. logrus.Infof("%s does not exist, we create an empty resolv.conf for container", sb.config.originResolvConfPath)
  176. if err := createFile(sb.config.resolvConfPath); err != nil {
  177. return err
  178. }
  179. }
  180. return nil
  181. }
  182. originResolvConfPath := sb.config.originResolvConfPath
  183. if originResolvConfPath == "" {
  184. // fallback if not specified
  185. originResolvConfPath = resolvconf.Path()
  186. }
  187. currRC, err := resolvconf.GetSpecific(originResolvConfPath)
  188. if err != nil {
  189. if !os.IsNotExist(err) {
  190. return err
  191. }
  192. // it's ok to continue if /etc/resolv.conf doesn't exist, default resolvers (Google's Public DNS)
  193. // will be used
  194. currRC = &resolvconf.File{}
  195. logrus.Infof("/etc/resolv.conf does not exist")
  196. }
  197. if len(sb.config.dnsList) > 0 || len(sb.config.dnsSearchList) > 0 || len(sb.config.dnsOptionsList) > 0 {
  198. var (
  199. err error
  200. dnsList = resolvconf.GetNameservers(currRC.Content, types.IP)
  201. dnsSearchList = resolvconf.GetSearchDomains(currRC.Content)
  202. dnsOptionsList = resolvconf.GetOptions(currRC.Content)
  203. )
  204. if len(sb.config.dnsList) > 0 {
  205. dnsList = sb.config.dnsList
  206. }
  207. if len(sb.config.dnsSearchList) > 0 {
  208. dnsSearchList = sb.config.dnsSearchList
  209. }
  210. if len(sb.config.dnsOptionsList) > 0 {
  211. dnsOptionsList = sb.config.dnsOptionsList
  212. }
  213. newRC, err = resolvconf.Build(sb.config.resolvConfPath, dnsList, dnsSearchList, dnsOptionsList)
  214. if err != nil {
  215. return err
  216. }
  217. // After building the resolv.conf from the user config save the
  218. // external resolvers in the sandbox. Note that --dns 127.0.0.x
  219. // config refers to the loopback in the container namespace
  220. sb.setExternalResolvers(newRC.Content, types.IPv4, false)
  221. } else {
  222. // If the host resolv.conf file has 127.0.0.x container should
  223. // use the host resolver for queries. This is supported by the
  224. // docker embedded DNS server. Hence save the external resolvers
  225. // before filtering it out.
  226. sb.setExternalResolvers(currRC.Content, types.IPv4, true)
  227. // Replace any localhost/127.* (at this point we have no info about ipv6, pass it as true)
  228. if newRC, err = resolvconf.FilterResolvDNS(currRC.Content, true); err != nil {
  229. return err
  230. }
  231. // No contention on container resolv.conf file at sandbox creation
  232. if err := ioutil.WriteFile(sb.config.resolvConfPath, newRC.Content, filePerm); err != nil {
  233. return types.InternalErrorf("failed to write unhaltered resolv.conf file content when setting up dns for sandbox %s: %v", sb.ID(), err)
  234. }
  235. }
  236. // Write hash
  237. if err := ioutil.WriteFile(sb.config.resolvConfHashFile, []byte(newRC.Hash), filePerm); err != nil {
  238. return types.InternalErrorf("failed to write resolv.conf hash file when setting up dns for sandbox %s: %v", sb.ID(), err)
  239. }
  240. return nil
  241. }
  242. func (sb *sandbox) updateDNS(ipv6Enabled bool) error {
  243. var (
  244. currHash string
  245. hashFile = sb.config.resolvConfHashFile
  246. )
  247. // This is for the host mode networking
  248. if sb.config.useDefaultSandBox {
  249. return nil
  250. }
  251. if len(sb.config.dnsList) > 0 || len(sb.config.dnsSearchList) > 0 || len(sb.config.dnsOptionsList) > 0 {
  252. return nil
  253. }
  254. currRC, err := resolvconf.GetSpecific(sb.config.resolvConfPath)
  255. if err != nil {
  256. if !os.IsNotExist(err) {
  257. return err
  258. }
  259. } else {
  260. h, err := ioutil.ReadFile(hashFile)
  261. if err != nil {
  262. if !os.IsNotExist(err) {
  263. return err
  264. }
  265. } else {
  266. currHash = string(h)
  267. }
  268. }
  269. if currHash != "" && currHash != currRC.Hash {
  270. // Seems the user has changed the container resolv.conf since the last time
  271. // we checked so return without doing anything.
  272. //logrus.Infof("Skipping update of resolv.conf file with ipv6Enabled: %t because file was touched by user", ipv6Enabled)
  273. return nil
  274. }
  275. // replace any localhost/127.* and remove IPv6 nameservers if IPv6 disabled.
  276. newRC, err := resolvconf.FilterResolvDNS(currRC.Content, ipv6Enabled)
  277. if err != nil {
  278. return err
  279. }
  280. err = ioutil.WriteFile(sb.config.resolvConfPath, newRC.Content, 0644)
  281. if err != nil {
  282. return err
  283. }
  284. // write the new hash in a temp file and rename it to make the update atomic
  285. dir := path.Dir(sb.config.resolvConfPath)
  286. tmpHashFile, err := ioutil.TempFile(dir, "hash")
  287. if err != nil {
  288. return err
  289. }
  290. if err = tmpHashFile.Chmod(filePerm); err != nil {
  291. tmpHashFile.Close()
  292. return err
  293. }
  294. _, err = tmpHashFile.Write([]byte(newRC.Hash))
  295. if err1 := tmpHashFile.Close(); err == nil {
  296. err = err1
  297. }
  298. if err != nil {
  299. return err
  300. }
  301. return os.Rename(tmpHashFile.Name(), hashFile)
  302. }
  303. // Embedded DNS server has to be enabled for this sandbox. Rebuild the container's
  304. // resolv.conf by doing the following
  305. // - Add only the embedded server's IP to container's resolv.conf
  306. // - If the embedded server needs any resolv.conf options add it to the current list
  307. func (sb *sandbox) rebuildDNS() error {
  308. currRC, err := resolvconf.GetSpecific(sb.config.resolvConfPath)
  309. if err != nil {
  310. return err
  311. }
  312. if len(sb.extDNS) == 0 {
  313. sb.setExternalResolvers(currRC.Content, types.IPv4, false)
  314. }
  315. var (
  316. dnsList = []string{sb.resolver.NameServer()}
  317. dnsOptionsList = resolvconf.GetOptions(currRC.Content)
  318. dnsSearchList = resolvconf.GetSearchDomains(currRC.Content)
  319. )
  320. // external v6 DNS servers has to be listed in resolv.conf
  321. dnsList = append(dnsList, resolvconf.GetNameservers(currRC.Content, types.IPv6)...)
  322. // If the user config and embedded DNS server both have ndots option set,
  323. // remember the user's config so that unqualified names not in the docker
  324. // domain can be dropped.
  325. resOptions := sb.resolver.ResolverOptions()
  326. dnsOpt:
  327. for _, resOpt := range resOptions {
  328. if strings.Contains(resOpt, "ndots") {
  329. for _, option := range dnsOptionsList {
  330. if strings.Contains(option, "ndots") {
  331. parts := strings.Split(option, ":")
  332. if len(parts) != 2 {
  333. return fmt.Errorf("invalid ndots option %v", option)
  334. }
  335. if num, err := strconv.Atoi(parts[1]); err != nil {
  336. return fmt.Errorf("invalid number for ndots option: %v", parts[1])
  337. } else if num >= 0 {
  338. // if the user sets ndots, use the user setting
  339. sb.ndotsSet = true
  340. break dnsOpt
  341. } else {
  342. return fmt.Errorf("invalid number for ndots option: %v", num)
  343. }
  344. }
  345. }
  346. }
  347. }
  348. if !sb.ndotsSet {
  349. // if the user did not set the ndots, set it to 0 to prioritize the service name resolution
  350. // Ref: https://linux.die.net/man/5/resolv.conf
  351. dnsOptionsList = append(dnsOptionsList, resOptions...)
  352. }
  353. _, err = resolvconf.Build(sb.config.resolvConfPath, dnsList, dnsSearchList, dnsOptionsList)
  354. return err
  355. }
  356. func createBasePath(dir string) error {
  357. return os.MkdirAll(dir, dirPerm)
  358. }
  359. func createFile(path string) error {
  360. var f *os.File
  361. dir, _ := filepath.Split(path)
  362. err := createBasePath(dir)
  363. if err != nil {
  364. return err
  365. }
  366. f, err = os.Create(path)
  367. if err == nil {
  368. f.Close()
  369. }
  370. return err
  371. }
  372. func copyFile(src, dst string) error {
  373. sBytes, err := ioutil.ReadFile(src)
  374. if err != nil {
  375. return err
  376. }
  377. return ioutil.WriteFile(dst, sBytes, filePerm)
  378. }