resolved-bus.c revision 3347dd5ea1dba5e27a677954b505bb6a2ee6cd75
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringstatic int reply_query_state(DnsQuery *q) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_RESOURCES, "Not enough resources");
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_ABORTED, "Query aborted");
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_DNSSEC_FAILED, "DNSSEC validation failed: %s",
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering dnssec_result_to_string(q->answer_dnssec_result));
e0240c64b76ba8f0c9219feb23a5783f23100216Lennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_TRUST_ANCHOR, "No suitable trust anchor known");
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED:
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_RR_TYPE_UNSUPPORTED, "Server does not support requested resource record type");
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NETWORK_DOWN, "Network is down");
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", dns_query_string(q));
6f717d0817573a76c3e586eae02793d8b23a0581Lennart Poettering const char *rc, *n;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering char p[DECIMAL_STR_MAX(q->answer_rcode)];
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering rc = dns_rcode_to_string(q->answer_rcode);
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", dns_query_string(q), rc);
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return sd_bus_reply_method_error(q->request, &error);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringstatic int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_bus_message_open_container(reply, 'r', "iiay");
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering r = sd_bus_message_append(reply, "i", ifindex);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_bus_message_append(reply, "i", AF_INET);
d75acfb059ece4512278b8820a9103664996f1e5Lennart Poettering r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering } else if (rr->key->type == DNS_TYPE_AAAA) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = sd_bus_message_append(reply, "i", AF_INET6);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_bus_message_close_container(reply);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void bus_method_resolve_hostname_complete(DnsQuery *q) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (q->state != DNS_TRANSACTION_SUCCESS) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (r == DNS_QUERY_RESTARTED) /* This was a cname, and the query was restarted. */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = sd_bus_message_new_method_return(q->request, &reply);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = sd_bus_message_open_container(reply, 'a', "(iiay)");
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen question = dns_query_question_for_protocol(q, q->answer_protocol);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen r = dns_question_matches_rr(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = sd_bus_message_close_container(reply);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek /* Return the precise spelling and uppercasing and CNAME target reported by the server */
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek DNS_RESOURCE_KEY_NAME(canonical->key),
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = sd_bus_send(q->manager->bus, reply, NULL);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_error_errno(r, "Failed to send hostname reply: %m");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen sd_bus_reply_method_errno(q->request, r, NULL);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenstatic int check_ifindex_flags(int ifindex, uint64_t *flags, uint64_t ok, sd_bus_error *error) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (*flags & ~(SD_RESOLVED_PROTOCOLS_ALL|SD_RESOLVED_NO_CNAME|ok))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if ((*flags & SD_RESOLVED_PROTOCOLS_ALL) == 0) /* If no protocol is enabled, enable all */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_cc(sizeof(int) == sizeof(int32_t));
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
7778dffff3d8bd7438fe19a248c16203668324c9Daniel Mack return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_SEARCH, error);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = dns_question_new_address(&question_utf8, family, hostname, false);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = dns_question_new_address(&question_idna, family, hostname, true);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = dns_query_new(m, &q, question_utf8, question_idna, ifindex, flags);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering q->request = sd_bus_message_ref(message);
goto fail;
r = dns_query_go(q);
goto fail;
fail:
dns_query_free(q);
unsigned added = 0;
int ifindex, r;
assert(q);
r = reply_query_state(q);
goto finish;
r = dns_query_process_cname(q);
if (r == -ELOOP) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
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, "Address '%s' does not have any RR of requested type", strna(ip));
goto finish;
goto finish;
r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
goto finish;
dns_query_free(q);
static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
DnsQuery *q;
assert(m);
goto fail;
r = dns_query_go(q);
goto fail;
fail:
dns_query_free(q);
assert(m);
return sd_bus_message_close_container(m);
unsigned added = 0;
int ifindex;
assert(q);
r = reply_query_state(q);
goto finish;
r = dns_query_process_cname(q);
if (r == -ELOOP) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
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", dns_query_string(q));
goto finish;
goto finish;
r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
goto finish;
dns_query_free(q);
static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
int r, ifindex;
DnsQuery *q;
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified resource record type %" PRIu16 " may not be used in a query.", type);
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
if (!question)
return -ENOMEM;
if (!key)
return -ENOMEM;
goto fail;
r = dns_query_go(q);
goto fail;
fail:
dns_query_free(q);
assert(q);
if (canonical)
if (!canonical)
int ifindex;
r = sd_bus_message_append(reply, "s", canonical ? DNS_RESOURCE_KEY_NAME(canonical->key) : rr->srv.name);
DnsTxtItem *i;
if (i->length <= 0)
unsigned added = 0;
assert(q);
if (q->block_all_complete > 0)
bool have_success = false;
case DNS_TRANSACTION_PENDING:
case DNS_TRANSACTION_SUCCESS:
have_success = true;
if (!have_success) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad));
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
if (!canonical)
added++;
if (added <= 0) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
dns_query_free(q);
assert(q);
r = dns_query_process_cname(q);
q->auxiliary_result = r;
assert(q);
if (r == -EAGAIN) {
goto fail;
goto fail;
fail:
bool has_root_domain = false;
unsigned found = 0;
int ifindex, r;
assert(q);
r = reply_query_state(q);
goto finish;
r = dns_query_process_cname(q);
if (r == -ELOOP) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
goto finish;
goto finish;
goto finish;
has_root_domain = true;
q->block_all_complete ++;
q->block_all_complete --;
goto finish;
found++;
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide the requested service", dns_query_string(q));
goto finish;
if (found <= 0) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
goto finish;
dns_query_free(q);
static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
DnsQuery *q;
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type.");
r = dns_question_new_service(&question_utf8, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), false);
r = dns_question_new_service(&question_idna, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), true);
goto fail;
r = dns_query_go(q);
goto fail;
fail:
dns_query_free(q);
assert(s);
if (with_ifindex) {
static int bus_property_get_dns_servers(
const char *path,
const char *interface,
const char *property,
void *userdata,
DnsServer *s;
Iterator i;
Link *l;
assert(m);
static int bus_property_get_search_domains(
const char *path,
const char *interface,
const char *property,
void *userdata,
DnsSearchDomain *d;
Iterator i;
Link *l;
assert(m);
static int bus_property_get_transaction_statistics(
const char *path,
const char *interface,
const char *property,
void *userdata,
assert(m);
static int bus_property_get_cache_statistics(
const char *path,
const char *interface,
const char *property,
void *userdata,
DnsScope *s;
assert(m);
static int bus_property_get_dnssec_statistics(
const char *path,
const char *interface,
const char *property,
void *userdata,
assert(m);
static int bus_property_get_dnssec_supported(
const char *path,
const char *interface,
const char *property,
void *userdata,
assert(m);
static int bus_method_reset_statistics(sd_bus_message *message, void *userdata, sd_bus_error *error) {
DnsScope *s;
assert(m);
m->n_transactions_total = 0;
Link *l;
assert(m);
if (ifindex <= 0)
*ret = l;
Link *l;
assert(m);
if (l->is_managed)
*ret = l;
static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
int ifindex, r;
Link *l;
assert(m);
static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int bus_method_set_link_search_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error);
int r, ifindex;
Link *l;
assert(m);
p = link_bus_path(l);
return -ENOMEM;
SD_BUS_METHOD("ResolveHostname", "isit", "a(iiay)st", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ResolveAddress", "iiayt", "a(is)t", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ResolveRecord", "isqqt", "a(iqqay)t", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ResolveService", "isssit", "a(qqqsa(iiay)s)aayssst", bus_method_resolve_service, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SetLinkDNSSECNegativeTrustAnchors", "ias", NULL, bus_method_set_link_dnssec_negative_trust_anchors, 0),
assert(s);
assert(m);
static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
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);
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/resolve1/link", "org.freedesktop.resolve1.Link", link_vtable, link_object_find, m);
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/resolve1/link", link_node_enumerator, m);
"path='/org/freedesktop/login1'",