/***
This file is part of systemd.
Copyright 2014 Zbigniew Jędrzejewski-Szmek
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 <getopt.h>
#include "sd-bus.h"
#include "af-list.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-util.h"
#include "escape.h"
#include "in-addr-util.h"
#include "parse-util.h"
#include "resolved-def.h"
#include "resolved-dns-packet.h"
#include "terminal-util.h"
static int arg_ifindex = 0;
static bool arg_legend = true;
static enum {
if (!arg_legend)
return;
if (flags == 0)
return;
if (flags != 0)
printf(" protocol%s%s%s%s%s",
}
unsigned c = 0;
int r;
log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveHostname");
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
const void *a;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
continue;
}
log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown");
return -EINVAL;
}
ifname[0] = 0;
if (r < 0)
printf("%*s%s %s%s%s\n",
c++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
printf("%*s%s (%s)\n",
if (c == 0) {
return -ESRCH;
}
return 0;
}
static int resolve_address(sd_bus *bus, int family, const union in_addr_union *address, int ifindex) {
unsigned c = 0;
int r;
if (ifindex <= 0)
if (r < 0)
return log_oom();
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveAddress");
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_create_error(r);
const char *n;
if (r < 0)
return r;
if (r < 0)
return r;
ifname[0] = 0;
printf("%*s%*s%*s%s %s\n",
c == 0 ? ":" : " ",
n);
c++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (c == 0) {
return -ESRCH;
}
return 0;
}
const char *percent, *a;
int ifi = 0;
int r;
if (percent) {
if (ifi <= 0)
return -EINVAL;
}
} else
a = s;
if (r < 0)
return r;
return 0;
}
unsigned n = 0;
int r;
log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(class), dns_type_to_string(type), isempty(ifname) ? "*" : ifname);
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveRecord");
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
const char *s;
uint16_t c, t;
int ifindex;
const void *d;
size_t l;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0);
if (r < 0)
return log_oom();
p->refuse_compression = true;
r = dns_packet_append_blob(p, d, l, NULL);
if (r < 0)
return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to parse RR: %m");
if (!s)
return log_oom();
ifname[0] = 0;
n++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (n == 0) {
return -ESRCH;
}
return 0;
}
const char *p, *q, *n;
int r;
/* Parse RFC 4501 dns: URIs */
p = name + 4;
if (p[0] == '/') {
const char *e;
if (p[1] != '/')
goto invalid;
if (!e)
goto invalid;
if (e != p + 2)
log_warning("DNS authority specification not supported; ignoring specified authority.");
p = e + 1;
}
q = strchr(p, '?');
if (q) {
n = strndupa(p, q - p);
q++;
for (;;) {
const char *f;
f = startswith_no_case(q, "class=");
if (f) {
_cleanup_free_ char *t = NULL;
const char *e;
if (class != 0) {
log_error("DNS class specified twice.");
return -EINVAL;
}
e = strchrnul(f, ';');
t = strndup(f, e - f);
if (!t)
return log_oom();
r = dns_class_from_string(t);
if (r < 0) {
log_error("Unknown DNS class %s.", t);
return -EINVAL;
}
class = r;
if (*e == ';') {
q = e + 1;
continue;
}
break;
}
f = startswith_no_case(q, "type=");
if (f) {
_cleanup_free_ char *t = NULL;
const char *e;
if (type != 0) {
log_error("DNS type specified twice.");
return -EINVAL;
}
e = strchrnul(f, ';');
t = strndup(f, e - f);
if (!t)
return log_oom();
r = dns_type_from_string(t);
if (r < 0) {
log_error("Unknown DNS type %s.", t);
return -EINVAL;
}
type = r;
if (*e == ';') {
q = e + 1;
continue;
}
break;
}
goto invalid;
}
} else
n = p;
if (type == 0)
if (type == 0)
type = DNS_TYPE_A;
if (class == 0)
if (class == 0)
return -EINVAL;
}
const char *p;
unsigned c;
int r;
if (name)
log_debug("Resolving service \"%s\" of type %s in %s (family %s, interface %s).", name, type, domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
else if (type)
log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
else
log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(ifname) ? "*" : ifname);
bus,
&req,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResolveService");
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
indent =
c = 0;
if (r < 0)
return bus_log_parse_error(r);
if (name)
if (type)
printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
c == 0 ? ":" : " ",
if (r < 0)
return bus_log_parse_error(r);
const void *a;
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
continue;
}
log_error("%s: systemd-resolved returned address of invalid size %zu for family %s", name, sz, af_to_name(family) ?: "unknown");
return -EINVAL;
}
ifname[0] = 0;
if (r < 0)
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
c++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
c = 0;
if (!escaped)
return log_oom();
c++;
}
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags);
if (r < 0)
return bus_log_parse_error(r);
if (isempty(canonical_name))
if (isempty(canonical_type))
if (canonical_name)
if (canonical_type)
}
return 0;
}
int r, dnssec_supported;
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"DNSSECSupported",
&error,
'b',
if (r < 0)
return log_error_errno(r, "Failed to get DNSSEC supported state: %s", bus_error_message(&error, r));
printf("DNSSEC supported by current servers: %s%s%s\n\n",
ansi_normal());
r = sd_bus_get_property(bus,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"TransactionStatistics",
&error,
&reply,
"(tt)");
if (r < 0)
return log_error_errno(r, "Failed to get transaction statistics: %s", bus_error_message(&error, r));
if (r < 0)
return bus_log_parse_error(r);
printf("%sTransactions%s\n"
ansi_normal(),
r = sd_bus_get_property(bus,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"CacheStatistics",
&error,
&reply,
"(ttt)");
if (r < 0)
&n_cache_miss);
if (r < 0)
return bus_log_parse_error(r);
printf("\n%sCache%s\n"
ansi_normal(),
r = sd_bus_get_property(bus,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"DNSSECStatistics",
&error,
&reply,
"(tttt)");
if (r < 0)
if (r < 0)
return bus_log_parse_error(r);
printf("\n%sDNSSEC Verdicts%s\n"
ansi_normal(),
return 0;
}
int r;
r = sd_bus_call_method(bus,
"org.freedesktop.resolve1",
"/org/freedesktop/resolve1",
"org.freedesktop.resolve1.Manager",
"ResetStatistics",
&error,
NULL,
NULL);
if (r < 0)
return 0;
}
static void help_protocol_types(void) {
if (arg_legend)
puts("Known protocol types:");
puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6");
}
static void help_dns_types(void) {
int i;
const char *t;
if (arg_legend)
puts("Known DNS RR types:");
for (i = 0; i < _DNS_TYPE_MAX; i++) {
t = dns_type_to_string(i);
if (t)
puts(t);
}
}
static void help_dns_classes(void) {
int i;
const char *t;
if (arg_legend)
puts("Known DNS RR classes:");
for (i = 0; i < _DNS_CLASS_MAX; i++) {
t = dns_class_to_string(i);
if (t)
puts(t);
}
}
static void help(void) {
printf("%s [OPTIONS...] NAME...\n"
"%s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n\n"
"Resolve domain names, IPv4 and IPv6 addresses, DNS resource records, and services.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" -4 Resolve IPv4 addresses\n"
" -6 Resolve IPv6 addresses\n"
" -i --interface=INTERFACE Look on interface\n"
" -p --protocol=PROTOCOL|help Look via protocol\n"
" -t --type=TYPE|help Query RR with DNS type\n"
" -c --class=CLASS|help Query RR with DNS class\n"
" --service Resolve service (SRV)\n"
" --service-address=BOOL Do [not] resolve address for services\n"
" --service-txt=BOOL Do [not] resolve TXT records for services\n"
" --cname=BOOL Do [not] follow CNAME redirects\n"
" --search=BOOL Do [not] use search domains\n"
" --legend=BOOL Do [not] print column headers and meta information\n"
" --statistics Show resolver statistics\n"
" --reset-statistics Reset resolver statistics\n"
}
enum {
};
{}
};
int c, r;
switch(c) {
case 'h':
help();
return 0; /* done */;
case ARG_VERSION:
return version();
case '4':
break;
case '6':
break;
case 'i': {
int ifi;
arg_ifindex = ifi;
else {
if (ifi <= 0)
arg_ifindex = ifi;
}
break;
}
case 't':
return 0;
}
r = dns_type_from_string(optarg);
if (r < 0) {
return r;
}
break;
case 'c':
return 0;
}
r = dns_class_from_string(optarg);
if (r < 0) {
return r;
}
break;
case ARG_LEGEND:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --legend= argument");
arg_legend = r;
break;
case 'p':
return 0;
else {
return -EINVAL;
}
break;
case ARG_SERVICE:
break;
case ARG_CNAME:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --cname= argument.");
if (r == 0)
else
break;
case ARG_SERVICE_ADDRESS:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --service-address= argument.");
if (r == 0)
else
break;
case ARG_SERVICE_TXT:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --service-txt= argument.");
if (r == 0)
else
break;
case ARG_SEARCH:
r = parse_boolean(optarg);
if (r < 0)
return log_error_errno(r, "Failed to parse --search argument.");
if (r == 0)
else
break;
case ARG_STATISTICS:
break;
case ARG_RESET_STATISTICS:
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("--class= may only be used in conjunction with --type=.");
return -EINVAL;
}
log_error("--service and --type= may not be combined.");
return -EINVAL;
}
return 1 /* work to do */;
}
int r;
log_open();
if (r <= 0)
goto finish;
r = sd_bus_open_system(&bus);
if (r < 0) {
log_error_errno(r, "sd_bus_open_system: %m");
goto finish;
}
switch (arg_mode) {
case MODE_RESOLVE_HOST:
log_error("No arguments passed.");
r = -EINVAL;
goto finish;
}
union in_addr_union a;
else {
if (k >= 0)
else
}
if (r == 0)
r = k;
optind++;
}
break;
case MODE_RESOLVE_RECORD:
log_error("No arguments passed.");
r = -EINVAL;
goto finish;
}
int k;
if (r == 0)
r = k;
optind++;
}
break;
case MODE_RESOLVE_SERVICE:
log_error("Domain specification required.");
r = -EINVAL;
goto finish;
else {
log_error("Too many arguments.");
r = -EINVAL;
goto finish;
}
break;
case MODE_STATISTICS:
log_error("Too many arguments.");
r = -EINVAL;
goto finish;
}
r = show_statistics(bus);
break;
case MODE_RESET_STATISTICS:
log_error("Too many arguments.");
r = -EINVAL;
goto finish;
}
r = reset_statistics(bus);
break;
}
return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}