Skip to content

Commit 51884cc

Browse files
ldv-altkeszybz
authored andcommitted
resolved: fix the canonical name returned by hosts lookup by address
In etc_hosts_lookup_by_address(), make sure the canonical name of the given address is returned first in the list of names that address resolves to. Resolves: #25088 (cherry picked from commit 0ff8f2a)
1 parent 139cb31 commit 51884cc

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/resolve/resolved-etc-hosts.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,20 @@ static int manager_etc_hosts_read(Manager *m) {
380380
return 1;
381381
}
382382

383+
static int answer_add_ptr(DnsAnswer *answer, DnsResourceKey *key, const char *name) {
384+
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
385+
386+
rr = dns_resource_record_new(key);
387+
if (!rr)
388+
return -ENOMEM;
389+
390+
rr->ptr.name = strdup(name);
391+
if (!rr->ptr.name)
392+
return -ENOMEM;
393+
394+
return dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL);
395+
}
396+
383397
static int etc_hosts_lookup_by_address(
384398
EtcHosts *hosts,
385399
DnsQuestion *q,
@@ -426,18 +440,17 @@ static int etc_hosts_lookup_by_address(
426440
if (r < 0)
427441
return r;
428442

429-
SET_FOREACH(n, item->names) {
430-
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
431-
432-
rr = dns_resource_record_new(found_ptr);
433-
if (!rr)
434-
return -ENOMEM;
443+
if (item->canonical_name) {
444+
r = answer_add_ptr(*answer, found_ptr, item->canonical_name);
445+
if (r < 0)
446+
return r;
447+
}
435448

436-
rr->ptr.name = strdup(n);
437-
if (!rr->ptr.name)
438-
return -ENOMEM;
449+
SET_FOREACH(n, item->names) {
450+
if (n == item->canonical_name)
451+
continue;
439452

440-
r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL);
453+
r = answer_add_ptr(*answer, found_ptr, n);
441454
if (r < 0)
442455
return r;
443456
}

test/units/testsuite-75.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,12 @@ ip link del hoge.foo
162162
### SETUP ###
163163
# Configure network
164164
hostnamectl hostname ns1.unsigned.test
165-
{
166-
echo "10.0.0.1 ns1.unsigned.test"
167-
echo "fd00:dead:beef:cafe::1 ns1.unsigned.test"
168-
} >>/etc/hosts
165+
cat >>/etc/hosts <<EOF
166+
10.0.0.1 ns1.unsigned.test
167+
fd00:dead:beef:cafe::1 ns1.unsigned.test
168+
169+
127.128.0.5 localhost5 localhost5.localdomain localhost5.localdomain4 localhost.localdomain5 localhost5.localdomain5
170+
EOF
169171

170172
mkdir -p /etc/systemd/network
171173
cat >/etc/systemd/network/dns0.netdev <<EOF
@@ -295,6 +297,11 @@ run getent -s myhostname hosts localhost
295297
grep -qE "^127\.0\.0\.1\s+localhost" "$RUN_OUT"
296298
enable_ipv6
297299

300+
# Issue: https://github.com/systemd/systemd/issues/25088
301+
run getent -s resolve hosts 127.128.0.5
302+
grep -qEx '127\.128\.0\.5\s+localhost5(\s+localhost5?\.localdomain[45]?){4}' "$RUN_OUT"
303+
[ "$(wc -l <"$RUN_OUT")" -eq 1 ]
304+
298305
: "--- Basic resolved tests ---"
299306
# Issue: https://github.com/systemd/systemd/issues/22229
300307
# PR: https://github.com/systemd/systemd/pull/22231

0 commit comments

Comments
 (0)