resolved-bus.c revision 7b9f7afcc04e80b77a2567b0750aa2cd03c1a1cd
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek This file is part of systemd.
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek Copyright 2014 Lennart Poettering
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek (at your option) any later version.
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include "resolved-dns-domain.h"
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poetteringstatic int reply_query_state(DnsQuery *q) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = in_addr_to_string(q->request_family, &q->request_address, &ip);
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers switch (q->state) {
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
0b6b6787e3f0ae8906ce0212bd629edbe931b73dKay Sievers return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_RESOURCES, "Not enough resources");
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek case DNS_TRANSACTION_INVALID_REPLY:
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", name);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek const char *rc, *n;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek char p[3]; /* the rcode is 4 bits long */
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek rc = dns_rcode_to_string(q->answer_rcode);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek sprintf(p, "%i", q->answer_rcode);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek n = strappenda(_BUS_ERROR_DNS, rc);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", name, rc);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek return sd_bus_reply_method_error(q->request, &error);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek assert_not_reached("Impossible state");
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmekstatic int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_message_open_container(reply, 'r', "iayi");
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (rr->key->type == DNS_TYPE_A) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_message_append(reply, "i", AF_INET);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek } else if (rr->key->type == DNS_TYPE_AAAA) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_message_append(reply, "i", AF_INET6);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_message_append(reply, "i", ifindex);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_message_close_container(reply);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmekstatic void bus_method_resolve_hostname_complete(DnsQuery *q) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL, *canonical = NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek unsigned added = 0, i;
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek if (q->state != DNS_TRANSACTION_SUCCESS) {
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_message_new_method_return(q->request, &reply);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_message_open_container(reply, 'a', "(iayi)");
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek answer = dns_answer_ref(q->answer);
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak r = dns_question_matches_rr(q->question, answer->rrs[i]);
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak if (r == 0) {
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak /* Hmm, if this is not an address record,
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak maybe it's a cname? If so, remember this */
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak r = dns_question_matches_cname(q->question, answer->rrs[i]);
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak r = append_address(reply, answer->rrs[i], ifindex);
3519d230c8bafe834b2dac26ace49fcfba139823Karel Zak canonical = dns_resource_record_ref(answer->rrs[i]);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of requested type", q->request_hostname);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek /* This has a cname? Then update the query with the
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = dns_query_cname_redirect(q, cname->cname.name);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop on '%s'", q->request_hostname);
a75f4e2a02e287294b21ae9e5b1f28b2f8faea39Zbigniew Jędrzejewski-Szmek r = sd_bus_reply_method_errno(q->request, -r, NULL);
d15d0333be6a1ca7fdd99a1881d967b6be8f387aZbigniew Jędrzejewski-Szmek /* Before we restart the query, let's see if any of
c5e04d51277994cca29234b33a6b8fc90a183cf3Zbigniew Jędrzejewski-Szmek * the RRs we already got already answers our query */
c5e04d51277994cca29234b33a6b8fc90a183cf3Zbigniew Jędrzejewski-Szmek for (i = 0; i < answer->n_rrs; i++) {
c5e04d51277994cca29234b33a6b8fc90a183cf3Zbigniew Jędrzejewski-Szmek r = dns_question_matches_rr(q->question, answer->rrs[i]);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering r = append_address(reply, answer->rrs[i], ifindex);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering canonical = dns_resource_record_ref(answer->rrs[i]);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering /* If we didn't find anything, then let's restart the
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering * query, this time with the cname */
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering r = sd_bus_reply_method_errno(q->request, -r, NULL);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering r = sd_bus_message_close_container(reply);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering /* Return the precise spelling and uppercasing reported by the server */
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering r = sd_bus_message_append(reply, "s", DNS_RESOURCE_KEY_NAME(canonical->key));
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering r = sd_bus_send(q->manager->bus, reply, NULL);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering log_error("Failed to send hostname reply: %s", strerror(-r));
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering sd_bus_reply_method_errno(q->request, -r, NULL);
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poetteringstatic int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
6550203eb471595e41e27f46e5d0a00a4c0e47bbLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
assert(m);
if (!question)
return -ENOMEM;
if (!key)
return -ENOMEM;
if (!key)
return -ENOMEM;
r = dns_query_go(q);
dns_query_free(q);
if (r == -ESRCH)
sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
unsigned added = 0, i;
assert(q);
r = reply_query_state(q);
goto finish;
goto finish;
goto finish;
if (q->answer) {
goto finish;
goto finish;
added ++;
if (added <= 0) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address '%s' does not have any RR of requested type", ip);
goto finish;
goto finish;
dns_query_free(q);
static int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
DnsQuery *q;
assert(m);
if (ifindex < 0)
if (!question)
return -ENOMEM;
if (!key)
return -ENOMEM;
r = dns_query_go(q);
dns_query_free(q);
if (r == -ESRCH)
sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
unsigned added = 0, i;
assert(q);
r = reply_query_state(q);
goto finish;
goto finish;
goto finish;
if (q->answer) {
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
added ++;
if (added <= 0) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", q->request_hostname);
goto finish;
goto finish;
dns_query_free(q);
static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
DnsQuery *q;
const char *name;
assert(m);
if (!question)
return -ENOMEM;
if (!key)
return -ENOMEM;
r = dns_query_go(q);
dns_query_free(q);
if (r == -ESRCH)
sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
SD_BUS_METHOD("ResolveHostname", "si", "a(iayi)s", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ResolveAddress", "iayi", "as", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ResolveRecord", "sqq", "a(qqay)", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED),
assert(s);
assert(m);
assert(m);
if (m->bus)
r = sd_event_add_time(m->event, &m->bus_retry_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 5*USEC_PER_SEC, 0, on_bus_retry, m);
r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable, m);