Skip to content

Commit cfac7ec

Browse files
committed
resolvectl: use varlink instead of dbus for ResolveRecord
1 parent 5ff17fb commit cfac7ec

3 files changed

Lines changed: 153 additions & 100 deletions

File tree

‎src/resolve/resolvectl.c‎

Lines changed: 64 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ static void print_ifindex_comment(int printed_so_far, int ifindex) {
279279
ansi_grey(), ifname, ansi_normal());
280280
}
281281

282-
static int varlink_log_resolve_error(const char *name, const char *error_id, sd_json_variant *reply) {
282+
static int varlink_log_resolve_error(const char *name, const char *error_id, sd_json_variant *reply, bool warn_missing) {
283283
int r;
284284

285285
assert(name);
@@ -342,9 +342,13 @@ static int varlink_log_resolve_error(const char *name, const char *error_id, sd_
342342
return log_error_errno(ret, "%s: resolve call failed: DNSSEC validation failed: %s%s", name, error.result, strempty(msg_extended));
343343

344344
if (error.rcode != _DNS_RCODE_INVALID) {
345-
if (error.rcode == DNS_RCODE_NXDOMAIN)
345+
if (error.rcode == DNS_RCODE_NXDOMAIN) {
346+
if (!warn_missing)
347+
return -ENXIO;
348+
346349
return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "%s: resolve call failed: Name '%s' not found%s%s",
347350
name, error.query_string ?: name, error.ede_rcode >= 0 ? ":" : "", strempty(msg_extended));
351+
}
348352

349353
return log_error_errno(ret, "%s: resolve call failed: Could not resolve '%s', server or network returned error: %s%s",
350354
name, error.query_string ?: name, FORMAT_DNS_RCODE(error.rcode), strempty(msg_extended));
@@ -403,7 +407,7 @@ static int resolve_host(const char *name) {
403407
ts = now(CLOCK_MONOTONIC) - ts;
404408

405409
if (!isempty(error_id))
406-
return varlink_log_resolve_error(name, error_id, v);
410+
return varlink_log_resolve_error(name, error_id, v, /* warn_missing = */ true);
407411

408412
_cleanup_(resolve_hostname_reply_done) ResolveHostnameReply reply = {};
409413
r = dispatch_resolve_hostname_reply(/* name = */ NULL, v, SD_JSON_LOG, &reply);
@@ -489,7 +493,7 @@ static int resolve_address(int family, const union in_addr_union *address, int i
489493
ts = now(CLOCK_MONOTONIC) - ts;
490494

491495
if (!isempty(error_id))
492-
return varlink_log_resolve_error(pretty, error_id, v);
496+
return varlink_log_resolve_error(pretty, error_id, v, /* warn_missing = */ true);
493497

494498
_cleanup_(resolve_address_reply_done) ResolveAddressReply reply = {};
495499
r = dispatch_resolve_address_reply(/* name = */ NULL, v, SD_JSON_LOG, &reply);
@@ -520,15 +524,10 @@ static int resolve_address(int family, const union in_addr_union *address, int i
520524
return 0;
521525
}
522526

523-
static int output_rr_packet(const void *d, size_t l, int ifindex) {
524-
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
527+
static int output_rr_packet(DnsResourceRecord *rr, int ifindex) {
525528
int r;
526529

527-
assert(d || l == 0);
528-
529-
r = dns_resource_record_new_from_raw(&rr, d, l);
530-
if (r < 0)
531-
return log_error_errno(r, "Failed to parse RR: %m");
530+
assert(rr);
532531

533532
if (sd_json_format_enabled(arg_json_format_flags)) {
534533
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
@@ -611,14 +610,7 @@ static bool single_label_nonsynthetic(const char *name) {
611610
return !streq(name, first_label);
612611
}
613612

614-
static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type, bool warn_missing) {
615-
_cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
616-
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
617-
_cleanup_free_ char *idnafied = NULL;
618-
bool needs_authentication = false;
619-
unsigned n = 0;
620-
uint64_t flags;
621-
usec_t ts;
613+
static int resolve_record(const char *name, uint16_t class, uint16_t type, bool warn_missing) {
622614
int r;
623615

624616
assert(name);
@@ -629,6 +621,7 @@ static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_
629621
log_notice("(Note that search domains are not appended when --type= is specified. "
630622
"Please specify fully qualified domain names, or remove --type= switch from invocation in order to request regular hostname resolution.)");
631623

624+
_cleanup_free_ char *idnafied = NULL;
632625
r = idna_candidate(name, &idnafied);
633626
if (r < 0)
634627
return r;
@@ -637,85 +630,76 @@ static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_
637630
"Please specify translated domain names — i.e. '%s' — when resolving raw records, or remove --type= switch from invocation in order to request regular hostname resolution.",
638631
idnafied);
639632

640-
r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveRecord");
641-
if (r < 0)
642-
return bus_log_create_error(r);
643-
644-
r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, class, type, arg_flags);
633+
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL;
634+
r = varlink_connect_with_query_timeout(&vl);
645635
if (r < 0)
646-
return bus_log_create_error(r);
647-
648-
ts = now(CLOCK_MONOTONIC);
649-
650-
r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
651-
if (r < 0) {
652-
if (warn_missing || r != -ENXIO)
653-
log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r));
654636
return r;
655-
}
656637

657-
ts = now(CLOCK_MONOTONIC) - ts;
638+
usec_t ts = now(CLOCK_MONOTONIC);
658639

659-
r = sd_bus_message_enter_container(reply, 'a', "(iqqay)");
640+
const char *error_id = NULL;
641+
sd_json_variant *v = NULL;
642+
r = sd_varlink_callbo(
643+
vl,
644+
"io.systemd.Resolve.ResolveRecord",
645+
&v,
646+
&error_id,
647+
SD_JSON_BUILD_PAIR_STRING("name", name),
648+
SD_JSON_BUILD_PAIR_UNSIGNED("type", type),
649+
JSON_BUILD_PAIR_CONDITION_UNSIGNED(arg_ifindex > 0, "ifindex", arg_ifindex),
650+
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("class", class),
651+
JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("flags", arg_flags));
660652
if (r < 0)
661-
return bus_log_parse_error(r);
653+
return log_error_errno(r, "Failed to issue varlink call: %m");
662654

663-
while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) {
664-
uint16_t c, t;
665-
int ifindex;
666-
const void *d;
667-
size_t l;
655+
ts = now(CLOCK_MONOTONIC) - ts;
668656

669-
assert_cc(sizeof(int) == sizeof(int32_t));
657+
if (!isempty(error_id)) {
658+
if (streq(error_id, "io.systemd.Resolve.ResourceRecordTypeInvalidForQuery"))
659+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified resource record type %" PRIu16 " may not be used in a query", type);
670660

671-
r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t);
672-
if (r < 0)
673-
return bus_log_parse_error(r);
661+
if (streq(error_id, "io.systemd.Resolve.ResourceRecordTypeObsolete"))
662+
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Specified DNS resource record type %" PRIu16 " is obsolete", type);
674663

675-
r = sd_bus_message_read_array(reply, 'y', &d, &l);
676-
if (r < 0)
677-
return bus_log_parse_error(r);
664+
return varlink_log_resolve_error(name, error_id, v, warn_missing);
665+
}
678666

679-
r = sd_bus_message_exit_container(reply);
667+
_cleanup_(resolve_record_reply_done) ResolveRecordReply reply = {};
668+
r = dispatch_resolve_record_reply(/* name = */ NULL, v, SD_JSON_LOG, &reply);
669+
if (r < 0)
670+
return r;
671+
672+
bool needs_authentication = false;
673+
FOREACH_ARRAY(record, reply.records, reply.n_records) {
674+
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
675+
r = dns_resource_record_new_from_raw(&rr, record->raw.iov_base, record->raw.iov_len);
680676
if (r < 0)
681-
return bus_log_parse_error(r);
677+
return log_error_errno(r, "Failed to parse RR: %m");
682678

683679
if (arg_raw == RAW_PACKET) {
684-
uint64_t u64 = htole64(l);
680+
uint64_t u64 = htole64(record->raw.iov_len);
685681

686682
fwrite(&u64, sizeof(u64), 1, stdout);
687-
fwrite(d, 1, l, stdout);
683+
fwrite(record->raw.iov_base, 1, record->raw.iov_len, stdout);
688684
} else {
689-
r = output_rr_packet(d, l, ifindex);
685+
r = output_rr_packet(rr, record->ifindex);
690686
if (r < 0)
691687
return r;
692688
}
693689

694-
if (dns_type_needs_authentication(t))
690+
if (dns_type_needs_authentication(rr->key->type))
695691
needs_authentication = true;
696-
697-
n++;
698692
}
699-
if (r < 0)
700-
return bus_log_parse_error(r);
701-
702-
r = sd_bus_message_exit_container(reply);
703-
if (r < 0)
704-
return bus_log_parse_error(r);
705-
706-
r = sd_bus_message_read(reply, "t", &flags);
707-
if (r < 0)
708-
return bus_log_parse_error(r);
709693

710-
if (n == 0) {
694+
if (reply.n_records == 0) {
711695
if (warn_missing)
712696
log_error("%s: no records found", name);
713697
return -ESRCH;
714698
}
715699

716-
print_source(flags, ts);
700+
print_source(reply.flags, ts);
717701

718-
if ((flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) {
702+
if ((reply.flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) {
719703
fflush(stdout);
720704

721705
fprintf(stderr, "\n%s"
@@ -730,12 +714,11 @@ static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_
730714
return 0;
731715
}
732716

733-
static int resolve_rfc4501(sd_bus *bus, const char *name) {
717+
static int resolve_rfc4501(const char *name) {
734718
uint16_t type = 0, class = 0;
735719
const char *p, *q, *n;
736720
int r;
737721

738-
assert(bus);
739722
assert(name);
740723
assert(startswith(name, "dns:"));
741724

@@ -833,7 +816,7 @@ static int resolve_rfc4501(sd_bus *bus, const char *name) {
833816
if (type == 0)
834817
type = arg_type ?: DNS_TYPE_A;
835818

836-
return resolve_record(bus, n, class, type, true);
819+
return resolve_record(n, class, type, true);
837820

838821
invalid:
839822
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
@@ -843,21 +826,16 @@ static int resolve_rfc4501(sd_bus *bus, const char *name) {
843826
VERB(verb_query, "query", "HOSTNAME|ADDRESS…", 2, VERB_ANY, 0,
844827
"Resolve domain names, IPv4 and IPv6 addresses");
845828
static int verb_query(int argc, char *argv[], uintptr_t _data, void *userdata) {
846-
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
847829
int ret = 0, r;
848830

849-
r = acquire_bus(&bus);
850-
if (r < 0)
851-
return r;
852-
853831
if (arg_type != 0)
854832
STRV_FOREACH(p, strv_skip(argv, 1))
855-
RET_GATHER(ret, resolve_record(bus, *p, arg_class, arg_type, true));
833+
RET_GATHER(ret, resolve_record(*p, arg_class, arg_type, true));
856834

857835
else
858836
STRV_FOREACH(p, strv_skip(argv, 1)) {
859837
if (startswith(*p, "dns:"))
860-
RET_GATHER(ret, resolve_rfc4501(bus, *p));
838+
RET_GATHER(ret, resolve_rfc4501(*p));
861839
else {
862840
int family, ifindex;
863841
union in_addr_union a;
@@ -1074,10 +1052,9 @@ static int verb_service(int argc, char *argv[], uintptr_t _data, void *userdata)
10741052
}
10751053

10761054
#if HAVE_OPENSSL
1077-
static int resolve_openpgp(sd_bus *bus, const char *address) {
1055+
static int resolve_openpgp(const char *address) {
10781056
int r;
10791057

1080-
assert(bus);
10811058
assert(address);
10821059

10831060
const char *domain = strrchr(address, '@');
@@ -1108,7 +1085,6 @@ static int resolve_openpgp(sd_bus *bus, const char *address) {
11081085
log_debug("Looking up \"%s\".", full);
11091086

11101087
r = resolve_record(
1111-
bus,
11121088
full,
11131089
arg_class ?: DNS_CLASS_IN,
11141090
arg_type ?: DNS_TYPE_OPENPGPKEY,
@@ -1128,7 +1104,6 @@ static int resolve_openpgp(sd_bus *bus, const char *address) {
11281104
log_debug("Looking up \"%s\".", full);
11291105

11301106
return resolve_record(
1131-
bus,
11321107
full,
11331108
arg_class ?: DNS_CLASS_IN,
11341109
arg_type ?: DNS_TYPE_OPENPGPKEY,
@@ -1140,32 +1115,26 @@ VERB(verb_openpgp, "openpgp", "EMAIL@DOMAIN…", 2, VERB_ANY, 0,
11401115
"Query OpenPGP public key");
11411116
static int verb_openpgp(int argc, char *argv[], uintptr_t _data, void *userdata) {
11421117
#if HAVE_OPENSSL
1143-
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1144-
int r, ret = 0;
1145-
1146-
r = acquire_bus(&bus);
1147-
if (r < 0)
1148-
return r;
1118+
int ret = 0;
11491119

11501120
if (sd_json_format_enabled(arg_json_format_flags))
11511121
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format.");
11521122

11531123
STRV_FOREACH(p, strv_skip(argv, 1))
1154-
RET_GATHER(ret, resolve_openpgp(bus, *p));
1124+
RET_GATHER(ret, resolve_openpgp(*p));
11551125

11561126
return ret;
11571127
#else
11581128
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "OpenSSL support is disabled, cannot query Open PGP keys.");
11591129
#endif
11601130
}
11611131

1162-
static int resolve_tlsa(sd_bus *bus, const char *family, const char *address) {
1132+
static int resolve_tlsa(const char *family, const char *address) {
11631133
const char *port;
11641134
uint16_t port_num = 443;
11651135
_cleanup_free_ char *full = NULL;
11661136
int r;
11671137

1168-
assert(bus);
11691138
assert(address);
11701139

11711140
port = strrchr(address, ':');
@@ -1186,7 +1155,7 @@ static int resolve_tlsa(sd_bus *bus, const char *family, const char *address) {
11861155

11871156
log_debug("Looking up \"%s\".", full);
11881157

1189-
return resolve_record(bus, full,
1158+
return resolve_record(full,
11901159
arg_class ?: DNS_CLASS_IN,
11911160
arg_type ?: DNS_TYPE_TLSA, true);
11921161
}
@@ -1198,17 +1167,12 @@ static bool service_family_is_valid(const char *s) {
11981167
VERB(verb_tlsa, "tlsa", "DOMAIN[:PORT]…", 2, VERB_ANY, 0,
11991168
"Query TLS public key");
12001169
static int verb_tlsa(int argc, char *argv[], uintptr_t _data, void *userdata) {
1201-
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
12021170
const char *family = "tcp";
12031171
char **args;
1204-
int r, ret = 0;
1172+
int ret = 0;
12051173

12061174
assert(argc >= 2);
12071175

1208-
r = acquire_bus(&bus);
1209-
if (r < 0)
1210-
return r;
1211-
12121176
if (sd_json_format_enabled(arg_json_format_flags))
12131177
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format.");
12141178

@@ -1219,7 +1183,7 @@ static int verb_tlsa(int argc, char *argv[], uintptr_t _data, void *userdata) {
12191183
args = strv_skip(argv, 1);
12201184

12211185
STRV_FOREACH(p, args)
1222-
RET_GATHER(ret, resolve_tlsa(bus, family, *p));
1186+
RET_GATHER(ret, resolve_tlsa(family, *p));
12231187

12241188
return ret;
12251189
}

0 commit comments

Comments
 (0)