resolved-bus.c revision 4ad7f2761da661853dcc29d542efb4727abb1101
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "bus-common-errors.h"
#include "bus-util.h"
#include "dns-domain.h"
#include "resolved-bus.h"
#include "resolved-def.h"
static int reply_query_state(DnsQuery *q) {
const char *name;
int r;
if (q->request_hostname)
name = q->request_hostname;
else {
if (r < 0)
return r;
}
switch (q->state) {
return sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
case DNS_TRANSACTION_TIMEOUT:
return sd_bus_reply_method_errorf(q->request, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
case DNS_TRANSACTION_ABORTED:
case DNS_TRANSACTION_FAILURE: {
if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
else {
const char *rc, *n;
char p[3]; /* the rcode is 4 bits long */
if (!rc) {
rc = p;
}
sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", name, rc);
}
}
case DNS_TRANSACTION_NULL:
case DNS_TRANSACTION_PENDING:
case DNS_TRANSACTION_SUCCESS:
default:
assert_not_reached("Impossible state");
}
}
int r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
} else
return -EAFNOSUPPORT;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
static void bus_method_resolve_hostname_complete(DnsQuery *q) {
unsigned added = 0, i;
int r;
assert(q);
if (q->state != DNS_TRANSACTION_SUCCESS) {
r = reply_query_state(q);
goto finish;
}
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (q->answer) {
if (r < 0)
goto finish;
if (r == 0) {
/* Hmm, if this is not an address record,
maybe it's a cname? If so, remember this */
if (r < 0)
goto finish;
if (r > 0)
continue;
}
if (r < 0)
goto finish;
if (!canonical)
added ++;
}
}
if (added == 0) {
if (!cname) {
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);
goto finish;
}
/* This has a cname? Then update the query with the
* new cname. */
if (r < 0) {
if (r == -ELOOP)
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_CNAME_LOOP, "CNAME loop on '%s'", q->request_hostname);
else
goto finish;
}
/* Before we restart the query, let's see if any of
* the RRs we already got already answers our query */
if (r < 0)
goto finish;
if (r == 0)
continue;
if (r < 0)
goto finish;
if (!canonical)
added++;
}
// what about the cache?
/* If we didn't find anything, then let's restart the
* query, this time with the cname */
if (added <= 0) {
r = dns_query_go(q);
if (r == -ESRCH) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
goto finish;
}
if (r < 0) {
goto finish;
}
return;
}
}
if (r < 0)
goto finish;
/* Return the precise spelling and uppercasing reported by the server */
r = sd_bus_message_append(reply, "st", DNS_RESOURCE_KEY_NAME(canonical->key), SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
if (r < 0)
goto finish;
if (r < 0) {
log_error_errno(r, "Failed to send hostname reply: %m");
}
dns_query_free(q);
}
if (ifindex < 0)
if (*flags & ~SD_RESOLVED_FLAGS_ALL)
if (*flags == 0)
return 0;
}
static int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *hostname;
DnsQuery *q;
int r;
assert(m);
if (r < 0)
return r;
if (r < 0)
if (r < 0)
return r;
if (!question)
return -ENOMEM;
if (!key)
return -ENOMEM;
if (r < 0)
return r;
}
if (!key)
return -ENOMEM;
if (r < 0)
return r;
}
if (r < 0)
return r;
q->request_family = family;
q->request_hostname = hostname;
r = dns_query_bus_track(q, 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_address_complete(DnsQuery *q) {
unsigned added = 0, i;
int r;
assert(q);
if (q->state != DNS_TRANSACTION_SUCCESS) {
r = reply_query_state(q);
goto finish;
}
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (q->answer) {
if (r < 0)
goto finish;
if (r == 0)
continue;
if (r < 0)
goto finish;
added ++;
}
}
if (added == 0) {
r = sd_bus_reply_method_errorf(q->request, BUS_ERROR_NO_SUCH_RR, "Address '%s' does not have any RR of requested type", ip);
goto finish;
}
if (r < 0)
goto finish;
r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
if (r < 0)
goto finish;
if (r < 0) {
log_error_errno(r, "Failed to send address reply: %m");
}
dns_query_free(q);
}
static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const void *d;
DnsQuery *q;
int r;
assert(m);
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (!question)
return -ENOMEM;
if (!key)
return -ENOMEM;
if (r < 0)
return r;
if (r < 0)
return r;
q->request_family = family;
r = dns_query_bus_track(q, 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) {
unsigned added = 0, i;
int r;
assert(q);
if (q->state != DNS_TRANSACTION_SUCCESS) {
r = reply_query_state(q);
goto finish;
}
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (q->answer) {
if (r < 0)
goto finish;
if (r == 0)
continue;
r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0);
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0)
goto finish;
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;
}
if (r < 0)
goto finish;
r = sd_bus_message_append(reply, "t", SD_RESOLVED_FLAGS_MAKE(q->answer_protocol, q->answer_family));
if (r < 0)
goto finish;
if (r < 0) {
log_error_errno(r, "Failed to send record reply: %m");
}
dns_query_free(q);
}
static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
int r, ifindex;
DnsQuery *q;
assert(m);
if (r < 0)
return r;
if (r < 0)
if (r < 0)
return r;
if (!question)
return -ENOMEM;
if (!key)
return -ENOMEM;
if (r < 0)
return r;
if (r < 0)
return r;
q->request_hostname = name;
r = dns_query_bus_track(q, 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_METHOD("ResolveHostname", "isit", "ia(iay)st", bus_method_resolve_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ResolveAddress", "iiayt", "iast", bus_method_resolve_address, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ResolveRecord", "isqqt", "ia(qqay)t", bus_method_resolve_record, SD_BUS_VTABLE_UNPRIVILEGED),
};
assert(s);
assert(m);
return 0;
}
static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
int b, r;
assert(m);
if (r < 0) {
log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
return 0;
}
if (b)
return 0;
log_debug("Coming back from suspend, verifying all RRs...");
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_errno(r, "Failed to connect to bus, trying again in 5s: %m");
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)
return log_error_errno(r, "Failed to install bus reconnect time event: %m");
return 0;
}
r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/resolve1", "org.freedesktop.resolve1.Manager", resolve_vtable, m);
if (r < 0)
return log_error_errno(r, "Failed to register object: %m");
if (r < 0)
return log_error_errno(r, "Failed to register name: %m");
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
"type='signal',"
"sender='org.freedesktop.login1',"
"interface='org.freedesktop.login1.Manager',"
"member='PrepareForSleep',"
"path='/org/freedesktop/login1'",
m);
if (r < 0)
log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
return 0;
}