libc resolver not stripping trailing dot for lookup in /etc/hosts

I want to make sure my expectation is correct before reporting this as a bug.

hostname(7) describes the libc resolver's operation (well, just the file-based / non-DNS part).

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, conforming to RFC 1123 section 6.1.4.3, the trailing dot is OK as input to the resolver, in order to designate the domain as absolute; 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.

Thus, /etc/hosts entries should not have the trailing dot, and a lookup of a name with the dot should match an /etc/hosts entry without the dot. But on FreeBSD, that's not actually what happens; to get a match, the /etc/hosts entry must have the 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.

Code:
# 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.)

In contrast, if you run this same test on Debian 3.2, it works as expected, resolving with and without the dot. So I propose that FreeBSD match this behavior and work as documented in hostname(7).
 
Back
Top