Bug 204927

Summary: libc resolver not stripping trailing dot for lookup in /etc/hosts
Product: Base System Reporter: mike
Component: miscAssignee: freebsd-bugs (Nobody) <bugs>
Status: New ---    
Severity: Affects Only Me    
Priority: ---    
Version: 10.2-STABLE   
Hardware: Any   
OS: Any   

Description mike 2015-12-01 02:55:46 UTC
The manpage for hostname(7) describes the file-based, non-DNS part of the libc resolver's operation. It begins by defining a hostname as a domain, i.e. "a dot-separated list of subdomains", an example for which is given on FreeBSD's version of this page with the explicit note "with no trailing dot".

The manpage goes on to say: "If the input name ends with a trailing dot, the trailing dot is removed, and the remaining name is looked up with no further processing."

In other words, the trailing dot is OK as input to the resolver, in order to designate the domain as absolute.  In this case, the name is just looked up in the /etc/hosts table as a literal string without the dot, without trying variations with appended search domains. This conforms to RFC 1123 section 6.1.4.3.

So, it seems reasonable to say that 1. /etc/hosts entries should not have the trailing dot, and 2. a lookup of a name with the dot should match an /etc/hosts entry without the dot.

On FreeBSD, this is not actually what happens; you must have a trailing dot in /etc/hosts in order to match a lookup given with the trailing dot.

Below is a demonstration on FreeBSD (tested on 8.4 and 10.2) which assumes /etc/nsswitch.conf has its normal "host: files dns" line, and /etc/hosts has an entry for "localhost.my.domain" without the trailing dot.

# fetch http://beej.us/guide/bgnet/examples/showip.c
# cc -o showip showip.c

# ./showip localhost.my.domain.
getaddrinfo: hostname nor servname provided, or not known

# ./showip localhost.my.domain
IP addresses for localhost.my.domain:

  IPv4: 127.0.0.1
  IPv6: ::1

showip.c is from Beej's Guide to Network Programming Using Internet Sockets. It just does a getaddrinfo() call on the given name.

I got the above results when testing on 8.4-RELEASE and 10.2-STABLE. When I ran the same test on Debian 3.2, it worked as expected, resolving with and without the dot. So I propose that FreeBSD match this behavior and work as documented in hostname(7).