Skip to content

Commit 95d6516

Browse files
ldv-altkeszybz
authored andcommitted
resolved: fix the canonical name returned by hosts lookup by name
In etc_hosts_lookup_by_name(), return the canonical name of the resolved address instead of the name used to obtain that address. Resolves: #20158 (cherry picked from commit 1ddc2f7)
1 parent 51884cc commit 95d6516

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/resolve/resolved-etc-hosts.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,31 @@ static int answer_add_ptr(DnsAnswer *answer, DnsResourceKey *key, const char *na
394394
return dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL);
395395
}
396396

397+
static int answer_add_cname(DnsAnswer *answer, const char *name, const char *cname) {
398+
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
399+
400+
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, name);
401+
if (!rr)
402+
return -ENOMEM;
403+
404+
rr->cname.name = strdup(cname);
405+
if (!rr->cname.name)
406+
return -ENOMEM;
407+
408+
return dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL);
409+
}
410+
411+
static int answer_add_addr(DnsAnswer *answer, const char *name, const struct in_addr_data *a) {
412+
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
413+
int r;
414+
415+
r = dns_resource_record_new_address(&rr, a->family, &a->address, name);
416+
if (r < 0)
417+
return r;
418+
419+
return dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL);
420+
}
421+
397422
static int etc_hosts_lookup_by_address(
398423
EtcHosts *hosts,
399424
DnsQuestion *q,
@@ -509,17 +534,26 @@ static int etc_hosts_lookup_by_name(
509534
}
510535

511536
SET_FOREACH(a, item ? item->addresses : NULL) {
512-
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
537+
EtcHostsItemByAddress *item_by_addr;
538+
const char *canonical_name;
513539

514540
if ((!found_a && a->family == AF_INET) ||
515541
(!found_aaaa && a->family == AF_INET6))
516542
continue;
517543

518-
r = dns_resource_record_new_address(&rr, a->family, &a->address, item->name);
519-
if (r < 0)
520-
return r;
544+
item_by_addr = hashmap_get(hosts->by_address, a);
545+
if (item_by_addr && item_by_addr->canonical_name)
546+
canonical_name = item_by_addr->canonical_name;
547+
else
548+
canonical_name = item->name;
549+
550+
if (!streq(item->name, canonical_name)) {
551+
r = answer_add_cname(*answer, item->name, canonical_name);
552+
if (r < 0)
553+
return r;
554+
}
521555

522-
r = dns_answer_add(*answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL);
556+
r = answer_add_addr(*answer, canonical_name, a);
523557
if (r < 0)
524558
return r;
525559
}

test/units/testsuite-75.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,15 @@ run getent -s resolve hosts 127.128.0.5
302302
grep -qEx '127\.128\.0\.5\s+localhost5(\s+localhost5?\.localdomain[45]?){4}' "$RUN_OUT"
303303
[ "$(wc -l <"$RUN_OUT")" -eq 1 ]
304304

305+
# Issue: https://github.com/systemd/systemd/issues/20158
306+
run dig +noall +answer +additional localhost5.
307+
grep -qEx 'localhost5\.\s+0\s+IN\s+A\s+127\.128\.0\.5' "$RUN_OUT"
308+
[ "$(wc -l <"$RUN_OUT")" -eq 1 ]
309+
run dig +noall +answer +additional localhost5.localdomain4.
310+
grep -qEx 'localhost5\.localdomain4\.\s+0\s+IN\s+CNAME\s+localhost5\.' "$RUN_OUT"
311+
grep -qEx 'localhost5\.\s+0\s+IN\s+A\s+127\.128\.0\.5' "$RUN_OUT"
312+
[ "$(wc -l <"$RUN_OUT")" -eq 2 ]
313+
305314
: "--- Basic resolved tests ---"
306315
# Issue: https://github.com/systemd/systemd/issues/22229
307316
# PR: https://github.com/systemd/systemd/pull/22231

0 commit comments

Comments
 (0)