resolved-bus.c revision 019036a47fcd10fcf0286800d144c706f3773e2f
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering This file is part of systemd.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Copyright 2014 Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (at your option) any later version.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is distributed in the hope that it will be useful, but
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Lesser General Public License for more details.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int reply_query_state(DnsQuery *q) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = in_addr_to_string(q->request_family, &q->request_address, &ip);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering name = dns_question_first_name(q->question);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
4f10118016f9b2fd7e1d26c9ef7d91eb33fba694Lennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_RESOURCES, "Not enough resources");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_ABORTED, "Query aborted");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_ABORTED, "DNSSEC validation failed: %s",
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering dnssec_result_to_string(q->answer_dnssec_result));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", name);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering const char *rc, *n;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering char p[3]; /* the rcode is 4 bits long */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering rc = dns_rcode_to_string(q->answer_rcode);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", name, rc);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return sd_bus_reply_method_error(q->request, &error);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_open_container(reply, 'r', "iiay");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_append(reply, "i", ifindex);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_append(reply, "i", AF_INET);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering } else if (rr->key->type == DNS_TYPE_AAAA) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_append(reply, "i", AF_INET6);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_close_container(reply);
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersenstatic void bus_method_resolve_hostname_complete(DnsQuery *q) {
7dfbe2e3fc0215b49d8202a32beb6b1aae08c4e4Tom Gundersen _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (q->state != DNS_TRANSACTION_SUCCESS) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_question_first_name(q->question));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (r > 0) /* This was a cname, and the query was restarted. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_new_method_return(q->request, &reply);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_open_container(reply, 'a', "(iiay)");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_question_matches_rr(q->question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_question_first_name(q->question));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_close_container(reply);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Return the precise spelling and uppercasing and CNAME target reported by the server */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_send(q->manager->bus, reply, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error_errno(r, "Failed to send hostname reply: %m");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sd_bus_reply_method_errno(q->request, r, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int check_ifindex_flags(int ifindex, uint64_t *flags, uint64_t ok, sd_bus_error *error) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (*flags & ~(SD_RESOLVED_PROTOCOLS_ALL|SD_RESOLVED_NO_CNAME|ok))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if ((*flags & SD_RESOLVED_PROTOCOLS_ALL) == 0) /* If no protocol is enabled, enable all */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_cc(sizeof(int) == sizeof(int32_t));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_SEARCH, error);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = dns_question_new_address(&question, family, hostname);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_query_new(m, &q, question, ifindex, flags);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering q->request = sd_bus_message_ref(message);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering q->complete = bus_method_resolve_hostname_complete;
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poetteringstatic void bus_method_resolve_address_complete(DnsQuery *q) {
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (q->state != DNS_TRANSACTION_SUCCESS) {
cbf60d0a7fda7bd6c0578a6cc151f798a0edfd02Lennart Poettering r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_question_first_name(q->question));
8f077bf94e129fa1b6f0159e3140c4326f1066cfZbigniew Jędrzejewski-Szmek if (r > 0) /* This was a cname, and the query was restarted. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_new_method_return(q->request, &reply);
b2c23da8cea1987a1a329f5a964d3299b7ca7890Lennart Poettering r = sd_bus_message_open_container(reply, 'a', "(is)");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_question_matches_rr(q->question, rr, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_append(reply, "(is)", ifindex, rr->ptr.name);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering in_addr_to_string(q->request_family, &q->request_address, &ip);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering 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));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_close_container(reply);
be847e82cf95bf8eb589778df2aa2b3d1d7ae99eLennart Poettering r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_send(q->manager->bus, reply, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_error_errno(r, "Failed to send address reply: %m");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sd_bus_reply_method_errno(q->request, r, NULL);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering const void *d;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_cc(sizeof(int) == sizeof(int32_t));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_read(message, "ii", &ifindex, &family);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_read_array(message, 'y', &d, &sz);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_read(message, "t", &flags);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = check_ifindex_flags(ifindex, &flags, 0, error);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = dns_question_new_reverse(&question, family, d);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = dns_query_new(m, &q, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering q->request = sd_bus_message_ref(message);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering q->complete = bus_method_resolve_address_complete;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_open_container(m, 'r', "iqqay");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_resource_record_to_wire_format(rr, false);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return sd_bus_message_close_container(m);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void bus_method_resolve_record_complete(DnsQuery *q) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (q->state != DNS_TRANSACTION_SUCCESS) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_question_first_name(q->question));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r > 0) /* Following a CNAME */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_new_method_return(q->request, &reply);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = dns_question_matches_rr(q->question, rr, NULL);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering 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_question_first_name(q->question));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_close_container(reply);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_send(q->manager->bus, reply, NULL);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error_errno(r, "Failed to send record reply: %m");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering sd_bus_reply_method_errno(q->request, r, NULL);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering assert_cc(sizeof(int) == sizeof(int32_t));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid RR type for query %" PRIu16, type);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = check_ifindex_flags(ifindex, &flags, 0, error);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering key = dns_resource_key_new(class, type, name);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_query_new(m, &q, question, ifindex, flags|SD_RESOLVED_NO_SEARCH);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering q->request = sd_bus_message_ref(message);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering q->complete = bus_method_resolve_record_complete;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* First, let's see if we could find an appropriate A or AAAA
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering * record for the SRV record */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (aux->state != DNS_TRANSACTION_SUCCESS)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_name_equal(dns_question_first_name(aux->question), rr->srv.name);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_question_matches_rr(aux->question, zz, NULL);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering rr->srv.priority, rr->srv.weight, rr->srv.port, rr->srv.name);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_open_container(reply, 'a', "(iiay)");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (aux->state != DNS_TRANSACTION_SUCCESS)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_name_equal(dns_question_first_name(aux->question), rr->srv.name);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_question_matches_rr(aux->question, zz, NULL);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering r = sd_bus_message_close_container(reply);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Note that above we appended the hostname as encoded in the
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * SRV, and here the canonical hostname this maps to. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_append(reply, "s", canonical ? DNS_RESOURCE_KEY_NAME(canonical->key) : rr->srv.name);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_close_container(reply);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poetteringstatic int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_append_array(reply, 'y', i->data, i->length);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void resolve_service_all_complete(DnsQuery *q) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering unsigned added = false;
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
dcc2fc01fa850e9ee36c549dc2691e7e5c71bebfLennart Poettering /* If an auxiliary query is still pending, let's wait */
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering /* We can only return one error, hence pick the last error we encountered */
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (bad->state == DNS_TRANSACTION_SUCCESS) {
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_question_first_name(bad->question));
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering r = sd_bus_message_new_method_return(q->request, &reply);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)");
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering r = dns_question_matches_rr(q->question, rr, NULL);
bd5f920f1288c0d4d488629fadf067f709227030Lennart Poettering if (r == 0) /* not an SRV record */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_question_first_name(q->question));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_close_container(reply);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_open_container(reply, 'a', "ay");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_question_matches_rr(q->question, rr, NULL);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_message_close_container(reply);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = dns_service_split(DNS_RESOURCE_KEY_NAME(canonical->key), &name, &type, &domain);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family, q->answer_authenticated));
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering r = sd_bus_send(q->manager->bus, reply, NULL);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error_errno(r, "Failed to send service reply: %m");
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering sd_bus_reply_method_errno(q->request, r, NULL);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic void resolve_service_hostname_complete(DnsQuery *q) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (q->state != DNS_TRANSACTION_SUCCESS) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering resolve_service_all_complete(q->auxiliary_for);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r > 0) /* This was a cname, and the query was restarted. */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering resolve_service_all_complete(q->auxiliary_for);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poetteringstatic int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* OK, we found an SRV record for the service. Let's resolve
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering * the hostname included in it */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = dns_question_new_address(&question, q->request_family, rr->srv.name);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = dns_query_new(q->manager, &aux, question, ifindex, q->flags|SD_RESOLVED_NO_SEARCH);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering aux->complete = resolve_service_hostname_complete;
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering /* Too many auxiliary lookups? If so, don't complain,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * let's just not add this one, we already have more
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * than enough */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Note that auxiliary queries do not track the original bus
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * client, only the primary request does that. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void bus_method_resolve_service_complete(DnsQuery *q) {
2f671520ebade4877cbf6aca3572a5f8c4e1871dLennart Poettering if (q->state != DNS_TRANSACTION_SUCCESS) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_question_first_name(q->question));
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering if (r > 0) /* This was a cname, and the query was restarted. */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering r = dns_question_matches_rr(q->question, rr, NULL);
d79acc309327f8c0863bd3da8b93d926a1c93ba1David Herrmann if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering r = resolve_service_hostname(q, rr, ifindex);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_question_first_name(q->question));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Maybe we are already finished? check now... */
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering log_error_errno(r, "Failed to send service reply: %m");
700ff4d97311902a440109a2c081731ab6ae8a20Lennart Poettering sd_bus_reply_method_errno(q->request, r, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering const char *name, *type, *domain, *joined;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_cc(sizeof(int) == sizeof(int32_t));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = sd_bus_message_read(message, "isssit", &ifindex, &name, &type, &domain, &family, &flags);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid service name '%s'", name);
a4152e3fe28b53b8919cc404dd7eca7ead1bf9bdLennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid SRV service type '%s'", type);
0faacd470dfbd24f4c6504da6f04213aa05f9d19Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid domain '%s'", domain);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type.");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS, error);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* If the type is specified, we generate the full domain name to look up ourselves */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering r = dns_service_join(name, type, domain, &n);
joined = n;
goto fail;
r = dns_query_go(q);
goto fail;
fail:
dns_query_free(q);
assert(s);
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);
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),
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);
"path='/org/freedesktop/login1'",