Source file src/net/conf.go

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"errors"
     9  	"internal/bytealg"
    10  	"internal/godebug"
    11  	"io/fs"
    12  	"os"
    13  	"runtime"
    14  	"sync"
    15  	"syscall"
    16  )
    17  
    18  // The net package's name resolution is rather complicated.
    19  // There are two main approaches, go and cgo.
    20  // The cgo resolver uses C functions like getaddrinfo.
    21  // The go resolver reads system files directly and
    22  // sends DNS packets directly to servers.
    23  //
    24  // The netgo build tag prefers the go resolver.
    25  // The netcgo build tag prefers the cgo resolver.
    26  //
    27  // The netgo build tag also prohibits the use of the cgo tool.
    28  // However, on Darwin, Plan 9, and Windows the cgo resolver is still available.
    29  // On those systems the cgo resolver does not require the cgo tool.
    30  // (The term "cgo resolver" was locked in by GODEBUG settings
    31  // at a time when the cgo resolver did require the cgo tool.)
    32  //
    33  // Adding netdns=go to GODEBUG will prefer the go resolver.
    34  // Adding netdns=cgo to GODEBUG will prefer the cgo resolver.
    35  //
    36  // The Resolver struct has a PreferGo field that user code
    37  // may set to prefer the go resolver. It is documented as being
    38  // equivalent to adding netdns=go to GODEBUG.
    39  //
    40  // When deciding which resolver to use, we first check the PreferGo field.
    41  // If that is not set, we check the GODEBUG setting.
    42  // If that is not set, we check the netgo or netcgo build tag.
    43  // If none of those are set, we normally prefer the go resolver by default.
    44  // However, if the cgo resolver is available,
    45  // there is a complex set of conditions for which we prefer the cgo resolver.
    46  //
    47  // Other files define the netGoBuildTag, netCgoBuildTag, and cgoAvailable
    48  // constants.
    49  
    50  // conf is used to determine name resolution configuration.
    51  type conf struct {
    52  	netGo  bool // prefer go approach, based on build tag and GODEBUG
    53  	netCgo bool // prefer cgo approach, based on build tag and GODEBUG
    54  
    55  	dnsDebugLevel int // from GODEBUG
    56  
    57  	preferCgo bool // if no explicit preference, use cgo
    58  
    59  	goos     string   // copy of runtime.GOOS, used for testing
    60  	mdnsTest mdnsTest // assume /etc/mdns.allow exists, for testing
    61  }
    62  
    63  // mdnsTest is for testing only.
    64  type mdnsTest int
    65  
    66  const (
    67  	mdnsFromSystem mdnsTest = iota
    68  	mdnsAssumeExists
    69  	mdnsAssumeDoesNotExist
    70  )
    71  
    72  var (
    73  	confOnce sync.Once // guards init of confVal via initConfVal
    74  	confVal  = &conf{goos: runtime.GOOS}
    75  )
    76  
    77  // systemConf returns the machine's network configuration.
    78  func systemConf() *conf {
    79  	confOnce.Do(initConfVal)
    80  	return confVal
    81  }
    82  
    83  // initConfVal initializes confVal based on the environment
    84  // that will not change during program execution.
    85  func initConfVal() {
    86  	dnsMode, debugLevel := goDebugNetDNS()
    87  	confVal.netGo = netGoBuildTag || dnsMode == "go"
    88  	confVal.netCgo = netCgoBuildTag || dnsMode == "cgo"
    89  	confVal.dnsDebugLevel = debugLevel
    90  
    91  	if confVal.dnsDebugLevel > 0 {
    92  		defer func() {
    93  			if confVal.dnsDebugLevel > 1 {
    94  				println("go package net: confVal.netCgo =", confVal.netCgo, " netGo =", confVal.netGo)
    95  			}
    96  			switch {
    97  			case confVal.netGo:
    98  				if netGoBuildTag {
    99  					println("go package net: built with netgo build tag; using Go's DNS resolver")
   100  				} else {
   101  					println("go package net: GODEBUG setting forcing use of Go's resolver")
   102  				}
   103  			case !cgoAvailable:
   104  				println("go package net: cgo resolver not supported; using Go's DNS resolver")
   105  			case confVal.netCgo || confVal.preferCgo:
   106  				println("go package net: using cgo DNS resolver")
   107  			default:
   108  				println("go package net: dynamic selection of DNS resolver")
   109  			}
   110  		}()
   111  	}
   112  
   113  	// The remainder of this function sets preferCgo based on
   114  	// conditions that will not change during program execution.
   115  
   116  	// By default, prefer the go resolver.
   117  	confVal.preferCgo = false
   118  
   119  	// If the cgo resolver is not available, we can't prefer it.
   120  	if !cgoAvailable {
   121  		return
   122  	}
   123  
   124  	// Some operating systems always prefer the cgo resolver.
   125  	if goosPrefersCgo() {
   126  		confVal.preferCgo = true
   127  		return
   128  	}
   129  
   130  	// The remaining checks are specific to Unix systems.
   131  	switch runtime.GOOS {
   132  	case "plan9", "windows", "js", "wasip1":
   133  		return
   134  	}
   135  
   136  	// If any environment-specified resolver options are specified,
   137  	// prefer the cgo resolver.
   138  	// Note that LOCALDOMAIN can change behavior merely by being
   139  	// specified with the empty string.
   140  	_, localDomainDefined := syscall.Getenv("LOCALDOMAIN")
   141  	if localDomainDefined || os.Getenv("RES_OPTIONS") != "" || os.Getenv("HOSTALIASES") != "" {
   142  		confVal.preferCgo = true
   143  		return
   144  	}
   145  
   146  	// OpenBSD apparently lets you override the location of resolv.conf
   147  	// with ASR_CONFIG. If we notice that, defer to libc.
   148  	if runtime.GOOS == "openbsd" && os.Getenv("ASR_CONFIG") != "" {
   149  		confVal.preferCgo = true
   150  		return
   151  	}
   152  }
   153  
   154  // goosPrefersCgo reports whether the GOOS value passed in prefers
   155  // the cgo resolver.
   156  func goosPrefersCgo() bool {
   157  	switch runtime.GOOS {
   158  	// Historically on Windows and Plan 9 we prefer the
   159  	// cgo resolver (which doesn't use the cgo tool) rather than
   160  	// the go resolver. This is because originally these
   161  	// systems did not support the go resolver.
   162  	// Keep it this way for better compatibility.
   163  	// Perhaps we can revisit this some day.
   164  	case "windows", "plan9":
   165  		return true
   166  
   167  	// Darwin pops up annoying dialog boxes if programs try to
   168  	// do their own DNS requests, so prefer cgo.
   169  	case "darwin", "ios":
   170  		return true
   171  
   172  	// DNS requests don't work on Android, so prefer the cgo resolver.
   173  	// Issue #10714.
   174  	case "android":
   175  		return true
   176  
   177  	default:
   178  		return false
   179  	}
   180  }
   181  
   182  // mustUseGoResolver reports whether a DNS lookup of any sort is
   183  // required to use the go resolver. The provided Resolver is optional.
   184  // This will report true if the cgo resolver is not available.
   185  func (c *conf) mustUseGoResolver(r *Resolver) bool {
   186  	if !cgoAvailable {
   187  		return true
   188  	}
   189  
   190  	if runtime.GOOS == "plan9" {
   191  		// TODO(bradfitz): for now we only permit use of the PreferGo
   192  		// implementation when there's a non-nil Resolver with a
   193  		// non-nil Dialer. This is a sign that the code is trying
   194  		// to use their DNS-speaking net.Conn (such as an in-memory
   195  		// DNS cache) and they don't want to actually hit the network.
   196  		// Once we add support for looking the default DNS servers
   197  		// from plan9, though, then we can relax this.
   198  		if r == nil || r.Dial == nil {
   199  			return false
   200  		}
   201  	}
   202  
   203  	return c.netGo || r.preferGo()
   204  }
   205  
   206  // addrLookupOrder determines which strategy to use to resolve addresses.
   207  // The provided Resolver is optional. nil means to not consider its options.
   208  // It also returns dnsConfig when it was used to determine the lookup order.
   209  func (c *conf) addrLookupOrder(r *Resolver, addr string) (ret hostLookupOrder, dnsConf *dnsConfig) {
   210  	if c.dnsDebugLevel > 1 {
   211  		defer func() {
   212  			print("go package net: addrLookupOrder(", addr, ") = ", ret.String(), "\n")
   213  		}()
   214  	}
   215  	return c.lookupOrder(r, "")
   216  }
   217  
   218  // hostLookupOrder determines which strategy to use to resolve hostname.
   219  // The provided Resolver is optional. nil means to not consider its options.
   220  // It also returns dnsConfig when it was used to determine the lookup order.
   221  func (c *conf) hostLookupOrder(r *Resolver, hostname string) (ret hostLookupOrder, dnsConf *dnsConfig) {
   222  	if c.dnsDebugLevel > 1 {
   223  		defer func() {
   224  			print("go package net: hostLookupOrder(", hostname, ") = ", ret.String(), "\n")
   225  		}()
   226  	}
   227  	return c.lookupOrder(r, hostname)
   228  }
   229  
   230  func (c *conf) lookupOrder(r *Resolver, hostname string) (ret hostLookupOrder, dnsConf *dnsConfig) {
   231  	// fallbackOrder is the order we return if we can't figure it out.
   232  	var fallbackOrder hostLookupOrder
   233  
   234  	var canUseCgo bool
   235  	if c.mustUseGoResolver(r) {
   236  		// Go resolver was explicitly requested
   237  		// or cgo resolver is not available.
   238  		// Figure out the order below.
   239  		fallbackOrder = hostLookupFilesDNS
   240  		canUseCgo = false
   241  	} else if c.netCgo {
   242  		// Cgo resolver was explicitly requested.
   243  		return hostLookupCgo, nil
   244  	} else if c.preferCgo {
   245  		// Given a choice, we prefer the cgo resolver.
   246  		return hostLookupCgo, nil
   247  	} else {
   248  		// Neither resolver was explicitly requested
   249  		// and we have no preference.
   250  
   251  		if bytealg.IndexByteString(hostname, '\\') != -1 || bytealg.IndexByteString(hostname, '%') != -1 {
   252  			// Don't deal with special form hostnames
   253  			// with backslashes or '%'.
   254  			return hostLookupCgo, nil
   255  		}
   256  
   257  		// If something is unrecognized, use cgo.
   258  		fallbackOrder = hostLookupCgo
   259  		canUseCgo = true
   260  	}
   261  
   262  	// On systems that don't use /etc/resolv.conf or /etc/nsswitch.conf, we are done.
   263  	switch c.goos {
   264  	case "windows", "plan9", "android", "ios":
   265  		return fallbackOrder, nil
   266  	}
   267  
   268  	// Try to figure out the order to use for searches.
   269  	// If we don't recognize something, use fallbackOrder.
   270  	// That will use cgo unless the Go resolver was explicitly requested.
   271  	// If we do figure out the order, return something other
   272  	// than fallbackOrder to use the Go resolver with that order.
   273  
   274  	dnsConf = getSystemDNSConfig()
   275  
   276  	if canUseCgo && dnsConf.err != nil && !errors.Is(dnsConf.err, fs.ErrNotExist) && !errors.Is(dnsConf.err, fs.ErrPermission) {
   277  		// We can't read the resolv.conf file, so use cgo if we can.
   278  		return hostLookupCgo, dnsConf
   279  	}
   280  
   281  	if canUseCgo && dnsConf.unknownOpt {
   282  		// We didn't recognize something in resolv.conf,
   283  		// so use cgo if we can.
   284  		return hostLookupCgo, dnsConf
   285  	}
   286  
   287  	// OpenBSD is unique and doesn't use nsswitch.conf.
   288  	// It also doesn't support mDNS.
   289  	if c.goos == "openbsd" {
   290  		// OpenBSD's resolv.conf manpage says that a
   291  		// non-existent resolv.conf means "lookup" defaults
   292  		// to only "files", without DNS lookups.
   293  		if errors.Is(dnsConf.err, fs.ErrNotExist) {
   294  			return hostLookupFiles, dnsConf
   295  		}
   296  
   297  		lookup := dnsConf.lookup
   298  		if len(lookup) == 0 {
   299  			// https://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man5/resolv.conf.5
   300  			// "If the lookup keyword is not used in the
   301  			// system's resolv.conf file then the assumed
   302  			// order is 'bind file'"
   303  			return hostLookupDNSFiles, dnsConf
   304  		}
   305  		if len(lookup) < 1 || len(lookup) > 2 {
   306  			// We don't recognize this format.
   307  			return fallbackOrder, dnsConf
   308  		}
   309  		switch lookup[0] {
   310  		case "bind":
   311  			if len(lookup) == 2 {
   312  				if lookup[1] == "file" {
   313  					return hostLookupDNSFiles, dnsConf
   314  				}
   315  				// Unrecognized.
   316  				return fallbackOrder, dnsConf
   317  			}
   318  			return hostLookupDNS, dnsConf
   319  		case "file":
   320  			if len(lookup) == 2 {
   321  				if lookup[1] == "bind" {
   322  					return hostLookupFilesDNS, dnsConf
   323  				}
   324  				// Unrecognized.
   325  				return fallbackOrder, dnsConf
   326  			}
   327  			return hostLookupFiles, dnsConf
   328  		default:
   329  			// Unrecognized.
   330  			return fallbackOrder, dnsConf
   331  		}
   332  
   333  		// We always return before this point.
   334  		// The code below is for non-OpenBSD.
   335  	}
   336  
   337  	// Canonicalize the hostname by removing any trailing dot.
   338  	if stringsHasSuffix(hostname, ".") {
   339  		hostname = hostname[:len(hostname)-1]
   340  	}
   341  
   342  	nss := getSystemNSS()
   343  	srcs := nss.sources["hosts"]
   344  	// If /etc/nsswitch.conf doesn't exist or doesn't specify any
   345  	// sources for "hosts", assume Go's DNS will work fine.
   346  	if errors.Is(nss.err, fs.ErrNotExist) || (nss.err == nil && len(srcs) == 0) {
   347  		if canUseCgo && c.goos == "solaris" {
   348  			// illumos defaults to
   349  			// "nis [NOTFOUND=return] files",
   350  			// which the go resolver doesn't support.
   351  			return hostLookupCgo, dnsConf
   352  		}
   353  
   354  		return hostLookupFilesDNS, dnsConf
   355  	}
   356  	if nss.err != nil {
   357  		// We failed to parse or open nsswitch.conf, so
   358  		// we have nothing to base an order on.
   359  		return fallbackOrder, dnsConf
   360  	}
   361  
   362  	var hasDNSSource bool
   363  	var hasDNSSourceChecked bool
   364  
   365  	var filesSource, dnsSource bool
   366  	var first string
   367  	for i, src := range srcs {
   368  		if src.source == "files" || src.source == "dns" {
   369  			if canUseCgo && !src.standardCriteria() {
   370  				// non-standard; let libc deal with it.
   371  				return hostLookupCgo, dnsConf
   372  			}
   373  			if src.source == "files" {
   374  				filesSource = true
   375  			} else {
   376  				hasDNSSource = true
   377  				hasDNSSourceChecked = true
   378  				dnsSource = true
   379  			}
   380  			if first == "" {
   381  				first = src.source
   382  			}
   383  			continue
   384  		}
   385  
   386  		if canUseCgo {
   387  			switch {
   388  			case hostname != "" && src.source == "myhostname":
   389  				// Let the cgo resolver handle myhostname
   390  				// if we are looking up the local hostname.
   391  				if isLocalhost(hostname) || isGateway(hostname) || isOutbound(hostname) {
   392  					return hostLookupCgo, dnsConf
   393  				}
   394  				hn, err := getHostname()
   395  				if err != nil || stringsEqualFold(hostname, hn) {
   396  					return hostLookupCgo, dnsConf
   397  				}
   398  				continue
   399  			case hostname != "" && stringsHasPrefix(src.source, "mdns"):
   400  				if stringsHasSuffixFold(hostname, ".local") {
   401  					// Per RFC 6762, the ".local" TLD is special. And
   402  					// because Go's native resolver doesn't do mDNS or
   403  					// similar local resolution mechanisms, assume that
   404  					// libc might (via Avahi, etc) and use cgo.
   405  					return hostLookupCgo, dnsConf
   406  				}
   407  
   408  				// We don't parse mdns.allow files. They're rare. If one
   409  				// exists, it might list other TLDs (besides .local) or even
   410  				// '*', so just let libc deal with it.
   411  				var haveMDNSAllow bool
   412  				switch c.mdnsTest {
   413  				case mdnsFromSystem:
   414  					_, err := os.Stat("/etc/mdns.allow")
   415  					if err != nil && !errors.Is(err, fs.ErrNotExist) {
   416  						// Let libc figure out what is going on.
   417  						return hostLookupCgo, dnsConf
   418  					}
   419  					haveMDNSAllow = err == nil
   420  				case mdnsAssumeExists:
   421  					haveMDNSAllow = true
   422  				case mdnsAssumeDoesNotExist:
   423  					haveMDNSAllow = false
   424  				}
   425  				if haveMDNSAllow {
   426  					return hostLookupCgo, dnsConf
   427  				}
   428  				continue
   429  			default:
   430  				// Some source we don't know how to deal with.
   431  				return hostLookupCgo, dnsConf
   432  			}
   433  		}
   434  
   435  		if !hasDNSSourceChecked {
   436  			hasDNSSourceChecked = true
   437  			for _, v := range srcs[i+1:] {
   438  				if v.source == "dns" {
   439  					hasDNSSource = true
   440  					break
   441  				}
   442  			}
   443  		}
   444  
   445  		// If we saw a source we don't recognize, which can only
   446  		// happen if we can't use the cgo resolver, treat it as DNS,
   447  		// but only when there is no dns in all other sources.
   448  		if !hasDNSSource {
   449  			dnsSource = true
   450  			if first == "" {
   451  				first = "dns"
   452  			}
   453  		}
   454  	}
   455  
   456  	// Cases where Go can handle it without cgo and C thread overhead,
   457  	// or where the Go resolver has been forced.
   458  	switch {
   459  	case filesSource && dnsSource:
   460  		if first == "files" {
   461  			return hostLookupFilesDNS, dnsConf
   462  		} else {
   463  			return hostLookupDNSFiles, dnsConf
   464  		}
   465  	case filesSource:
   466  		return hostLookupFiles, dnsConf
   467  	case dnsSource:
   468  		return hostLookupDNS, dnsConf
   469  	}
   470  
   471  	// Something weird. Fallback to the default.
   472  	return fallbackOrder, dnsConf
   473  }
   474  
   475  var netdns = godebug.New("netdns")
   476  
   477  // goDebugNetDNS parses the value of the GODEBUG "netdns" value.
   478  // The netdns value can be of the form:
   479  //
   480  //	1       // debug level 1
   481  //	2       // debug level 2
   482  //	cgo     // use cgo for DNS lookups
   483  //	go      // use go for DNS lookups
   484  //	cgo+1   // use cgo for DNS lookups + debug level 1
   485  //	1+cgo   // same
   486  //	cgo+2   // same, but debug level 2
   487  //
   488  // etc.
   489  func goDebugNetDNS() (dnsMode string, debugLevel int) {
   490  	goDebug := netdns.Value()
   491  	parsePart := func(s string) {
   492  		if s == "" {
   493  			return
   494  		}
   495  		if '0' <= s[0] && s[0] <= '9' {
   496  			debugLevel, _, _ = dtoi(s)
   497  		} else {
   498  			dnsMode = s
   499  		}
   500  	}
   501  	if i := bytealg.IndexByteString(goDebug, '+'); i != -1 {
   502  		parsePart(goDebug[:i])
   503  		parsePart(goDebug[i+1:])
   504  		return
   505  	}
   506  	parsePart(goDebug)
   507  	return
   508  }
   509  
   510  // isLocalhost reports whether h should be considered a "localhost"
   511  // name for the myhostname NSS module.
   512  func isLocalhost(h string) bool {
   513  	return stringsEqualFold(h, "localhost") || stringsEqualFold(h, "localhost.localdomain") || stringsHasSuffixFold(h, ".localhost") || stringsHasSuffixFold(h, ".localhost.localdomain")
   514  }
   515  
   516  // isGateway reports whether h should be considered a "gateway"
   517  // name for the myhostname NSS module.
   518  func isGateway(h string) bool {
   519  	return stringsEqualFold(h, "_gateway")
   520  }
   521  
   522  // isOutbound reports whether h should be considered an "outbound"
   523  // name for the myhostname NSS module.
   524  func isOutbound(h string) bool {
   525  	return stringsEqualFold(h, "_outbound")
   526  }
   527  

View as plain text