sandbox_dns_unix.go 12 KB

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