Source file src/net/dnsconfig_unix.go

     1  // Copyright 2009 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  //go:build !windows
     6  
     7  // Read system DNS config from /etc/resolv.conf
     8  
     9  package net
    10  
    11  import (
    12  	"internal/bytealg"
    13  	"net/netip"
    14  	"time"
    15  )
    16  
    17  // See resolv.conf(5) on a Linux machine.
    18  func dnsReadConfig(filename string) *dnsConfig {
    19  	conf := &dnsConfig{
    20  		ndots:    1,
    21  		timeout:  5 * time.Second,
    22  		attempts: 2,
    23  	}
    24  	file, err := open(filename)
    25  	if err != nil {
    26  		conf.servers = defaultNS
    27  		conf.search = dnsDefaultSearch()
    28  		conf.err = err
    29  		return conf
    30  	}
    31  	defer file.close()
    32  	if fi, err := file.file.Stat(); err == nil {
    33  		conf.mtime = fi.ModTime()
    34  	} else {
    35  		conf.servers = defaultNS
    36  		conf.search = dnsDefaultSearch()
    37  		conf.err = err
    38  		return conf
    39  	}
    40  	for line, ok := file.readLine(); ok; line, ok = file.readLine() {
    41  		if len(line) > 0 && (line[0] == ';' || line[0] == '#') {
    42  			// comment.
    43  			continue
    44  		}
    45  		f := getFields(line)
    46  		if len(f) < 1 {
    47  			continue
    48  		}
    49  		switch f[0] {
    50  		case "nameserver": // add one name server
    51  			if len(f) > 1 && len(conf.servers) < 3 { // small, but the standard limit
    52  				// One more check: make sure server name is
    53  				// just an IP address. Otherwise we need DNS
    54  				// to look it up.
    55  				if _, err := netip.ParseAddr(f[1]); err == nil {
    56  					conf.servers = append(conf.servers, JoinHostPort(f[1], "53"))
    57  				}
    58  			}
    59  
    60  		case "domain": // set search path to just this domain
    61  			if len(f) > 1 {
    62  				conf.search = []string{ensureRooted(f[1])}
    63  			}
    64  
    65  		case "search": // set search path to given servers
    66  			conf.search = make([]string, 0, len(f)-1)
    67  			for i := 1; i < len(f); i++ {
    68  				name := ensureRooted(f[i])
    69  				if name == "." {
    70  					continue
    71  				}
    72  				conf.search = append(conf.search, name)
    73  			}
    74  
    75  		case "options": // magic options
    76  			for _, s := range f[1:] {
    77  				switch {
    78  				case hasPrefix(s, "ndots:"):
    79  					n, _, _ := dtoi(s[6:])
    80  					if n < 0 {
    81  						n = 0
    82  					} else if n > 15 {
    83  						n = 15
    84  					}
    85  					conf.ndots = n
    86  				case hasPrefix(s, "timeout:"):
    87  					n, _, _ := dtoi(s[8:])
    88  					if n < 1 {
    89  						n = 1
    90  					}
    91  					conf.timeout = time.Duration(n) * time.Second
    92  				case hasPrefix(s, "attempts:"):
    93  					n, _, _ := dtoi(s[9:])
    94  					if n < 1 {
    95  						n = 1
    96  					}
    97  					conf.attempts = n
    98  				case s == "rotate":
    99  					conf.rotate = true
   100  				case s == "single-request" || s == "single-request-reopen":
   101  					// Linux option:
   102  					// http://man7.org/linux/man-pages/man5/resolv.conf.5.html
   103  					// "By default, glibc performs IPv4 and IPv6 lookups in parallel [...]
   104  					//  This option disables the behavior and makes glibc
   105  					//  perform the IPv6 and IPv4 requests sequentially."
   106  					conf.singleRequest = true
   107  				case s == "use-vc" || s == "usevc" || s == "tcp":
   108  					// Linux (use-vc), FreeBSD (usevc) and OpenBSD (tcp) option:
   109  					// http://man7.org/linux/man-pages/man5/resolv.conf.5.html
   110  					// "Sets RES_USEVC in _res.options.
   111  					//  This option forces the use of TCP for DNS resolutions."
   112  					// https://www.freebsd.org/cgi/man.cgi?query=resolv.conf&sektion=5&manpath=freebsd-release-ports
   113  					// https://man.openbsd.org/resolv.conf.5
   114  					conf.useTCP = true
   115  				case s == "trust-ad":
   116  					conf.trustAD = true
   117  				case s == "edns0":
   118  					// We use EDNS by default.
   119  					// Ignore this option.
   120  				case s == "no-reload":
   121  					conf.noReload = true
   122  				default:
   123  					conf.unknownOpt = true
   124  				}
   125  			}
   126  
   127  		case "lookup":
   128  			// OpenBSD option:
   129  			// https://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man5/resolv.conf.5
   130  			// "the legal space-separated values are: bind, file, yp"
   131  			conf.lookup = f[1:]
   132  
   133  		default:
   134  			conf.unknownOpt = true
   135  		}
   136  	}
   137  	if len(conf.servers) == 0 {
   138  		conf.servers = defaultNS
   139  	}
   140  	if len(conf.search) == 0 {
   141  		conf.search = dnsDefaultSearch()
   142  	}
   143  	return conf
   144  }
   145  
   146  func dnsDefaultSearch() []string {
   147  	hn, err := getHostname()
   148  	if err != nil {
   149  		// best effort
   150  		return nil
   151  	}
   152  	if i := bytealg.IndexByteString(hn, '.'); i >= 0 && i < len(hn)-1 {
   153  		return []string{ensureRooted(hn[i+1:])}
   154  	}
   155  	return nil
   156  }
   157  
   158  func hasPrefix(s, prefix string) bool {
   159  	return len(s) >= len(prefix) && s[:len(prefix)] == prefix
   160  }
   161  
   162  func ensureRooted(s string) string {
   163  	if len(s) > 0 && s[len(s)-1] == '.' {
   164  		return s
   165  	}
   166  	return s + "."
   167  }
   168  

View as plain text