python / python/cpython

socket.getfqdn() returns unqualifed hostname if the resolver is configured to trim local domains

Open
#100,298 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-socket type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

socket.getfqdn() is returning an unqualified address for my host. I tracked this down to the use of the trim keyword in /etc/host.conf the removal of which would cause other unrelated problems.

Looking at the source code for the system hostname command, they appear to call getaddrinfo with the service parameter set to NULL and pull out the ai_canonname field from the result. See: https://sources.debian.org/src/hostname/3.23/hostname.c lines 332-340 and 386 The python getaddrinfo doesn't especially help with this but perhaps this provides clues as to a more robust implementation.

Looking at Lib/socket.py, gethostname() actually is returning a fully qualified hostname and the gethostbyaddr() step is turning it back into an unqualified address. If this is hard to do portably, perhaps it could check for a dot in the values.

Your environment

Tested on a variety of RHEL based systems - Oracle Linux and CentOS Stream versions 8 and 9 both with distributed Python(s) and a build from source of the latest Python. Can also verify the issue by breaking down and tracing the source code in cpython/Lib/socket.py

/etc/host.conf on these systems includes the trim directive to remove the local domain from returned results.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Lib/socket.py, tracing getfqdn() through gethostname() and gethostbyaddr(); compare that behavior with the hostname command's getaddrinfo and ai_canonname usage. Reproduce the case with /etc/host.conf configured with trim, and consider the result complete when getfqdn() preserves the fully qualified hostname without requiring that directive to be removed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.