resolved-bus.c revision 7f220d94a938a99c77400fa0ca30485e269bae7c
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer This file is part of systemd.
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer Copyright 2014 Lennart Poettering
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer systemd is free software; you can redistribute it and/or modify it
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer under the terms of the GNU Lesser General Public License as published by
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt the Free Software Foundation; either version 2.1 of the License, or
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt (at your option) any later version.
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt systemd is distributed in the hope that it will be useful, but
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt WITHOUT ANY WARRANTY; without even the implied warranty of
4be4833ece2856e0cacc09f8f8b2c02b320751faMartin Pitt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c50a4525aa8151b180d5a325e88c5f3812e66c36Martin Pitt Lesser General Public License for more details.
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier You should have received a copy of the GNU Lesser General Public License
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier along with systemd; If not, see <http://www.gnu.org/licenses/>.
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier r = in_addr_to_string(q->request_family, &q->request_address, &ip);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier name = dns_question_first_name(q->question);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier switch (q->state) {
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
b6f0c419e38a960873fe68bf8f89bbb0268eed02Harald Hoyer return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_RESOURCES, "Not enough resources");
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier return sd_bus_reply_method_errorf(q->request, BUS_ERROR_ABORTED, "Query aborted");
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", name);
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier const char *rc, *n;
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", name, rc);
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_error(q->request, &error);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_open_container(reply, 'r', "iiay");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_append(reply, "i", ifindex);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_append(reply, "i", AF_INET);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier } else if (rr->key->type == DNS_TYPE_AAAA) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_append(reply, "i", AF_INET6);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
8a8332f77e61d41f3bb28b8f929ed41e0ffaf721Zbigniew Jędrzejewski-Szmek r = sd_bus_message_close_container(reply);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic void bus_method_resolve_hostname_complete(DnsQuery *q) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier unsigned added = 0;
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek r = dns_query_process_cname(q);
edbced8a151c1b7ded685e2ec644950d2adec5f5Harald Hoyer 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));
739d81ddd005fae2bb82edce5b8a6173c7c48b34Zbigniew Jędrzejewski-Szmek if (r > 0) /* This was a cname, and the query was restarted. */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_new_method_return(q->request, &reply);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_open_container(reply, 'a', "(iiay)");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = dns_question_matches_rr(q->question, rr);
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin 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));
c7eda0133b6bf13a182337cbe8a61bf2faf9b32eEvgeny Vereshchagin r = sd_bus_message_close_container(reply);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* Return the precise spelling and uppercasing and CNAME target reported by the server */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_send(q->manager->bus, reply, NULL);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier log_error_errno(r, "Failed to send hostname reply: %m");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier sd_bus_reply_method_errno(q->request, r, NULL);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int check_ifindex_flags(int ifindex, uint64_t *flags, uint64_t ok, sd_bus_error *error) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if (*flags & ~(SD_RESOLVED_PROTOCOLS_ALL|SD_RESOLVED_NO_CNAME|ok))
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier if ((*flags & SD_RESOLVED_PROTOCOLS_ALL) == 0) /* If no protocol is enabled, enable all */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
4be4833ece2856e0cacc09f8f8b2c02b320751faMartin Pitt if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
7d023341c765c205068e33d23d63a4000ec211dfMartin Pitt return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = check_ifindex_flags(ifindex, &flags, 0, error);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = dns_question_new_address(&question, family, hostname);
3486cb6cfa3d32a95c0daf02c7510fdf372507bfMartin Pitt r = dns_query_new(m, &q, question, ifindex, flags);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier q->complete = bus_method_resolve_hostname_complete;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalierstatic void bus_method_resolve_address_complete(DnsQuery *q) {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier unsigned added = 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier /* We don't process CNAME for PTR lookups. */
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_new_method_return(q->request, &reply);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_open_container(reply, 'a', "(is)");
53d90f9582f96208b3674da823ad1a3d2c3b1aa4Martin Pitt DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
5c404f1ab8e96efedb983806443ca982a1b2a372Evgeny Vereshchagin r = dns_question_matches_rr(q->question, rr);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_append(reply, "(is)", ifindex, rr->ptr.name);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier in_addr_to_string(q->request_family, &q->request_address, &ip);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer 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));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error_errno(r, "Failed to send address reply: %m");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_bus_reply_method_errno(q->request, r, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const void *d;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "ii", &ifindex, &family);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read_array(message, 'y', &d, &sz);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_read(message, "t", &flags);
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = check_ifindex_flags(ifindex, &flags, 0, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = dns_question_new_reverse(&question, family, d);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = dns_query_new(m, &q, question, ifindex, flags);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer q->complete = bus_method_resolve_address_complete;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_open_container(m, 'r', "iqqay");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_append_array(m, 'y', DNS_PACKET_DATA(p) + start, p->size - start);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void bus_method_resolve_record_complete(DnsQuery *q) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned added = 0;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer 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));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r > 0) /* Following a CNAME */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_new_method_return(q->request, &reply);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer 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));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error_errno(r, "Failed to send record reply: %m");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_bus_reply_method_errno(q->request, r, NULL);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *name;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = check_ifindex_flags(ifindex, &flags, 0, error);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = dns_query_new(m, &q, question, ifindex, flags);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer q->complete = bus_method_resolve_record_complete;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* First, let's see if we could find an appropriate A or AAAA
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * record for the SRV record */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = dns_name_equal(dns_question_first_name(aux->question), rr->srv.name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = dns_question_matches_rr(aux->question, zz);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer rr->srv.priority, rr->srv.weight, rr->srv.port, rr->srv.name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_open_container(reply, 'a', "(iiay)");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = dns_name_equal(dns_question_first_name(aux->question), rr->srv.name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = dns_question_matches_rr(aux->question, zz);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* Note that above we appended the hostname as encoded in the
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer * SRV, and here the canonical hostname this maps to. */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_append(reply, "s", canonical ? DNS_RESOURCE_KEY_NAME(canonical->key) : rr->srv.name);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_append_array(reply, 'y', i->data, i->length);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic void resolve_service_all_complete(DnsQuery *q) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned added = false;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* If an auxiliary query is still pending, let's wait */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer /* We can only return one error, hence pick the last error we encountered */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer 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));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_new_method_return(q->request, &reply);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r == 0) /* not an SRV record */
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer 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));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_open_container(reply, 'a', "ay");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = dns_service_split(DNS_RESOURCE_KEY_NAME(canonical->key), &name, &type, &domain);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (r < 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer log_error_errno(r, "Failed to send service reply: %m");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer sd_bus_reply_method_errno(q->request, r, NULL);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic void resolve_service_hostname_complete(DnsQuery *q) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer resolve_service_all_complete(q->auxiliary_for);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r > 0) /* This was a cname, and the query was restarted. */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer resolve_service_all_complete(q->auxiliary_for);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* OK, we found an SRV record for the service. Let's resolve
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * the hostname included in it */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = dns_question_new_address(&question, q->request_family, rr->srv.name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = dns_query_new(q->manager, &aux, question, ifindex, q->flags);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer aux->complete = resolve_service_hostname_complete;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* Too many auxiliary lookups? If so, don't complain,
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * let's just not add this one, we already have more
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * than enough */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* Note that auxiliary queries do not track the original bus
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * client, only the primary request does that. */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic void bus_method_resolve_service_complete(DnsQuery *q) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer unsigned found = 0;
c53158818d8cdaf46b3f1b5299b9bda118a1043fThomas Hindoe Paaboel Andersen if (q->state != DNS_TRANSACTION_SUCCESS) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer 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));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r > 0) /* This was a cname, and the query was restarted. */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer 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));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* Maybe we are already finished? check now... */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (r < 0) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer log_error_errno(r, "Failed to send service reply: %m");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer sd_bus_reply_method_errno(q->request, r, NULL);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyerstatic int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = sd_bus_message_read(message, "isssit", &ifindex, &name, &type, &domain, &family, &flags);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid service name '%s'", name);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid SRV service type '%s'", type);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid domain '%s'", domain);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type.");
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = check_ifindex_flags(ifindex, &flags, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS, error);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* If the type is specified, we generate the full domain name to look up ourselves */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer /* If no type is specified, we assume the domain
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer * contains the full domain name to lookup already */
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = dns_question_new_service(&question, joined, !(flags & SD_RESOLVED_NO_TXT));
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer r = dns_query_new(m, &q, question, ifindex, flags);
0d6e798a784ef0ba6b95512e4453067b2f84a91aHarald Hoyer q->complete = bus_method_resolve_service_complete;
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyerstatic int append_dns_server(sd_bus_message *reply, DnsServer *s) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_open_container(reply, 'r', "iiay");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_append(reply, "ii", s->link ? s->link->ifindex : 0, s->family);
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer r = sd_bus_message_append_array(reply, 'y', &s->address, FAMILY_ADDRESS_SIZE(s->family));
1ecf6a2b4960229ad1d06c591b4776ddf065e834Harald Hoyer const char *path,
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer unsigned c = 0;
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier r = sd_bus_message_open_container(reply, 'a', "(iiay)");
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer if (c == 0) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer LIST_FOREACH(servers, s, m->fallback_dns_servers) {
898720b7e9cf3bdf7a93e435cbed5dd6942ecf9bHarald Hoyer const char *path,
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'",