resolved-bus.c revision 82bd6dddc4a363a9c3c6f41eb46eb171a80dca27
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek/***
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek This file is part of systemd.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek Copyright 2014 Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek (at your option) any later version.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek***/
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering#include "bus-errors.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "bus-util.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "resolved-dns-domain.h"
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek#include "resolved-bus.h"
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int reply_query_state(DnsQuery *q) {
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering _cleanup_free_ char *ip = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek const char *name;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek int r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering if (q->request_hostname)
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering name = q->request_hostname;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering else {
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering r = in_addr_to_string(q->request_family, &q->request_address, &ip);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek name = ip;
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek }
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek switch (q->state) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering case DNS_TRANSACTION_NO_SERVERS:
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case DNS_TRANSACTION_TIMEOUT:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "Query timed out");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case DNS_TRANSACTION_INVALID_REPLY:
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return sd_bus_reply_method_errorf(q->request, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt
603938e0a9b5379984d7397e3cf81683c0037a53Lennart Poettering case DNS_TRANSACTION_RESOURCES:
603938e0a9b5379984d7397e3cf81683c0037a53Lennart Poettering return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_RESOURCES, "Not enough resources");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek case DNS_TRANSACTION_ABORTED:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return sd_bus_reply_method_errorf(q->request, BUS_ERROR_ABORTED, "Query aborted");
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
29a5ca9baa58e55c4d9e1d008cdd014aa9c3c3e1Lennart Poettering case DNS_TRANSACTION_FAILURE: {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", name);
eb56eb9b40950f1edcffdb7313f8de4f8572a6d5Michal Schmidt else {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering const char *rc, *n;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering char p[3]; /* the rcode is 4 bits long */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering rc = dns_rcode_to_string(q->answer_rcode);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering if (!rc) {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering sprintf(p, "%i", q->answer_rcode);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering rc = p;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering n = strappenda(_BUS_ERROR_DNS, rc);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", name, rc);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek return sd_bus_reply_method_error(q->request, &error);
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek }
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek case DNS_TRANSACTION_NULL:
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek case DNS_TRANSACTION_PENDING:
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek case DNS_TRANSACTION_SUCCESS:
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek default:
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek assert_not_reached("Impossible state");
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek }
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek}
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmekstatic int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek int r;
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek
fff40a51ccbb02e8dec4ff2ee505bc84f75e445cZbigniew Jędrzejewski-Szmek assert(reply);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(rr);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
e0aa3726103448097e5ad7cc6f427e142103a321Zbigniew Jędrzejewski-Szmek r = sd_bus_message_open_container(reply, 'r', "iayi");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek return r;
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (rr->key->type == DNS_TYPE_A) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_append(reply, "i", AF_INET);
175a3d25d0e8596d4ba0759aea3f89ee228e7d6dLennart Poettering if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek } else if (rr->key->type == DNS_TYPE_AAAA) {
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek r = sd_bus_message_append(reply, "i", AF_INET6);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt return r;
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek } else
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering return -EAFNOSUPPORT;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering if (r < 0)
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering return r;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
29a5ca9baa58e55c4d9e1d008cdd014aa9c3c3e1Lennart Poettering r = sd_bus_message_append(reply, "i", ifindex);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_close_container(reply);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
23ea3dab15181a3966ec45fa2ccbb81e59d2e0a7Zbigniew Jędrzejewski-Szmek return 0;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poetteringstatic void bus_method_resolve_hostname_complete(DnsQuery *q) {
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL, *canonical = NULL;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek unsigned added = 0, i;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek int r, ifindex;
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert(q);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (q->state != DNS_TRANSACTION_SUCCESS) {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering r = reply_query_state(q);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering goto finish;
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek }
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek r = sd_bus_message_new_method_return(q->request, &reply);
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek if (r < 0)
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek goto finish;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering r = sd_bus_message_open_container(reply, 'a', "(iayi)");
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (r < 0)
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek goto finish;
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
23ea3dab15181a3966ec45fa2ccbb81e59d2e0a7Zbigniew Jędrzejewski-Szmek ifindex = q->answer_ifindex;
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek if (q->answer) {
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek answer = dns_answer_ref(q->answer);
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek for (i = 0; i < answer->n_rrs; i++) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = dns_question_matches_rr(q->question, answer->rrs[i]);
23ea3dab15181a3966ec45fa2ccbb81e59d2e0a7Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r == 0) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek /* Hmm, if this is not an address record,
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek maybe it's a cname? If so, remember this */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = dns_question_matches_cname(q->question, answer->rrs[i]);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r > 0)
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering cname = dns_resource_record_ref(answer->rrs[i]);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering continue;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering }
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering r = append_address(reply, answer->rrs[i], ifindex);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (r < 0)
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering goto finish;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (!canonical)
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering canonical = dns_resource_record_ref(answer->rrs[i]);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering added ++;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering }
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering }
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (added <= 0) {
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (!cname) {
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering 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);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering goto finish;
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering }
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering /* This has a cname? Then update the query with the
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek * new cname. */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = dns_query_cname_redirect(q, cname->cname.name);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r == -ELOOP)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop on '%s'", q->request_hostname);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek else
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering r = sd_bus_reply_method_errno(q->request, -r, NULL);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
23ea3dab15181a3966ec45fa2ccbb81e59d2e0a7Zbigniew Jędrzejewski-Szmek
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering /* Before we restart the query, let's see if any of
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek * the RRs we already got already answers our query */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek for (i = 0; i < answer->n_rrs; i++) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = dns_question_matches_rr(q->question, answer->rrs[i]);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r == 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek continue;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = append_address(reply, answer->rrs[i], ifindex);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt goto finish;
4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4fMichal Schmidt
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (!canonical)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek canonical = dns_resource_record_ref(answer->rrs[i]);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering added++;
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering }
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering /* If we didn't find anything, then let's restart the
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek * query, this time with the cname */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (added <= 0) {
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt r = dns_query_go(q);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r == -ESRCH) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0) {
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt r = sd_bus_reply_method_errno(q->request, -r, NULL);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_close_container(reply);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek /* Return the precise spelling and uppercasing reported by the server */
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(canonical);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_append(reply, "s", DNS_RESOURCE_KEY_NAME(canonical->key));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_send(q->manager->bus, reply, NULL);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidtfinish:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek log_error("Failed to send hostname reply: %s", strerror(-r));
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek sd_bus_reply_method_errno(q->request, -r, NULL);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek dns_query_free(q);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int bus_method_resolve_hostname(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
aa44499da15a8fa7026463555a7a27e55e4e24a8Philippe De Swert Manager *m = userdata;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek const char *hostname;
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering int family;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek DnsQuery *q;
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt int r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(bus);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(message);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering assert(m);
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering r = sd_bus_message_read(message, "si", &hostname, &family);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
30374ebe5e9f0b37e99dcbdc965c00fcf542f89dLennart Poettering return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
e9c1ea9de87d4d508ac38ce87a2fa56e7529a91aJason St. John
9d458c0902cdc5d993fd2f78d36fb83a8130c81bLennart Poettering r = dns_name_normalize(hostname, NULL);
9d458c0902cdc5d993fd2f78d36fb83a8130c81bLennart Poettering if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
e9c1ea9de87d4d508ac38ce87a2fa56e7529a91aJason St. John
e9c1ea9de87d4d508ac38ce87a2fa56e7529a91aJason St. John question = dns_question_new(family == AF_UNSPEC ? 2 : 1);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (!question)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return -ENOMEM;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (family != AF_INET6) {
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, hostname);
c099716487df4a4f5394e57e7ca14da1d358166aZbigniew Jędrzejewski-Szmek if (!key)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return -ENOMEM;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = dns_question_add(question, key);
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek if (family != AF_INET) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, hostname);
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek if (!key)
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek return -ENOMEM;
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek r = dns_question_add(question, key);
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = dns_query_new(m, &q, question);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek q->request = sd_bus_message_ref(message);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering q->request_family = family;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek q->request_hostname = hostname;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek q->complete = bus_method_resolve_hostname_complete;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek r = dns_query_bus_track(q, bus, message);
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek if (r < 0)
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek return r;
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek
892213bf1fd23e48d64a407ece6e10b07bef1926Zbigniew Jędrzejewski-Szmek r = dns_query_go(q);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (r < 0) {
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering dns_query_free(q);
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r == -ESRCH)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek sd_bus_error_setf(error, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return 1;
ed6556920c1a6cdfe0bb04e806bc1f54ea191545Mantas Mikulėnas}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic void bus_method_resolve_address_complete(DnsQuery *q) {
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek unsigned added = 0, i;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek int r;
3f6fd1ba65f962702753c4ad284b588e59689a23Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(q);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (q->state != DNS_TRANSACTION_SUCCESS) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = reply_query_state(q);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_new_method_return(q->request, &reply);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_open_container(reply, 'a', "s");
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (r < 0)
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering goto finish;
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (q->answer) {
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek answer = dns_answer_ref(q->answer);
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering for (i = 0; i < answer->n_rrs; i++) {
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering r = dns_question_matches_rr(q->question, answer->rrs[i]);
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering if (r < 0)
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering goto finish;
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering if (r == 0)
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering continue;
163c76c9305e6fee91dad8a3004f77020000ef96Lennart Poettering
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering r = sd_bus_message_append(reply, "s", answer->rrs[i]->ptr.name);
8dd4c05b5495c7ffe0f12ace87e71abe17bd0a0eLennart Poettering if (r < 0)
5e65c93a433447b15180249166f7b3944c3e6156Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek added ++;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (added <= 0) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *ip = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
601185b43da638b1c74153deae01dbd518680889Zbigniew Jędrzejewski-Szmek in_addr_to_string(q->request_family, &q->request_address, &ip);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address '%s' does not have any RR of requested type", ip);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_close_container(reply);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek goto finish;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_send(q->manager->bus, reply, NULL);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekfinish:
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek log_error("Failed to send address reply: %s", strerror(-r));
eceb8483e5a02e8e337486b89719a3b99cfcb7ceLennart Poettering sd_bus_reply_method_errno(q->request, -r, NULL);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek }
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek dns_query_free(q);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek}
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmekstatic int bus_method_resolve_address(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *reverse = NULL;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek Manager *m = userdata;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek int family, ifindex;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek const void *d;
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering DnsQuery *q;
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering size_t sz;
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering int r;
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(bus);
eceb8483e5a02e8e337486b89719a3b99cfcb7ceLennart Poettering assert(message);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek assert(m);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(message, "i", &family);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (!IN_SET(family, AF_INET, AF_INET6))
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read_array(message, 'y', &d, &sz);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2c408cb6276e3b8d18fb4e2a81a1128d8bbaa70dLennart Poettering return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (sz != FAMILY_ADDRESS_SIZE(family))
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = sd_bus_message_read(message, "i", &ifindex);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (ifindex < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek r = dns_name_reverse(family, d, &reverse);
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek if (r < 0)
2ca0435be9359bde3020eeb528c2a6d72ac1e0b0Zbigniew Jędrzejewski-Szmek return r;
question = dns_question_new(1);
if (!question)
return -ENOMEM;
key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, reverse);
if (!key)
return -ENOMEM;
reverse = NULL;
r = dns_question_add(question, key);
if (r < 0)
return r;
r = dns_query_new(m, &q, question);
if (r < 0)
return r;
q->request = sd_bus_message_ref(message);
q->request_family = family;
memcpy(&q->request_address, d, sz);
q->complete = bus_method_resolve_address_complete;
r = dns_query_bus_track(q, bus, message);
if (r < 0)
return r;
r = dns_query_go(q);
if (r < 0) {
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");
return r;
}
return 1;
}
static void bus_method_resolve_record_complete(DnsQuery *q) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
unsigned added = 0, i;
int r;
assert(q);
if (q->state != DNS_TRANSACTION_SUCCESS) {
r = reply_query_state(q);
goto finish;
}
r = sd_bus_message_new_method_return(q->request, &reply);
if (r < 0)
goto finish;
r = sd_bus_message_open_container(reply, 'a', "(qqay)");
if (r < 0)
goto finish;
if (q->answer) {
answer = dns_answer_ref(q->answer);
for (i = 0; i < answer->n_rrs; i++) {
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
size_t start;
r = dns_question_matches_rr(q->question, answer->rrs[i]);
if (r < 0)
goto finish;
if (r == 0)
continue;
r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0);
if (r < 0)
goto finish;
r = dns_packet_append_rr(p, answer->rrs[i], &start);
if (r < 0)
goto finish;
r = sd_bus_message_open_container(reply, 'r', "qqay");
if (r < 0)
goto finish;
r = sd_bus_message_append(reply, "qq", answer->rrs[i]->key->class, answer->rrs[i]->key->type);
if (r < 0)
goto finish;
r = sd_bus_message_append_array(reply, 'y', DNS_PACKET_DATA(p) + start, p->size - start);
if (r < 0)
goto finish;
r = sd_bus_message_close_container(reply);
if (r < 0)
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;
}
r = sd_bus_message_close_container(reply);
if (r < 0)
goto finish;
r = sd_bus_send(q->manager->bus, reply, NULL);
finish:
if (r < 0) {
log_error("Failed to send record reply: %s", strerror(-r));
sd_bus_reply_method_errno(q->request, -r, NULL);
}
dns_query_free(q);
}
static int bus_method_resolve_record(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
Manager *m = userdata;
DnsQuery *q;
int r;
uint16_t class, type;
const char *name;
assert(bus);
assert(message);
assert(m);
r = sd_bus_message_read(message, "sqq", &name, &class, &type);
if (r < 0)
return r;
r = dns_name_normalize(name, NULL);
if (r < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
question = dns_question_new(1);
if (!question)
return -ENOMEM;
key = dns_resource_key_new(class, type, name);
if (!key)
return -ENOMEM;
r = dns_question_add(question, key);
if (r < 0)
return r;
r = dns_query_new(m, &q, question);
if (r < 0)
return r;
q->request = sd_bus_message_ref(message);
q->request_hostname = name;
q->complete = bus_method_resolve_record_complete;
r = dns_query_bus_track(q, bus, message);
if (r < 0)
return r;
r = dns_query_go(q);
if (r < 0) {
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");
return r;
}
return 1;
}
static const sd_bus_vtable resolve_vtable[] = {
SD_BUS_VTABLE_START(0),
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),
SD_BUS_VTABLE_END,
};
static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
Manager *m = userdata;
assert(s);
assert(m);
m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
manager_connect_bus(m);
return 0;
}
int manager_connect_bus(Manager *m) {
int r;
assert(m);
if (m->bus)
return 0;
r = sd_bus_default_system(&m->bus);
if (r < 0) {
/* We failed to connect? Yuck, we must be in early
* boot. Let's try in 5s again. As soon as we have
* kdbus we can stop doing this... */
log_debug("Failed to connect to bus, trying again in 5s: %s", strerror(-r));
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);
if (r < 0) {
log_error("Failed to install bus reconnect time event: %s", strerror(-r));
return r;
}
return 0;
}
r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable, m);
if (r < 0) {
log_error("Failed to register object: %s", strerror(-r));
return r;
}
r = sd_bus_request_name(m->bus, "org.freedesktop.resolve1", 0);
if (r < 0) {
log_error("Failed to register name: %s", strerror(-r));
return r;
}
r = sd_bus_attach_event(m->bus, m->event, 0);
if (r < 0) {
log_error("Failed to attach bus to event loop: %s", strerror(-r));
return r;
}
return 0;
}