]>
Commit | Line | Data |
---|---|---|
a032b68d | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
4c89c718 MP |
2 | |
3 | #include <getopt.h> | |
bb4f798a | 4 | #include <locale.h> |
4c89c718 MP |
5 | #include <net/if.h> |
6 | ||
7 | #include "sd-bus.h" | |
5a920b42 | 8 | #include "sd-netlink.h" |
4c89c718 MP |
9 | |
10 | #include "af-list.h" | |
11 | #include "alloc-util.h" | |
52ad194e | 12 | #include "bus-common-errors.h" |
4c89c718 | 13 | #include "bus-error.h" |
a10f5d05 MB |
14 | #include "bus-locator.h" |
15 | #include "bus-map-properties.h" | |
16 | #include "bus-message-util.h" | |
52ad194e | 17 | #include "dns-domain.h" |
4c89c718 | 18 | #include "escape.h" |
46cdbd49 | 19 | #include "format-table.h" |
f2dec872 | 20 | #include "format-util.h" |
aa27b158 | 21 | #include "gcrypt-util.h" |
3a6ce677 | 22 | #include "hostname-util.h" |
6e866b33 MB |
23 | #include "main-func.h" |
24 | #include "missing_network.h" | |
5a920b42 MP |
25 | #include "netlink-util.h" |
26 | #include "pager.h" | |
3a6ce677 | 27 | #include "parse-argument.h" |
4c89c718 | 28 | #include "parse-util.h" |
6e866b33 | 29 | #include "pretty-print.h" |
3a6ce677 | 30 | #include "process-util.h" |
b012e921 MB |
31 | #include "resolvconf-compat.h" |
32 | #include "resolvectl.h" | |
4c89c718 MP |
33 | #include "resolved-def.h" |
34 | #include "resolved-dns-packet.h" | |
3a6ce677 | 35 | #include "resolved-util.h" |
46cdbd49 | 36 | #include "socket-netlink.h" |
a10f5d05 | 37 | #include "sort-util.h" |
46cdbd49 | 38 | #include "stdio-util.h" |
b012e921 | 39 | #include "string-table.h" |
5a920b42 | 40 | #include "strv.h" |
4c89c718 | 41 | #include "terminal-util.h" |
a032b68d | 42 | #include "utf8.h" |
b012e921 | 43 | #include "verbs.h" |
4c89c718 | 44 | |
4c89c718 | 45 | static int arg_family = AF_UNSPEC; |
6e866b33 MB |
46 | static int arg_ifindex = 0; |
47 | static char *arg_ifname = NULL; | |
4c89c718 MP |
48 | static uint16_t arg_type = 0; |
49 | static uint16_t arg_class = 0; | |
50 | static bool arg_legend = true; | |
51 | static uint64_t arg_flags = 0; | |
6e866b33 | 52 | static PagerFlags arg_pager_flags = 0; |
b012e921 MB |
53 | bool arg_ifindex_permissive = false; /* If true, don't generate an error if the specified interface index doesn't exist */ |
54 | static const char *arg_service_family = NULL; | |
aa27b158 MP |
55 | |
56 | typedef enum RawType { | |
57 | RAW_NONE, | |
58 | RAW_PAYLOAD, | |
59 | RAW_PACKET, | |
60 | } RawType; | |
61 | static RawType arg_raw = RAW_NONE; | |
62 | ||
b012e921 | 63 | ExecutionMode arg_mode = MODE_RESOLVE_HOST; |
52ad194e | 64 | |
b012e921 MB |
65 | char **arg_set_dns = NULL; |
66 | char **arg_set_domain = NULL; | |
67 | static const char *arg_set_llmnr = NULL; | |
68 | static const char *arg_set_mdns = NULL; | |
69 | static const char *arg_set_dns_over_tls = NULL; | |
70 | static const char *arg_set_dnssec = NULL; | |
71 | static char **arg_set_nta = NULL; | |
aa27b158 | 72 | |
6e866b33 MB |
73 | STATIC_DESTRUCTOR_REGISTER(arg_ifname, freep); |
74 | STATIC_DESTRUCTOR_REGISTER(arg_set_dns, strv_freep); | |
75 | STATIC_DESTRUCTOR_REGISTER(arg_set_domain, strv_freep); | |
76 | STATIC_DESTRUCTOR_REGISTER(arg_set_nta, strv_freep); | |
77 | ||
b012e921 MB |
78 | typedef enum StatusMode { |
79 | STATUS_ALL, | |
80 | STATUS_DNS, | |
81 | STATUS_DOMAIN, | |
6e866b33 | 82 | STATUS_DEFAULT_ROUTE, |
b012e921 MB |
83 | STATUS_LLMNR, |
84 | STATUS_MDNS, | |
85 | STATUS_PRIVATE, | |
86 | STATUS_DNSSEC, | |
87 | STATUS_NTA, | |
88 | } StatusMode; | |
89 | ||
a10f5d05 MB |
90 | typedef struct InterfaceInfo { |
91 | int index; | |
92 | const char *name; | |
93 | } InterfaceInfo; | |
94 | ||
95 | static int interface_info_compare(const InterfaceInfo *a, const InterfaceInfo *b) { | |
96 | int r; | |
97 | ||
98 | r = CMP(a->index, b->index); | |
99 | if (r != 0) | |
100 | return r; | |
101 | ||
102 | return strcmp_ptr(a->name, b->name); | |
103 | } | |
104 | ||
6e866b33 MB |
105 | int ifname_mangle(const char *s) { |
106 | _cleanup_free_ char *iface = NULL; | |
107 | const char *dot; | |
46cdbd49 | 108 | int ifi; |
6e866b33 MB |
109 | |
110 | assert(s); | |
111 | ||
112 | dot = strchr(s, '.'); | |
113 | if (dot) { | |
114 | log_debug("Ignoring protocol specifier '%s'.", dot + 1); | |
115 | iface = strndup(s, dot - s); | |
116 | ||
117 | } else | |
118 | iface = strdup(s); | |
119 | if (!iface) | |
120 | return log_oom(); | |
121 | ||
8b3d4ff0 | 122 | ifi = rtnl_resolve_interface(NULL, iface); |
46cdbd49 BR |
123 | if (ifi < 0) { |
124 | if (ifi == -ENODEV && arg_ifindex_permissive) { | |
f2dec872 BR |
125 | log_debug("Interface '%s' not found, but -f specified, ignoring.", iface); |
126 | return 0; /* done */ | |
6e866b33 | 127 | } |
f2dec872 | 128 | |
46cdbd49 | 129 | return log_error_errno(ifi, "Failed to resolve interface \"%s\": %m", iface); |
6e866b33 MB |
130 | } |
131 | ||
bb4f798a MB |
132 | if (arg_ifindex > 0 && arg_ifindex != ifi) |
133 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified multiple different interfaces. Refusing."); | |
6e866b33 MB |
134 | |
135 | arg_ifindex = ifi; | |
136 | free_and_replace(arg_ifname, iface); | |
137 | ||
138 | return 1; | |
139 | } | |
140 | ||
4c89c718 MP |
141 | static void print_source(uint64_t flags, usec_t rtt) { |
142 | char rtt_str[FORMAT_TIMESTAMP_MAX]; | |
143 | ||
144 | if (!arg_legend) | |
145 | return; | |
146 | ||
147 | if (flags == 0) | |
148 | return; | |
149 | ||
6e866b33 | 150 | printf("\n%s-- Information acquired via", ansi_grey()); |
4c89c718 | 151 | |
9e294e28 MB |
152 | printf(" protocol%s%s%s%s%s", |
153 | flags & SD_RESOLVED_DNS ? " DNS" :"", | |
154 | flags & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "", | |
155 | flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "", | |
156 | flags & SD_RESOLVED_MDNS_IPV4 ? " mDNS/IPv4" : "", | |
157 | flags & SD_RESOLVED_MDNS_IPV6 ? " mDNS/IPv6" : ""); | |
4c89c718 MP |
158 | |
159 | assert_se(format_timespan(rtt_str, sizeof(rtt_str), rtt, 100)); | |
160 | ||
6e866b33 | 161 | printf(" in %s.%s\n" |
3a6ce677 | 162 | "%s-- Data is authenticated: %s; Data was acquired via local or encrypted transport: %s%s\n", |
6e866b33 | 163 | rtt_str, ansi_normal(), |
3a6ce677 BR |
164 | ansi_grey(), |
165 | yes_no(flags & SD_RESOLVED_AUTHENTICATED), | |
166 | yes_no(flags & SD_RESOLVED_CONFIDENTIAL), | |
167 | ansi_normal()); | |
168 | ||
169 | if ((flags & (SD_RESOLVED_FROM_MASK|SD_RESOLVED_SYNTHETIC)) != 0) | |
170 | printf("%s-- Data from:%s%s%s%s%s%s\n", | |
171 | ansi_grey(), | |
172 | FLAGS_SET(flags, SD_RESOLVED_SYNTHETIC) ? " synthetic" : "", | |
173 | FLAGS_SET(flags, SD_RESOLVED_FROM_CACHE) ? " cache" : "", | |
174 | FLAGS_SET(flags, SD_RESOLVED_FROM_ZONE) ? " zone" : "", | |
175 | FLAGS_SET(flags, SD_RESOLVED_FROM_TRUST_ANCHOR) ? " trust-anchor" : "", | |
176 | FLAGS_SET(flags, SD_RESOLVED_FROM_NETWORK) ? " network" : "", | |
177 | ansi_normal()); | |
6e866b33 | 178 | } |
4c89c718 | 179 | |
6e866b33 | 180 | static void print_ifindex_comment(int printed_so_far, int ifindex) { |
f2dec872 | 181 | char ifname[IF_NAMESIZE + 1]; |
4c89c718 | 182 | |
6e866b33 MB |
183 | if (ifindex <= 0) |
184 | return; | |
185 | ||
f2dec872 | 186 | if (!format_ifname(ifindex, ifname)) |
6e866b33 MB |
187 | log_warning_errno(errno, "Failed to resolve interface name for index %i, ignoring: %m", ifindex); |
188 | else | |
189 | printf("%*s%s-- link: %s%s", | |
190 | 60 > printed_so_far ? 60 - printed_so_far : 0, " ", /* Align comment to the 60th column */ | |
191 | ansi_grey(), ifname, ansi_normal()); | |
4c89c718 MP |
192 | } |
193 | ||
194 | static int resolve_host(sd_bus *bus, const char *name) { | |
4c89c718 MP |
195 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; |
196 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
197 | const char *canonical = NULL; | |
4c89c718 | 198 | unsigned c = 0; |
4c89c718 MP |
199 | uint64_t flags; |
200 | usec_t ts; | |
6e866b33 | 201 | int r; |
4c89c718 MP |
202 | |
203 | assert(name); | |
204 | ||
6e866b33 | 205 | log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); |
4c89c718 | 206 | |
a10f5d05 | 207 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveHostname"); |
4c89c718 MP |
208 | if (r < 0) |
209 | return bus_log_create_error(r); | |
210 | ||
211 | r = sd_bus_message_append(req, "isit", arg_ifindex, name, arg_family, arg_flags); | |
212 | if (r < 0) | |
213 | return bus_log_create_error(r); | |
214 | ||
215 | ts = now(CLOCK_MONOTONIC); | |
216 | ||
1d42b86d | 217 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); |
4c89c718 MP |
218 | if (r < 0) |
219 | return log_error_errno(r, "%s: resolve call failed: %s", name, bus_error_message(&error, r)); | |
220 | ||
221 | ts = now(CLOCK_MONOTONIC) - ts; | |
222 | ||
223 | r = sd_bus_message_enter_container(reply, 'a', "(iiay)"); | |
224 | if (r < 0) | |
225 | return bus_log_parse_error(r); | |
226 | ||
227 | while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) { | |
228 | _cleanup_free_ char *pretty = NULL; | |
6e866b33 | 229 | int ifindex, family, k; |
a10f5d05 | 230 | union in_addr_union a; |
4c89c718 MP |
231 | |
232 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
233 | ||
a10f5d05 | 234 | r = sd_bus_message_read(reply, "i", &ifindex); |
4c89c718 MP |
235 | if (r < 0) |
236 | return bus_log_parse_error(r); | |
237 | ||
a10f5d05 MB |
238 | sd_bus_error_free(&error); |
239 | r = bus_message_read_in_addr_auto(reply, &error, &family, &a); | |
240 | if (r < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) | |
241 | return log_error_errno(r, "%s: systemd-resolved returned invalid result: %s", name, bus_error_message(&error, r)); | |
4c89c718 MP |
242 | |
243 | r = sd_bus_message_exit_container(reply); | |
244 | if (r < 0) | |
245 | return bus_log_parse_error(r); | |
246 | ||
a10f5d05 MB |
247 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) { |
248 | log_debug_errno(r, "%s: systemd-resolved returned invalid result, ignoring: %s", name, bus_error_message(&error, r)); | |
4c89c718 MP |
249 | continue; |
250 | } | |
251 | ||
a10f5d05 | 252 | r = in_addr_ifindex_to_string(family, &a, ifindex, &pretty); |
4c89c718 MP |
253 | if (r < 0) |
254 | return log_error_errno(r, "Failed to print address for %s: %m", name); | |
255 | ||
6e866b33 MB |
256 | k = printf("%*s%s %s%s%s", |
257 | (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ", | |
258 | ansi_highlight(), pretty, ansi_normal()); | |
259 | ||
260 | print_ifindex_comment(k, ifindex); | |
261 | fputc('\n', stdout); | |
4c89c718 MP |
262 | |
263 | c++; | |
264 | } | |
265 | if (r < 0) | |
266 | return bus_log_parse_error(r); | |
267 | ||
268 | r = sd_bus_message_exit_container(reply); | |
269 | if (r < 0) | |
270 | return bus_log_parse_error(r); | |
271 | ||
272 | r = sd_bus_message_read(reply, "st", &canonical, &flags); | |
273 | if (r < 0) | |
274 | return bus_log_parse_error(r); | |
275 | ||
276 | if (!streq(name, canonical)) | |
277 | printf("%*s%s (%s)\n", | |
278 | (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ", | |
279 | canonical); | |
280 | ||
a032b68d MB |
281 | if (c == 0) |
282 | return log_error_errno(SYNTHETIC_ERRNO(ESRCH), | |
283 | "%s: no addresses found", name); | |
4c89c718 MP |
284 | |
285 | print_source(flags, ts); | |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
290 | static int resolve_address(sd_bus *bus, int family, const union in_addr_union *address, int ifindex) { | |
291 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; | |
292 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
293 | _cleanup_free_ char *pretty = NULL; | |
4c89c718 MP |
294 | uint64_t flags; |
295 | unsigned c = 0; | |
296 | usec_t ts; | |
297 | int r; | |
298 | ||
299 | assert(bus); | |
300 | assert(IN_SET(family, AF_INET, AF_INET6)); | |
301 | assert(address); | |
302 | ||
303 | if (ifindex <= 0) | |
304 | ifindex = arg_ifindex; | |
305 | ||
5a920b42 | 306 | r = in_addr_ifindex_to_string(family, address, ifindex, &pretty); |
4c89c718 MP |
307 | if (r < 0) |
308 | return log_oom(); | |
309 | ||
6e866b33 | 310 | log_debug("Resolving %s.", pretty); |
4c89c718 | 311 | |
a10f5d05 | 312 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveAddress"); |
4c89c718 MP |
313 | if (r < 0) |
314 | return bus_log_create_error(r); | |
315 | ||
316 | r = sd_bus_message_append(req, "ii", ifindex, family); | |
317 | if (r < 0) | |
318 | return bus_log_create_error(r); | |
319 | ||
320 | r = sd_bus_message_append_array(req, 'y', address, FAMILY_ADDRESS_SIZE(family)); | |
321 | if (r < 0) | |
322 | return bus_log_create_error(r); | |
323 | ||
324 | r = sd_bus_message_append(req, "t", arg_flags); | |
325 | if (r < 0) | |
326 | return bus_log_create_error(r); | |
327 | ||
328 | ts = now(CLOCK_MONOTONIC); | |
329 | ||
1d42b86d | 330 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); |
6e866b33 MB |
331 | if (r < 0) |
332 | return log_error_errno(r, "%s: resolve call failed: %s", pretty, bus_error_message(&error, r)); | |
4c89c718 MP |
333 | |
334 | ts = now(CLOCK_MONOTONIC) - ts; | |
335 | ||
336 | r = sd_bus_message_enter_container(reply, 'a', "(is)"); | |
337 | if (r < 0) | |
338 | return bus_log_create_error(r); | |
339 | ||
340 | while ((r = sd_bus_message_enter_container(reply, 'r', "is")) > 0) { | |
341 | const char *n; | |
6e866b33 | 342 | int k; |
4c89c718 MP |
343 | |
344 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
345 | ||
346 | r = sd_bus_message_read(reply, "is", &ifindex, &n); | |
347 | if (r < 0) | |
348 | return r; | |
349 | ||
350 | r = sd_bus_message_exit_container(reply); | |
351 | if (r < 0) | |
352 | return r; | |
353 | ||
6e866b33 MB |
354 | k = printf("%*s%s %s%s%s", |
355 | (int) strlen(pretty), c == 0 ? pretty : "", | |
356 | c == 0 ? ":" : " ", | |
357 | ansi_highlight(), n, ansi_normal()); | |
4c89c718 | 358 | |
6e866b33 MB |
359 | print_ifindex_comment(k, ifindex); |
360 | fputc('\n', stdout); | |
4c89c718 MP |
361 | |
362 | c++; | |
363 | } | |
364 | if (r < 0) | |
365 | return bus_log_parse_error(r); | |
366 | ||
367 | r = sd_bus_message_exit_container(reply); | |
368 | if (r < 0) | |
369 | return bus_log_parse_error(r); | |
370 | ||
371 | r = sd_bus_message_read(reply, "t", &flags); | |
372 | if (r < 0) | |
373 | return bus_log_parse_error(r); | |
374 | ||
a032b68d MB |
375 | if (c == 0) |
376 | return log_error_errno(SYNTHETIC_ERRNO(ESRCH), | |
377 | "%s: no names found", pretty); | |
4c89c718 MP |
378 | |
379 | print_source(flags, ts); | |
380 | ||
381 | return 0; | |
382 | } | |
383 | ||
aa27b158 MP |
384 | static int output_rr_packet(const void *d, size_t l, int ifindex) { |
385 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
386 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; | |
387 | int r; | |
aa27b158 | 388 | |
f5e65279 | 389 | r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX); |
aa27b158 MP |
390 | if (r < 0) |
391 | return log_oom(); | |
392 | ||
393 | p->refuse_compression = true; | |
394 | ||
395 | r = dns_packet_append_blob(p, d, l, NULL); | |
396 | if (r < 0) | |
397 | return log_oom(); | |
398 | ||
399 | r = dns_packet_read_rr(p, &rr, NULL, NULL); | |
400 | if (r < 0) | |
401 | return log_error_errno(r, "Failed to parse RR: %m"); | |
402 | ||
403 | if (arg_raw == RAW_PAYLOAD) { | |
404 | void *data; | |
405 | ssize_t k; | |
406 | ||
407 | k = dns_resource_record_payload(rr, &data); | |
408 | if (k < 0) | |
409 | return log_error_errno(k, "Cannot dump RR: %m"); | |
410 | fwrite(data, 1, k, stdout); | |
411 | } else { | |
412 | const char *s; | |
6e866b33 | 413 | int k; |
aa27b158 MP |
414 | |
415 | s = dns_resource_record_to_string(rr); | |
416 | if (!s) | |
417 | return log_oom(); | |
418 | ||
6e866b33 MB |
419 | k = printf("%s", s); |
420 | print_ifindex_comment(k, ifindex); | |
421 | fputc('\n', stdout); | |
aa27b158 MP |
422 | } |
423 | ||
424 | return 0; | |
425 | } | |
426 | ||
3a6ce677 BR |
427 | static int idna_candidate(const char *name, char **ret) { |
428 | _cleanup_free_ char *idnafied = NULL; | |
429 | int r; | |
430 | ||
431 | assert(name); | |
432 | assert(ret); | |
433 | ||
434 | r = dns_name_apply_idna(name, &idnafied); | |
435 | if (r < 0) | |
436 | return log_error_errno(r, "Failed to apply IDNA to name '%s': %m", name); | |
437 | if (r > 0 && !streq(name, idnafied)) { | |
438 | *ret = TAKE_PTR(idnafied); | |
439 | return true; | |
440 | } | |
441 | ||
442 | *ret = NULL; | |
443 | return false; | |
444 | } | |
445 | ||
446 | static bool single_label_nonsynthetic(const char *name) { | |
447 | _cleanup_free_ char *first_label = NULL; | |
448 | int r; | |
449 | ||
450 | if (!dns_name_is_single_label(name)) | |
451 | return false; | |
452 | ||
453 | if (is_localhost(name) || is_gateway_hostname(name)) | |
454 | return false; | |
455 | ||
456 | r = resolve_system_hostname(NULL, &first_label); | |
457 | if (r < 0) { | |
458 | log_warning_errno(r, "Failed to determine the hostname: %m"); | |
459 | return false; | |
460 | } | |
461 | ||
462 | return !streq(name, first_label); | |
463 | } | |
464 | ||
8a584da2 | 465 | static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type, bool warn_missing) { |
4c89c718 MP |
466 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; |
467 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3a6ce677 BR |
468 | _cleanup_free_ char *idnafied = NULL; |
469 | bool needs_authentication = false; | |
4c89c718 MP |
470 | unsigned n = 0; |
471 | uint64_t flags; | |
4c89c718 | 472 | usec_t ts; |
3a6ce677 | 473 | int r; |
4c89c718 MP |
474 | |
475 | assert(name); | |
476 | ||
6e866b33 | 477 | log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(class), dns_type_to_string(type), isempty(arg_ifname) ? "*" : arg_ifname); |
4c89c718 | 478 | |
3a6ce677 BR |
479 | if (dns_name_dot_suffixed(name) == 0 && single_label_nonsynthetic(name)) |
480 | log_notice("(Note that search domains are not appended when --type= is specified. " | |
481 | "Please specify fully qualified domain names, or remove --type= switch from invocation in order to request regular hostname resolution.)"); | |
482 | ||
483 | r = idna_candidate(name, &idnafied); | |
484 | if (r < 0) | |
485 | return r; | |
486 | if (r > 0) | |
487 | log_notice("(Note that IDNA translation is not applied when --type= is specified. " | |
488 | "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.", | |
489 | idnafied); | |
490 | ||
a10f5d05 | 491 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveRecord"); |
4c89c718 MP |
492 | if (r < 0) |
493 | return bus_log_create_error(r); | |
494 | ||
495 | r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, class, type, arg_flags); | |
496 | if (r < 0) | |
497 | return bus_log_create_error(r); | |
498 | ||
499 | ts = now(CLOCK_MONOTONIC); | |
500 | ||
1d42b86d | 501 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); |
4c89c718 | 502 | if (r < 0) { |
8a584da2 MP |
503 | if (warn_missing || r != -ENXIO) |
504 | log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r)); | |
4c89c718 MP |
505 | return r; |
506 | } | |
507 | ||
508 | ts = now(CLOCK_MONOTONIC) - ts; | |
509 | ||
510 | r = sd_bus_message_enter_container(reply, 'a', "(iqqay)"); | |
511 | if (r < 0) | |
512 | return bus_log_parse_error(r); | |
513 | ||
514 | while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) { | |
4c89c718 MP |
515 | uint16_t c, t; |
516 | int ifindex; | |
517 | const void *d; | |
518 | size_t l; | |
519 | ||
520 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
521 | ||
522 | r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t); | |
523 | if (r < 0) | |
524 | return bus_log_parse_error(r); | |
525 | ||
526 | r = sd_bus_message_read_array(reply, 'y', &d, &l); | |
527 | if (r < 0) | |
528 | return bus_log_parse_error(r); | |
529 | ||
530 | r = sd_bus_message_exit_container(reply); | |
531 | if (r < 0) | |
532 | return bus_log_parse_error(r); | |
533 | ||
aa27b158 MP |
534 | if (arg_raw == RAW_PACKET) { |
535 | uint64_t u64 = htole64(l); | |
4c89c718 | 536 | |
aa27b158 MP |
537 | fwrite(&u64, sizeof(u64), 1, stdout); |
538 | fwrite(d, 1, l, stdout); | |
539 | } else { | |
540 | r = output_rr_packet(d, l, ifindex); | |
541 | if (r < 0) | |
542 | return r; | |
543 | } | |
4c89c718 | 544 | |
aa27b158 MP |
545 | if (dns_type_needs_authentication(t)) |
546 | needs_authentication = true; | |
4c89c718 | 547 | |
4c89c718 MP |
548 | n++; |
549 | } | |
550 | if (r < 0) | |
551 | return bus_log_parse_error(r); | |
552 | ||
553 | r = sd_bus_message_exit_container(reply); | |
554 | if (r < 0) | |
555 | return bus_log_parse_error(r); | |
556 | ||
557 | r = sd_bus_message_read(reply, "t", &flags); | |
558 | if (r < 0) | |
559 | return bus_log_parse_error(r); | |
560 | ||
561 | if (n == 0) { | |
8a584da2 MP |
562 | if (warn_missing) |
563 | log_error("%s: no records found", name); | |
4c89c718 MP |
564 | return -ESRCH; |
565 | } | |
566 | ||
567 | print_source(flags, ts); | |
568 | ||
aa27b158 MP |
569 | if ((flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) { |
570 | fflush(stdout); | |
571 | ||
572 | fprintf(stderr, "\n%s" | |
573 | "WARNING: The resources shown contain cryptographic key data which could not be\n" | |
574 | " authenticated. It is not suitable to authenticate any communication.\n" | |
575 | " This is usually indication that DNSSEC authentication was not enabled\n" | |
576 | " or is not available for the selected protocol or DNS servers.%s\n", | |
577 | ansi_highlight_red(), | |
578 | ansi_normal()); | |
579 | } | |
580 | ||
4c89c718 MP |
581 | return 0; |
582 | } | |
583 | ||
584 | static int resolve_rfc4501(sd_bus *bus, const char *name) { | |
585 | uint16_t type = 0, class = 0; | |
586 | const char *p, *q, *n; | |
587 | int r; | |
588 | ||
589 | assert(bus); | |
590 | assert(name); | |
591 | assert(startswith(name, "dns:")); | |
592 | ||
593 | /* Parse RFC 4501 dns: URIs */ | |
594 | ||
595 | p = name + 4; | |
596 | ||
597 | if (p[0] == '/') { | |
598 | const char *e; | |
599 | ||
600 | if (p[1] != '/') | |
601 | goto invalid; | |
602 | ||
603 | e = strchr(p + 2, '/'); | |
604 | if (!e) | |
605 | goto invalid; | |
606 | ||
607 | if (e != p + 2) | |
608 | log_warning("DNS authority specification not supported; ignoring specified authority."); | |
609 | ||
610 | p = e + 1; | |
611 | } | |
612 | ||
613 | q = strchr(p, '?'); | |
614 | if (q) { | |
615 | n = strndupa(p, q - p); | |
616 | q++; | |
617 | ||
618 | for (;;) { | |
619 | const char *f; | |
620 | ||
621 | f = startswith_no_case(q, "class="); | |
622 | if (f) { | |
623 | _cleanup_free_ char *t = NULL; | |
624 | const char *e; | |
625 | ||
6e866b33 MB |
626 | if (class != 0) |
627 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
628 | "DNS class specified twice."); | |
4c89c718 MP |
629 | |
630 | e = strchrnul(f, ';'); | |
631 | t = strndup(f, e - f); | |
632 | if (!t) | |
633 | return log_oom(); | |
634 | ||
635 | r = dns_class_from_string(t); | |
6e866b33 | 636 | if (r < 0) |
3a6ce677 | 637 | return log_error_errno(r, "Unknown DNS class %s.", t); |
4c89c718 MP |
638 | |
639 | class = r; | |
640 | ||
641 | if (*e == ';') { | |
642 | q = e + 1; | |
643 | continue; | |
644 | } | |
645 | ||
646 | break; | |
647 | } | |
648 | ||
649 | f = startswith_no_case(q, "type="); | |
650 | if (f) { | |
651 | _cleanup_free_ char *t = NULL; | |
652 | const char *e; | |
653 | ||
6e866b33 MB |
654 | if (type != 0) |
655 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
656 | "DNS type specified twice."); | |
4c89c718 MP |
657 | |
658 | e = strchrnul(f, ';'); | |
659 | t = strndup(f, e - f); | |
660 | if (!t) | |
661 | return log_oom(); | |
662 | ||
663 | r = dns_type_from_string(t); | |
6e866b33 | 664 | if (r < 0) |
3a6ce677 | 665 | return log_error_errno(r, "Unknown DNS type %s: %m", t); |
4c89c718 MP |
666 | |
667 | type = r; | |
668 | ||
669 | if (*e == ';') { | |
670 | q = e + 1; | |
671 | continue; | |
672 | } | |
673 | ||
674 | break; | |
675 | } | |
676 | ||
677 | goto invalid; | |
678 | } | |
679 | } else | |
680 | n = p; | |
681 | ||
4c89c718 | 682 | if (class == 0) |
aa27b158 MP |
683 | class = arg_class ?: DNS_CLASS_IN; |
684 | if (type == 0) | |
685 | type = arg_type ?: DNS_TYPE_A; | |
4c89c718 | 686 | |
8a584da2 | 687 | return resolve_record(bus, n, class, type, true); |
4c89c718 MP |
688 | |
689 | invalid: | |
6e866b33 MB |
690 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
691 | "Invalid DNS URI: %s", name); | |
4c89c718 MP |
692 | } |
693 | ||
b012e921 MB |
694 | static int verb_query(int argc, char **argv, void *userdata) { |
695 | sd_bus *bus = userdata; | |
696 | char **p; | |
697 | int q, r = 0; | |
698 | ||
699 | if (arg_type != 0) | |
700 | STRV_FOREACH(p, argv + 1) { | |
701 | q = resolve_record(bus, *p, arg_class, arg_type, true); | |
702 | if (q < 0) | |
703 | r = q; | |
704 | } | |
705 | ||
706 | else | |
707 | STRV_FOREACH(p, argv + 1) { | |
708 | if (startswith(*p, "dns:")) | |
709 | q = resolve_rfc4501(bus, *p); | |
710 | else { | |
711 | int family, ifindex; | |
712 | union in_addr_union a; | |
713 | ||
714 | q = in_addr_ifindex_from_string_auto(*p, &family, &a, &ifindex); | |
715 | if (q >= 0) | |
716 | q = resolve_address(bus, family, &a, ifindex); | |
717 | else | |
718 | q = resolve_host(bus, *p); | |
719 | } | |
720 | if (q < 0) | |
721 | r = q; | |
722 | } | |
723 | ||
724 | return r; | |
725 | } | |
726 | ||
4c89c718 MP |
727 | static int resolve_service(sd_bus *bus, const char *name, const char *type, const char *domain) { |
728 | const char *canonical_name, *canonical_type, *canonical_domain; | |
729 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; | |
730 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
4c89c718 MP |
731 | size_t indent, sz; |
732 | uint64_t flags; | |
733 | const char *p; | |
734 | unsigned c; | |
735 | usec_t ts; | |
736 | int r; | |
737 | ||
738 | assert(bus); | |
739 | assert(domain); | |
740 | ||
5a920b42 MP |
741 | name = empty_to_null(name); |
742 | type = empty_to_null(type); | |
4c89c718 | 743 | |
4c89c718 | 744 | if (name) |
6e866b33 | 745 | log_debug("Resolving service \"%s\" of type %s in %s (family %s, interface %s).", name, type, domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); |
4c89c718 | 746 | else if (type) |
6e866b33 | 747 | log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); |
4c89c718 | 748 | else |
6e866b33 | 749 | log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); |
4c89c718 | 750 | |
a10f5d05 | 751 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveService"); |
4c89c718 MP |
752 | if (r < 0) |
753 | return bus_log_create_error(r); | |
754 | ||
755 | r = sd_bus_message_append(req, "isssit", arg_ifindex, name, type, domain, arg_family, arg_flags); | |
756 | if (r < 0) | |
757 | return bus_log_create_error(r); | |
758 | ||
759 | ts = now(CLOCK_MONOTONIC); | |
760 | ||
1d42b86d | 761 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); |
4c89c718 MP |
762 | if (r < 0) |
763 | return log_error_errno(r, "Resolve call failed: %s", bus_error_message(&error, r)); | |
764 | ||
765 | ts = now(CLOCK_MONOTONIC) - ts; | |
766 | ||
767 | r = sd_bus_message_enter_container(reply, 'a', "(qqqsa(iiay)s)"); | |
768 | if (r < 0) | |
769 | return bus_log_parse_error(r); | |
770 | ||
771 | indent = | |
772 | (name ? strlen(name) + 1 : 0) + | |
773 | (type ? strlen(type) + 1 : 0) + | |
774 | strlen(domain) + 2; | |
775 | ||
776 | c = 0; | |
777 | while ((r = sd_bus_message_enter_container(reply, 'r', "qqqsa(iiay)s")) > 0) { | |
778 | uint16_t priority, weight, port; | |
779 | const char *hostname, *canonical; | |
780 | ||
781 | r = sd_bus_message_read(reply, "qqqs", &priority, &weight, &port, &hostname); | |
782 | if (r < 0) | |
783 | return bus_log_parse_error(r); | |
784 | ||
785 | if (name) | |
786 | printf("%*s%s", (int) strlen(name), c == 0 ? name : "", c == 0 ? "/" : " "); | |
787 | if (type) | |
788 | printf("%*s%s", (int) strlen(type), c == 0 ? type : "", c == 0 ? "/" : " "); | |
789 | ||
790 | printf("%*s%s %s:%u [priority=%u, weight=%u]\n", | |
791 | (int) strlen(domain), c == 0 ? domain : "", | |
792 | c == 0 ? ":" : " ", | |
793 | hostname, port, | |
794 | priority, weight); | |
795 | ||
796 | r = sd_bus_message_enter_container(reply, 'a', "(iiay)"); | |
797 | if (r < 0) | |
798 | return bus_log_parse_error(r); | |
799 | ||
800 | while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) { | |
801 | _cleanup_free_ char *pretty = NULL; | |
6e866b33 | 802 | int ifindex, family, k; |
a10f5d05 | 803 | union in_addr_union a;; |
4c89c718 MP |
804 | |
805 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
806 | ||
a10f5d05 | 807 | r = sd_bus_message_read(reply, "i", &ifindex); |
4c89c718 MP |
808 | if (r < 0) |
809 | return bus_log_parse_error(r); | |
810 | ||
a10f5d05 MB |
811 | sd_bus_error_free(&error); |
812 | r = bus_message_read_in_addr_auto(reply, &error, &family, &a); | |
813 | if (r < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) | |
814 | return log_error_errno(r, "%s: systemd-resolved returned invalid result: %s", name, bus_error_message(&error, r)); | |
4c89c718 MP |
815 | |
816 | r = sd_bus_message_exit_container(reply); | |
817 | if (r < 0) | |
818 | return bus_log_parse_error(r); | |
819 | ||
a10f5d05 MB |
820 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) { |
821 | log_debug_errno(r, "%s: systemd-resolved returned invalid result, ignoring: %s", name, bus_error_message(&error, r)); | |
4c89c718 MP |
822 | continue; |
823 | } | |
824 | ||
a10f5d05 | 825 | r = in_addr_ifindex_to_string(family, &a, ifindex, &pretty); |
4c89c718 MP |
826 | if (r < 0) |
827 | return log_error_errno(r, "Failed to print address for %s: %m", name); | |
828 | ||
6e866b33 MB |
829 | k = printf("%*s%s", (int) indent, "", pretty); |
830 | print_ifindex_comment(k, ifindex); | |
831 | fputc('\n', stdout); | |
4c89c718 MP |
832 | } |
833 | if (r < 0) | |
834 | return bus_log_parse_error(r); | |
835 | ||
836 | r = sd_bus_message_exit_container(reply); | |
837 | if (r < 0) | |
838 | return bus_log_parse_error(r); | |
839 | ||
840 | r = sd_bus_message_read(reply, "s", &canonical); | |
841 | if (r < 0) | |
842 | return bus_log_parse_error(r); | |
843 | ||
844 | if (!streq(hostname, canonical)) | |
845 | printf("%*s(%s)\n", (int) indent, "", canonical); | |
846 | ||
847 | r = sd_bus_message_exit_container(reply); | |
848 | if (r < 0) | |
849 | return bus_log_parse_error(r); | |
850 | ||
851 | c++; | |
852 | } | |
853 | if (r < 0) | |
854 | return bus_log_parse_error(r); | |
855 | ||
856 | r = sd_bus_message_exit_container(reply); | |
857 | if (r < 0) | |
858 | return bus_log_parse_error(r); | |
859 | ||
860 | r = sd_bus_message_enter_container(reply, 'a', "ay"); | |
861 | if (r < 0) | |
862 | return bus_log_parse_error(r); | |
863 | ||
4c89c718 MP |
864 | while ((r = sd_bus_message_read_array(reply, 'y', (const void**) &p, &sz)) > 0) { |
865 | _cleanup_free_ char *escaped = NULL; | |
866 | ||
867 | escaped = cescape_length(p, sz); | |
868 | if (!escaped) | |
869 | return log_oom(); | |
870 | ||
871 | printf("%*s%s\n", (int) indent, "", escaped); | |
4c89c718 MP |
872 | } |
873 | if (r < 0) | |
874 | return bus_log_parse_error(r); | |
875 | ||
876 | r = sd_bus_message_exit_container(reply); | |
877 | if (r < 0) | |
878 | return bus_log_parse_error(r); | |
879 | ||
880 | r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags); | |
881 | if (r < 0) | |
882 | return bus_log_parse_error(r); | |
883 | ||
5a920b42 MP |
884 | canonical_name = empty_to_null(canonical_name); |
885 | canonical_type = empty_to_null(canonical_type); | |
4c89c718 MP |
886 | |
887 | if (!streq_ptr(name, canonical_name) || | |
888 | !streq_ptr(type, canonical_type) || | |
889 | !streq_ptr(domain, canonical_domain)) { | |
890 | ||
891 | printf("%*s(", (int) indent, ""); | |
892 | ||
893 | if (canonical_name) | |
894 | printf("%s/", canonical_name); | |
895 | if (canonical_type) | |
896 | printf("%s/", canonical_type); | |
897 | ||
898 | printf("%s)\n", canonical_domain); | |
899 | } | |
900 | ||
901 | print_source(flags, ts); | |
902 | ||
903 | return 0; | |
904 | } | |
905 | ||
b012e921 MB |
906 | static int verb_service(int argc, char **argv, void *userdata) { |
907 | sd_bus *bus = userdata; | |
908 | ||
909 | if (argc == 2) | |
910 | return resolve_service(bus, NULL, NULL, argv[1]); | |
911 | else if (argc == 3) | |
912 | return resolve_service(bus, NULL, argv[1], argv[2]); | |
913 | else | |
914 | return resolve_service(bus, argv[1], argv[2], argv[3]); | |
915 | } | |
916 | ||
aa27b158 MP |
917 | static int resolve_openpgp(sd_bus *bus, const char *address) { |
918 | const char *domain, *full; | |
919 | int r; | |
920 | _cleanup_free_ char *hashed = NULL; | |
921 | ||
922 | assert(bus); | |
923 | assert(address); | |
924 | ||
925 | domain = strrchr(address, '@'); | |
6e866b33 MB |
926 | if (!domain) |
927 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
928 | "Address does not contain '@': \"%s\"", address); | |
929 | if (domain == address || domain[1] == '\0') | |
930 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
931 | "Address starts or ends with '@': \"%s\"", address); | |
aa27b158 MP |
932 | domain++; |
933 | ||
8a584da2 | 934 | r = string_hashsum_sha256(address, domain - 1 - address, &hashed); |
aa27b158 MP |
935 | if (r < 0) |
936 | return log_error_errno(r, "Hashing failed: %m"); | |
937 | ||
8a584da2 MP |
938 | strshorten(hashed, 56); |
939 | ||
aa27b158 MP |
940 | full = strjoina(hashed, "._openpgpkey.", domain); |
941 | log_debug("Looking up \"%s\".", full); | |
942 | ||
8a584da2 MP |
943 | r = resolve_record(bus, full, |
944 | arg_class ?: DNS_CLASS_IN, | |
945 | arg_type ?: DNS_TYPE_OPENPGPKEY, false); | |
946 | ||
947 | if (IN_SET(r, -ENXIO, -ESRCH)) { /* NXDOMAIN or NODATA? */ | |
6e866b33 | 948 | hashed = mfree(hashed); |
8a584da2 MP |
949 | r = string_hashsum_sha224(address, domain - 1 - address, &hashed); |
950 | if (r < 0) | |
951 | return log_error_errno(r, "Hashing failed: %m"); | |
952 | ||
953 | full = strjoina(hashed, "._openpgpkey.", domain); | |
954 | log_debug("Looking up \"%s\".", full); | |
955 | ||
956 | return resolve_record(bus, full, | |
957 | arg_class ?: DNS_CLASS_IN, | |
958 | arg_type ?: DNS_TYPE_OPENPGPKEY, true); | |
959 | } | |
960 | ||
961 | return r; | |
aa27b158 MP |
962 | } |
963 | ||
b012e921 MB |
964 | static int verb_openpgp(int argc, char **argv, void *userdata) { |
965 | sd_bus *bus = userdata; | |
966 | char **p; | |
967 | int q, r = 0; | |
968 | ||
969 | STRV_FOREACH(p, argv + 1) { | |
970 | q = resolve_openpgp(bus, *p); | |
971 | if (q < 0) | |
972 | r = q; | |
973 | } | |
974 | ||
975 | return r; | |
976 | } | |
977 | ||
978 | static int resolve_tlsa(sd_bus *bus, const char *family, const char *address) { | |
aa27b158 MP |
979 | const char *port; |
980 | uint16_t port_num = 443; | |
981 | _cleanup_free_ char *full = NULL; | |
982 | int r; | |
983 | ||
984 | assert(bus); | |
985 | assert(address); | |
986 | ||
987 | port = strrchr(address, ':'); | |
988 | if (port) { | |
2897b343 MP |
989 | r = parse_ip_port(port + 1, &port_num); |
990 | if (r < 0) | |
aa27b158 MP |
991 | return log_error_errno(r, "Invalid port \"%s\".", port + 1); |
992 | ||
993 | address = strndupa(address, port - address); | |
994 | } | |
995 | ||
b012e921 | 996 | r = asprintf(&full, "_%u._%s.%s", |
aa27b158 | 997 | port_num, |
b012e921 | 998 | family, |
aa27b158 MP |
999 | address); |
1000 | if (r < 0) | |
1001 | return log_oom(); | |
1002 | ||
1003 | log_debug("Looking up \"%s\".", full); | |
1004 | ||
1005 | return resolve_record(bus, full, | |
1006 | arg_class ?: DNS_CLASS_IN, | |
8a584da2 | 1007 | arg_type ?: DNS_TYPE_TLSA, true); |
aa27b158 MP |
1008 | } |
1009 | ||
b012e921 MB |
1010 | static bool service_family_is_valid(const char *s) { |
1011 | return STR_IN_SET(s, "tcp", "udp", "sctp"); | |
1012 | } | |
1013 | ||
1014 | static int verb_tlsa(int argc, char **argv, void *userdata) { | |
1015 | sd_bus *bus = userdata; | |
1016 | char **p, **args = argv + 1; | |
1017 | const char *family = "tcp"; | |
1018 | int q, r = 0; | |
1019 | ||
1020 | if (service_family_is_valid(argv[1])) { | |
1021 | family = argv[1]; | |
1022 | args++; | |
1023 | } | |
1024 | ||
1025 | STRV_FOREACH(p, args) { | |
1026 | q = resolve_tlsa(bus, family, *p); | |
1027 | if (q < 0) | |
1028 | r = q; | |
1029 | } | |
1030 | ||
1031 | return r; | |
1032 | } | |
1033 | ||
1034 | static int show_statistics(int argc, char **argv, void *userdata) { | |
4c89c718 MP |
1035 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1036 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
46cdbd49 | 1037 | _cleanup_(table_unrefp) Table *table = NULL; |
b012e921 | 1038 | sd_bus *bus = userdata; |
4c89c718 MP |
1039 | uint64_t n_current_transactions, n_total_transactions, |
1040 | cache_size, n_cache_hit, n_cache_miss, | |
1041 | n_dnssec_secure, n_dnssec_insecure, n_dnssec_bogus, n_dnssec_indeterminate; | |
1042 | int r, dnssec_supported; | |
1043 | ||
1044 | assert(bus); | |
1045 | ||
a10f5d05 | 1046 | r = bus_get_property_trivial(bus, bus_resolve_mgr, "DNSSECSupported", &error, 'b', &dnssec_supported); |
4c89c718 MP |
1047 | if (r < 0) |
1048 | return log_error_errno(r, "Failed to get DNSSEC supported state: %s", bus_error_message(&error, r)); | |
1049 | ||
1050 | printf("DNSSEC supported by current servers: %s%s%s\n\n", | |
1051 | ansi_highlight(), | |
1052 | yes_no(dnssec_supported), | |
1053 | ansi_normal()); | |
1054 | ||
a10f5d05 | 1055 | r = bus_get_property(bus, bus_resolve_mgr, "TransactionStatistics", &error, &reply, "(tt)"); |
4c89c718 MP |
1056 | if (r < 0) |
1057 | return log_error_errno(r, "Failed to get transaction statistics: %s", bus_error_message(&error, r)); | |
1058 | ||
1059 | r = sd_bus_message_read(reply, "(tt)", | |
1060 | &n_current_transactions, | |
1061 | &n_total_transactions); | |
1062 | if (r < 0) | |
1063 | return bus_log_parse_error(r); | |
1064 | ||
4c89c718 MP |
1065 | reply = sd_bus_message_unref(reply); |
1066 | ||
a10f5d05 | 1067 | r = bus_get_property(bus, bus_resolve_mgr, "CacheStatistics", &error, &reply, "(ttt)"); |
4c89c718 MP |
1068 | if (r < 0) |
1069 | return log_error_errno(r, "Failed to get cache statistics: %s", bus_error_message(&error, r)); | |
1070 | ||
1071 | r = sd_bus_message_read(reply, "(ttt)", | |
1072 | &cache_size, | |
1073 | &n_cache_hit, | |
1074 | &n_cache_miss); | |
1075 | if (r < 0) | |
1076 | return bus_log_parse_error(r); | |
1077 | ||
4c89c718 MP |
1078 | reply = sd_bus_message_unref(reply); |
1079 | ||
a10f5d05 | 1080 | r = bus_get_property(bus, bus_resolve_mgr, "DNSSECStatistics", &error, &reply, "(tttt)"); |
4c89c718 MP |
1081 | if (r < 0) |
1082 | return log_error_errno(r, "Failed to get DNSSEC statistics: %s", bus_error_message(&error, r)); | |
1083 | ||
1084 | r = sd_bus_message_read(reply, "(tttt)", | |
1085 | &n_dnssec_secure, | |
1086 | &n_dnssec_insecure, | |
1087 | &n_dnssec_bogus, | |
1088 | &n_dnssec_indeterminate); | |
1089 | if (r < 0) | |
1090 | return bus_log_parse_error(r); | |
1091 | ||
46cdbd49 BR |
1092 | table = table_new("key", "value"); |
1093 | if (!table) | |
1094 | return log_oom(); | |
1095 | ||
1096 | table_set_header(table, false); | |
1097 | ||
1098 | r = table_add_many(table, | |
1099 | TABLE_STRING, "Transactions", | |
1100 | TABLE_SET_COLOR, ansi_highlight(), | |
1101 | TABLE_EMPTY, | |
1102 | TABLE_STRING, "Current Transactions:", | |
1103 | TABLE_SET_ALIGN_PERCENT, 100, | |
1104 | TABLE_UINT64, n_current_transactions, | |
1105 | TABLE_STRING, "Total Transactions:", | |
1106 | TABLE_UINT64, n_total_transactions, | |
1107 | TABLE_EMPTY, TABLE_EMPTY, | |
1108 | TABLE_STRING, "Cache", | |
1109 | TABLE_SET_COLOR, ansi_highlight(), | |
1110 | TABLE_SET_ALIGN_PERCENT, 0, | |
1111 | TABLE_EMPTY, | |
1112 | TABLE_STRING, "Current Cache Size:", | |
1113 | TABLE_SET_ALIGN_PERCENT, 100, | |
1114 | TABLE_UINT64, cache_size, | |
1115 | TABLE_STRING, "Cache Hits:", | |
1116 | TABLE_UINT64, n_cache_hit, | |
1117 | TABLE_STRING, "Cache Misses:", | |
1118 | TABLE_UINT64, n_cache_miss, | |
1119 | TABLE_EMPTY, TABLE_EMPTY, | |
1120 | TABLE_STRING, "DNSSEC Verdicts", | |
1121 | TABLE_SET_COLOR, ansi_highlight(), | |
1122 | TABLE_SET_ALIGN_PERCENT, 0, | |
1123 | TABLE_EMPTY, | |
1124 | TABLE_STRING, "Secure:", | |
1125 | TABLE_SET_ALIGN_PERCENT, 100, | |
1126 | TABLE_UINT64, n_dnssec_secure, | |
1127 | TABLE_STRING, "Insecure:", | |
1128 | TABLE_UINT64, n_dnssec_insecure, | |
1129 | TABLE_STRING, "Bogus:", | |
1130 | TABLE_UINT64, n_dnssec_bogus, | |
1131 | TABLE_STRING, "Indeterminate:", | |
1132 | TABLE_UINT64, n_dnssec_indeterminate); | |
1133 | if (r < 0) | |
1134 | table_log_add_error(r); | |
1135 | ||
1136 | r = table_print(table, NULL); | |
1137 | if (r < 0) | |
a10f5d05 | 1138 | return table_log_print_error(r); |
4c89c718 MP |
1139 | |
1140 | return 0; | |
1141 | } | |
1142 | ||
b012e921 | 1143 | static int reset_statistics(int argc, char **argv, void *userdata) { |
4c89c718 | 1144 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b012e921 | 1145 | sd_bus *bus = userdata; |
4c89c718 MP |
1146 | int r; |
1147 | ||
a10f5d05 | 1148 | r = bus_call_method(bus, bus_resolve_mgr, "ResetStatistics", &error, NULL, NULL); |
4c89c718 MP |
1149 | if (r < 0) |
1150 | return log_error_errno(r, "Failed to reset statistics: %s", bus_error_message(&error, r)); | |
1151 | ||
1152 | return 0; | |
1153 | } | |
1154 | ||
b012e921 | 1155 | static int flush_caches(int argc, char **argv, void *userdata) { |
5a920b42 | 1156 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b012e921 | 1157 | sd_bus *bus = userdata; |
5a920b42 MP |
1158 | int r; |
1159 | ||
a10f5d05 | 1160 | r = bus_call_method(bus, bus_resolve_mgr, "FlushCaches", &error, NULL, NULL); |
5a920b42 MP |
1161 | if (r < 0) |
1162 | return log_error_errno(r, "Failed to flush caches: %s", bus_error_message(&error, r)); | |
1163 | ||
1164 | return 0; | |
1165 | } | |
1166 | ||
b012e921 | 1167 | static int reset_server_features(int argc, char **argv, void *userdata) { |
f5e65279 | 1168 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b012e921 | 1169 | sd_bus *bus = userdata; |
f5e65279 MB |
1170 | int r; |
1171 | ||
a10f5d05 | 1172 | r = bus_call_method(bus, bus_resolve_mgr, "ResetServerFeatures", &error, NULL, NULL); |
f5e65279 MB |
1173 | if (r < 0) |
1174 | return log_error_errno(r, "Failed to reset server features: %s", bus_error_message(&error, r)); | |
1175 | ||
1176 | return 0; | |
1177 | } | |
1178 | ||
a10f5d05 MB |
1179 | static int read_dns_server_one(sd_bus_message *m, bool with_ifindex, bool extended, char **ret) { |
1180 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
b012e921 | 1181 | _cleanup_free_ char *pretty = NULL; |
a10f5d05 MB |
1182 | int ifindex, family, r, k; |
1183 | union in_addr_union a; | |
1184 | const char *name = NULL; | |
1185 | uint16_t port = 0; | |
b012e921 MB |
1186 | |
1187 | assert(m); | |
1188 | assert(ret); | |
1189 | ||
a10f5d05 | 1190 | r = sd_bus_message_enter_container(m, 'r', with_ifindex ? (extended ? "iiayqs" : "iiay") : (extended ? "iayqs" : "iay")); |
b012e921 MB |
1191 | if (r <= 0) |
1192 | return r; | |
1193 | ||
1194 | if (with_ifindex) { | |
1195 | r = sd_bus_message_read(m, "i", &ifindex); | |
1196 | if (r < 0) | |
1197 | return r; | |
1198 | } | |
1199 | ||
a10f5d05 MB |
1200 | k = bus_message_read_in_addr_auto(m, &error, &family, &a); |
1201 | if (k < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) | |
1202 | return k; | |
b012e921 | 1203 | |
a10f5d05 MB |
1204 | if (extended) { |
1205 | r = sd_bus_message_read(m, "q", &port); | |
1206 | if (r < 0) | |
1207 | return r; | |
1208 | ||
1209 | r = sd_bus_message_read(m, "s", &name); | |
1210 | if (r < 0) | |
1211 | return r; | |
1212 | } | |
b012e921 MB |
1213 | |
1214 | r = sd_bus_message_exit_container(m); | |
1215 | if (r < 0) | |
1216 | return r; | |
1217 | ||
a10f5d05 MB |
1218 | if (k < 0) { |
1219 | log_debug("Invalid DNS server, ignoring: %s", bus_error_message(&error, k)); | |
b012e921 MB |
1220 | *ret = NULL; |
1221 | return 1; | |
1222 | } | |
1223 | ||
a10f5d05 MB |
1224 | if (with_ifindex && ifindex != 0) { |
1225 | /* only show the global ones here */ | |
b012e921 MB |
1226 | *ret = NULL; |
1227 | return 1; | |
1228 | } | |
1229 | ||
a10f5d05 | 1230 | r = in_addr_port_ifindex_name_to_string(family, &a, port, ifindex, name, &pretty); |
b012e921 MB |
1231 | if (r < 0) |
1232 | return r; | |
1233 | ||
1234 | *ret = TAKE_PTR(pretty); | |
1235 | ||
1236 | return 1; | |
1237 | } | |
1238 | ||
a10f5d05 | 1239 | static int map_link_dns_servers_internal(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata, bool extended) { |
5a920b42 MP |
1240 | char ***l = userdata; |
1241 | int r; | |
1242 | ||
1243 | assert(bus); | |
1244 | assert(member); | |
1245 | assert(m); | |
1246 | assert(l); | |
1247 | ||
a10f5d05 | 1248 | r = sd_bus_message_enter_container(m, 'a', extended ? "(iayqs)" : "(iay)"); |
5a920b42 MP |
1249 | if (r < 0) |
1250 | return r; | |
1251 | ||
1252 | for (;;) { | |
f2dec872 | 1253 | _cleanup_free_ char *pretty = NULL; |
5a920b42 | 1254 | |
a10f5d05 | 1255 | r = read_dns_server_one(m, false, extended, &pretty); |
5a920b42 MP |
1256 | if (r < 0) |
1257 | return r; | |
1258 | if (r == 0) | |
1259 | break; | |
1260 | ||
b012e921 | 1261 | if (isempty(pretty)) |
5a920b42 | 1262 | continue; |
5a920b42 | 1263 | |
f2dec872 | 1264 | r = strv_consume(l, TAKE_PTR(pretty)); |
5a920b42 MP |
1265 | if (r < 0) |
1266 | return r; | |
1267 | } | |
1268 | ||
1269 | r = sd_bus_message_exit_container(m); | |
1270 | if (r < 0) | |
1271 | return r; | |
1272 | ||
1273 | return 0; | |
1274 | } | |
1275 | ||
a10f5d05 MB |
1276 | static int map_link_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1277 | return map_link_dns_servers_internal(bus, member, m, error, userdata, false); | |
1278 | } | |
1279 | ||
1280 | static int map_link_dns_servers_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1281 | return map_link_dns_servers_internal(bus, member, m, error, userdata, true); | |
1282 | } | |
1283 | ||
b012e921 MB |
1284 | static int map_link_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1285 | assert(m); | |
1286 | assert(userdata); | |
1287 | ||
a10f5d05 MB |
1288 | return read_dns_server_one(m, false, false, userdata); |
1289 | } | |
1290 | ||
1291 | static int map_link_current_dns_server_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1292 | assert(m); | |
1293 | assert(userdata); | |
1294 | ||
1295 | return read_dns_server_one(m, false, true, userdata); | |
b012e921 MB |
1296 | } |
1297 | ||
1298 | static int read_domain_one(sd_bus_message *m, bool with_ifindex, char **ret) { | |
1299 | _cleanup_free_ char *str = NULL; | |
1300 | int ifindex, route_only, r; | |
1301 | const char *domain; | |
1302 | ||
1303 | assert(m); | |
1304 | assert(ret); | |
1305 | ||
1306 | if (with_ifindex) | |
1307 | r = sd_bus_message_read(m, "(isb)", &ifindex, &domain, &route_only); | |
1308 | else | |
1309 | r = sd_bus_message_read(m, "(sb)", &domain, &route_only); | |
1310 | if (r <= 0) | |
1311 | return r; | |
1312 | ||
1313 | if (with_ifindex && ifindex != 0) { | |
1314 | /* only show the global ones here */ | |
1315 | *ret = NULL; | |
1316 | return 1; | |
1317 | } | |
1318 | ||
1319 | if (route_only) | |
f2dec872 | 1320 | str = strjoin("~", domain); |
b012e921 MB |
1321 | else |
1322 | str = strdup(domain); | |
1323 | if (!str) | |
1324 | return -ENOMEM; | |
1325 | ||
1326 | *ret = TAKE_PTR(str); | |
1327 | ||
1328 | return 1; | |
1329 | } | |
1330 | ||
5a920b42 MP |
1331 | static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1332 | char ***l = userdata; | |
1333 | int r; | |
1334 | ||
1335 | assert(bus); | |
1336 | assert(member); | |
1337 | assert(m); | |
1338 | assert(l); | |
1339 | ||
1340 | r = sd_bus_message_enter_container(m, 'a', "(sb)"); | |
1341 | if (r < 0) | |
1342 | return r; | |
1343 | ||
1344 | for (;;) { | |
f2dec872 | 1345 | _cleanup_free_ char *pretty = NULL; |
5a920b42 | 1346 | |
b012e921 | 1347 | r = read_domain_one(m, false, &pretty); |
5a920b42 MP |
1348 | if (r < 0) |
1349 | return r; | |
1350 | if (r == 0) | |
1351 | break; | |
1352 | ||
b012e921 MB |
1353 | if (isempty(pretty)) |
1354 | continue; | |
5a920b42 | 1355 | |
f2dec872 | 1356 | r = strv_consume(l, TAKE_PTR(pretty)); |
5a920b42 MP |
1357 | if (r < 0) |
1358 | return r; | |
1359 | } | |
1360 | ||
1361 | r = sd_bus_message_exit_container(m); | |
1362 | if (r < 0) | |
1363 | return r; | |
1364 | ||
a032b68d MB |
1365 | strv_sort(*l); |
1366 | ||
5a920b42 MP |
1367 | return 0; |
1368 | } | |
1369 | ||
b012e921 | 1370 | static int status_print_strv_ifindex(int ifindex, const char *ifname, char **p) { |
a032b68d MB |
1371 | const unsigned indent = strlen("Global: "); /* Use the same indentation everywhere to make things nice */ |
1372 | int pos1, pos2; | |
1373 | ||
1374 | if (ifname) | |
1375 | printf("%s%nLink %i (%s)%n%s:", ansi_highlight(), &pos1, ifindex, ifname, &pos2, ansi_normal()); | |
1376 | else | |
1377 | printf("%s%nGlobal%n%s:", ansi_highlight(), &pos1, &pos2, ansi_normal()); | |
1378 | ||
1379 | size_t cols = columns(), position = pos2 - pos1 + 2; | |
b012e921 MB |
1380 | char **i; |
1381 | ||
a032b68d MB |
1382 | STRV_FOREACH(i, p) { |
1383 | size_t our_len = utf8_console_width(*i); /* This returns -1 on invalid utf-8 (which shouldn't happen). | |
1384 | * If that happens, we'll just print one item per line. */ | |
b012e921 | 1385 | |
a032b68d MB |
1386 | if (position <= indent || size_add(size_add(position, 1), our_len) < cols) { |
1387 | printf(" %s", *i); | |
1388 | position = size_add(size_add(position, 1), our_len); | |
1389 | } else { | |
1390 | printf("\n%*s%s", indent, "", *i); | |
1391 | position = size_add(our_len, indent); | |
1392 | } | |
1393 | } | |
b012e921 MB |
1394 | |
1395 | printf("\n"); | |
1396 | ||
1397 | return 0; | |
1398 | } | |
1399 | ||
a032b68d MB |
1400 | static int status_print_strv_global(char **p) { |
1401 | return status_print_strv_ifindex(0, NULL, p); | |
1402 | } | |
1403 | ||
1404 | typedef struct LinkInfo { | |
6e866b33 MB |
1405 | uint64_t scopes_mask; |
1406 | const char *llmnr; | |
1407 | const char *mdns; | |
1408 | const char *dns_over_tls; | |
1409 | const char *dnssec; | |
1410 | char *current_dns; | |
a10f5d05 | 1411 | char *current_dns_ex; |
6e866b33 | 1412 | char **dns; |
a10f5d05 | 1413 | char **dns_ex; |
6e866b33 MB |
1414 | char **domains; |
1415 | char **ntas; | |
1416 | bool dnssec_supported; | |
1417 | bool default_route; | |
a032b68d MB |
1418 | } LinkInfo; |
1419 | ||
1420 | typedef struct GlobalInfo { | |
1421 | char *current_dns; | |
1422 | char *current_dns_ex; | |
1423 | char **dns; | |
1424 | char **dns_ex; | |
1425 | char **fallback_dns; | |
1426 | char **fallback_dns_ex; | |
1427 | char **domains; | |
1428 | char **ntas; | |
1429 | const char *llmnr; | |
1430 | const char *mdns; | |
1431 | const char *dns_over_tls; | |
1432 | const char *dnssec; | |
1433 | const char *resolv_conf_mode; | |
1434 | bool dnssec_supported; | |
1435 | } GlobalInfo; | |
6e866b33 | 1436 | |
a032b68d | 1437 | static void link_info_clear(LinkInfo *p) { |
6e866b33 | 1438 | free(p->current_dns); |
a10f5d05 | 1439 | free(p->current_dns_ex); |
6e866b33 | 1440 | strv_free(p->dns); |
a10f5d05 | 1441 | strv_free(p->dns_ex); |
6e866b33 MB |
1442 | strv_free(p->domains); |
1443 | strv_free(p->ntas); | |
1444 | } | |
5a920b42 | 1445 | |
a032b68d MB |
1446 | static void global_info_clear(GlobalInfo *p) { |
1447 | free(p->current_dns); | |
1448 | free(p->current_dns_ex); | |
1449 | strv_free(p->dns); | |
1450 | strv_free(p->dns_ex); | |
1451 | strv_free(p->fallback_dns); | |
1452 | strv_free(p->fallback_dns_ex); | |
1453 | strv_free(p->domains); | |
1454 | strv_free(p->ntas); | |
1455 | } | |
1456 | ||
46cdbd49 BR |
1457 | static int dump_list(Table *table, const char *prefix, char * const *l) { |
1458 | int r; | |
1459 | ||
1460 | if (strv_isempty(l)) | |
1461 | return 0; | |
1462 | ||
1463 | r = table_add_many(table, | |
1464 | TABLE_STRING, prefix, | |
a032b68d | 1465 | TABLE_STRV_WRAPPED, l); |
46cdbd49 BR |
1466 | if (r < 0) |
1467 | return table_log_add_error(r); | |
1468 | ||
1469 | return 0; | |
1470 | } | |
1471 | ||
a032b68d MB |
1472 | static int strv_extend_extended_bool(char ***strv, const char *name, const char *value) { |
1473 | int r; | |
1474 | ||
1475 | if (value) { | |
1476 | r = parse_boolean(value); | |
1477 | if (r >= 0) | |
1478 | return strv_extendf(strv, "%s%s", plus_minus(r), name); | |
1479 | } | |
1480 | ||
1481 | return strv_extendf(strv, "%s=%s", name, value ?: "???"); | |
1482 | } | |
1483 | ||
1484 | static char** link_protocol_status(const LinkInfo *info) { | |
1485 | _cleanup_strv_free_ char **s = NULL; | |
1486 | ||
1487 | if (strv_extendf(&s, "%sDefaultRoute", plus_minus(info->default_route)) < 0) | |
1488 | return NULL; | |
1489 | ||
1490 | if (strv_extend_extended_bool(&s, "LLMNR", info->llmnr) < 0) | |
1491 | return NULL; | |
1492 | ||
1493 | if (strv_extend_extended_bool(&s, "mDNS", info->mdns) < 0) | |
1494 | return NULL; | |
1495 | ||
1496 | if (strv_extend_extended_bool(&s, "DNSOverTLS", info->dns_over_tls) < 0) | |
1497 | return NULL; | |
1498 | ||
1499 | if (strv_extendf(&s, "DNSSEC=%s/%s", | |
1500 | info->dnssec ?: "???", | |
1501 | info->dnssec_supported ? "supported" : "unsupported") < 0) | |
1502 | return NULL; | |
1503 | ||
1504 | return TAKE_PTR(s); | |
1505 | } | |
1506 | ||
1507 | static char** global_protocol_status(const GlobalInfo *info) { | |
1508 | _cleanup_strv_free_ char **s = NULL; | |
1509 | ||
1510 | if (strv_extend_extended_bool(&s, "LLMNR", info->llmnr) < 0) | |
1511 | return NULL; | |
1512 | ||
1513 | if (strv_extend_extended_bool(&s, "mDNS", info->mdns) < 0) | |
1514 | return NULL; | |
1515 | ||
1516 | if (strv_extend_extended_bool(&s, "DNSOverTLS", info->dns_over_tls) < 0) | |
1517 | return NULL; | |
1518 | ||
1519 | if (strv_extendf(&s, "DNSSEC=%s/%s", | |
1520 | info->dnssec ?: "???", | |
1521 | info->dnssec_supported ? "supported" : "unsupported") < 0) | |
1522 | return NULL; | |
1523 | ||
1524 | return TAKE_PTR(s); | |
1525 | } | |
1526 | ||
6e866b33 | 1527 | static int status_ifindex(sd_bus *bus, int ifindex, const char *name, StatusMode mode, bool *empty_line) { |
5a920b42 | 1528 | static const struct bus_properties_map property_map[] = { |
a032b68d MB |
1529 | { "ScopesMask", "t", NULL, offsetof(LinkInfo, scopes_mask) }, |
1530 | { "DNS", "a(iay)", map_link_dns_servers, offsetof(LinkInfo, dns) }, | |
1531 | { "DNSEx", "a(iayqs)", map_link_dns_servers_ex, offsetof(LinkInfo, dns_ex) }, | |
1532 | { "CurrentDNSServer", "(iay)", map_link_current_dns_server, offsetof(LinkInfo, current_dns) }, | |
1533 | { "CurrentDNSServerEx", "(iayqs)", map_link_current_dns_server_ex, offsetof(LinkInfo, current_dns_ex) }, | |
1534 | { "Domains", "a(sb)", map_link_domains, offsetof(LinkInfo, domains) }, | |
1535 | { "DefaultRoute", "b", NULL, offsetof(LinkInfo, default_route) }, | |
1536 | { "LLMNR", "s", NULL, offsetof(LinkInfo, llmnr) }, | |
1537 | { "MulticastDNS", "s", NULL, offsetof(LinkInfo, mdns) }, | |
1538 | { "DNSOverTLS", "s", NULL, offsetof(LinkInfo, dns_over_tls) }, | |
1539 | { "DNSSEC", "s", NULL, offsetof(LinkInfo, dnssec) }, | |
1540 | { "DNSSECNegativeTrustAnchors", "as", bus_map_strv_sort, offsetof(LinkInfo, ntas) }, | |
1541 | { "DNSSECSupported", "b", NULL, offsetof(LinkInfo, dnssec_supported) }, | |
5a920b42 MP |
1542 | {} |
1543 | }; | |
2897b343 | 1544 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b012e921 | 1545 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
a032b68d | 1546 | _cleanup_(link_info_clear) LinkInfo link_info = {}; |
46cdbd49 BR |
1547 | _cleanup_(table_unrefp) Table *table = NULL; |
1548 | _cleanup_free_ char *p = NULL; | |
1549 | char ifi[DECIMAL_STR_MAX(int)], ifname[IF_NAMESIZE + 1] = ""; | |
5a920b42 MP |
1550 | int r; |
1551 | ||
1552 | assert(bus); | |
1553 | assert(ifindex > 0); | |
5a920b42 MP |
1554 | |
1555 | if (!name) { | |
f2dec872 | 1556 | if (!format_ifname(ifindex, ifname)) |
5a920b42 MP |
1557 | return log_error_errno(errno, "Failed to resolve interface name for %i: %m", ifindex); |
1558 | ||
1559 | name = ifname; | |
1560 | } | |
1561 | ||
46cdbd49 | 1562 | xsprintf(ifi, "%i", ifindex); |
5a920b42 MP |
1563 | r = sd_bus_path_encode("/org/freedesktop/resolve1/link", ifi, &p); |
1564 | if (r < 0) | |
1565 | return log_oom(); | |
1566 | ||
1567 | r = bus_map_all_properties(bus, | |
1568 | "org.freedesktop.resolve1", | |
1569 | p, | |
1570 | property_map, | |
b012e921 | 1571 | BUS_MAP_BOOLEAN_AS_BOOL, |
2897b343 | 1572 | &error, |
b012e921 | 1573 | &m, |
5a920b42 | 1574 | &link_info); |
6e866b33 MB |
1575 | if (r < 0) |
1576 | return log_error_errno(r, "Failed to get link data for %i: %s", ifindex, bus_error_message(&error, r)); | |
5a920b42 | 1577 | |
6e866b33 | 1578 | (void) pager_open(arg_pager_flags); |
b012e921 | 1579 | |
6e866b33 | 1580 | if (mode == STATUS_DNS) |
a10f5d05 | 1581 | return status_print_strv_ifindex(ifindex, name, link_info.dns_ex ?: link_info.dns); |
b012e921 | 1582 | |
6e866b33 MB |
1583 | if (mode == STATUS_DOMAIN) |
1584 | return status_print_strv_ifindex(ifindex, name, link_info.domains); | |
1585 | ||
1586 | if (mode == STATUS_NTA) | |
1587 | return status_print_strv_ifindex(ifindex, name, link_info.ntas); | |
b012e921 | 1588 | |
6e866b33 MB |
1589 | if (mode == STATUS_DEFAULT_ROUTE) { |
1590 | printf("%sLink %i (%s)%s: %s\n", | |
1591 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1592 | yes_no(link_info.default_route)); | |
1593 | ||
1594 | return 0; | |
b012e921 MB |
1595 | } |
1596 | ||
1597 | if (mode == STATUS_LLMNR) { | |
1598 | printf("%sLink %i (%s)%s: %s\n", | |
1599 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1600 | strna(link_info.llmnr)); | |
1601 | ||
6e866b33 | 1602 | return 0; |
b012e921 MB |
1603 | } |
1604 | ||
1605 | if (mode == STATUS_MDNS) { | |
1606 | printf("%sLink %i (%s)%s: %s\n", | |
1607 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1608 | strna(link_info.mdns)); | |
1609 | ||
6e866b33 | 1610 | return 0; |
b012e921 MB |
1611 | } |
1612 | ||
1613 | if (mode == STATUS_PRIVATE) { | |
1614 | printf("%sLink %i (%s)%s: %s\n", | |
1615 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1616 | strna(link_info.dns_over_tls)); | |
1617 | ||
6e866b33 | 1618 | return 0; |
b012e921 MB |
1619 | } |
1620 | ||
1621 | if (mode == STATUS_DNSSEC) { | |
1622 | printf("%sLink %i (%s)%s: %s\n", | |
1623 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1624 | strna(link_info.dnssec)); | |
1625 | ||
6e866b33 | 1626 | return 0; |
b012e921 | 1627 | } |
5a920b42 | 1628 | |
b012e921 | 1629 | if (empty_line && *empty_line) |
5a920b42 MP |
1630 | fputc('\n', stdout); |
1631 | ||
1632 | printf("%sLink %i (%s)%s\n", | |
1633 | ansi_highlight(), ifindex, name, ansi_normal()); | |
1634 | ||
46cdbd49 BR |
1635 | table = table_new("key", "value"); |
1636 | if (!table) | |
1637 | return log_oom(); | |
1638 | ||
1639 | table_set_header(table, false); | |
1640 | ||
1641 | r = table_add_many(table, | |
1642 | TABLE_STRING, "Current Scopes:", | |
1643 | TABLE_SET_ALIGN_PERCENT, 100); | |
1644 | if (r < 0) | |
1645 | return table_log_add_error(r); | |
1646 | ||
5a920b42 | 1647 | if (link_info.scopes_mask == 0) |
46cdbd49 BR |
1648 | r = table_add_cell(table, NULL, TABLE_STRING, "none"); |
1649 | else { | |
1650 | _cleanup_free_ char *buf = NULL; | |
1651 | size_t len; | |
1652 | ||
1653 | if (asprintf(&buf, "%s%s%s%s%s", | |
1654 | link_info.scopes_mask & SD_RESOLVED_DNS ? "DNS " : "", | |
1655 | link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV4 ? "LLMNR/IPv4 " : "", | |
1656 | link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV6 ? "LLMNR/IPv6 " : "", | |
1657 | link_info.scopes_mask & SD_RESOLVED_MDNS_IPV4 ? "mDNS/IPv4 " : "", | |
1658 | link_info.scopes_mask & SD_RESOLVED_MDNS_IPV6 ? "mDNS/IPv6 " : "") < 0) | |
1659 | return log_oom(); | |
5a920b42 | 1660 | |
46cdbd49 BR |
1661 | len = strlen(buf); |
1662 | assert(len > 0); | |
1663 | buf[len - 1] = '\0'; | |
5a920b42 | 1664 | |
46cdbd49 BR |
1665 | r = table_add_cell(table, NULL, TABLE_STRING, buf); |
1666 | } | |
1667 | if (r < 0) | |
1668 | return table_log_add_error(r); | |
1669 | ||
a032b68d MB |
1670 | _cleanup_strv_free_ char **pstatus = link_protocol_status(&link_info); |
1671 | if (!pstatus) | |
1672 | return log_oom(); | |
1673 | ||
46cdbd49 | 1674 | r = table_add_many(table, |
a032b68d MB |
1675 | TABLE_STRING, "Protocols:", |
1676 | TABLE_STRV_WRAPPED, pstatus); | |
46cdbd49 BR |
1677 | if (r < 0) |
1678 | return table_log_add_error(r); | |
1679 | ||
1680 | if (link_info.current_dns) { | |
1681 | r = table_add_many(table, | |
1682 | TABLE_STRING, "Current DNS Server:", | |
a10f5d05 | 1683 | TABLE_STRING, link_info.current_dns_ex ?: link_info.current_dns); |
46cdbd49 BR |
1684 | if (r < 0) |
1685 | return table_log_add_error(r); | |
5a920b42 MP |
1686 | } |
1687 | ||
a10f5d05 | 1688 | r = dump_list(table, "DNS Servers:", link_info.dns_ex ?: link_info.dns); |
46cdbd49 BR |
1689 | if (r < 0) |
1690 | return r; | |
1691 | ||
1692 | r = dump_list(table, "DNS Domain:", link_info.domains); | |
1693 | if (r < 0) | |
1694 | return r; | |
1695 | ||
46cdbd49 BR |
1696 | r = table_print(table, NULL); |
1697 | if (r < 0) | |
a10f5d05 | 1698 | return table_log_print_error(r); |
46cdbd49 | 1699 | |
b012e921 MB |
1700 | if (empty_line) |
1701 | *empty_line = true; | |
5a920b42 | 1702 | |
6e866b33 | 1703 | return 0; |
5a920b42 MP |
1704 | } |
1705 | ||
a10f5d05 | 1706 | static int map_global_dns_servers_internal(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata, bool extended) { |
5a920b42 MP |
1707 | char ***l = userdata; |
1708 | int r; | |
1709 | ||
1710 | assert(bus); | |
1711 | assert(member); | |
1712 | assert(m); | |
1713 | assert(l); | |
1714 | ||
a10f5d05 | 1715 | r = sd_bus_message_enter_container(m, 'a', extended ? "(iiayqs)" : "(iiay)"); |
5a920b42 MP |
1716 | if (r < 0) |
1717 | return r; | |
1718 | ||
1719 | for (;;) { | |
f2dec872 | 1720 | _cleanup_free_ char *pretty = NULL; |
5a920b42 | 1721 | |
a10f5d05 | 1722 | r = read_dns_server_one(m, true, extended, &pretty); |
5a920b42 MP |
1723 | if (r < 0) |
1724 | return r; | |
1725 | if (r == 0) | |
1726 | break; | |
1727 | ||
b012e921 | 1728 | if (isempty(pretty)) |
5a920b42 | 1729 | continue; |
5a920b42 | 1730 | |
f2dec872 | 1731 | r = strv_consume(l, TAKE_PTR(pretty)); |
5a920b42 MP |
1732 | if (r < 0) |
1733 | return r; | |
1734 | } | |
1735 | ||
1736 | r = sd_bus_message_exit_container(m); | |
1737 | if (r < 0) | |
1738 | return r; | |
1739 | ||
1740 | return 0; | |
1741 | } | |
1742 | ||
a10f5d05 MB |
1743 | static int map_global_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1744 | return map_global_dns_servers_internal(bus, member, m, error, userdata, false); | |
1745 | } | |
1746 | ||
1747 | static int map_global_dns_servers_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1748 | return map_global_dns_servers_internal(bus, member, m, error, userdata, true); | |
1749 | } | |
1750 | ||
b012e921 MB |
1751 | static int map_global_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1752 | assert(m); | |
1753 | assert(userdata); | |
1754 | ||
a10f5d05 MB |
1755 | return read_dns_server_one(m, true, false, userdata); |
1756 | } | |
1757 | ||
1758 | static int map_global_current_dns_server_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1759 | assert(m); | |
1760 | assert(userdata); | |
1761 | ||
1762 | return read_dns_server_one(m, true, true, userdata); | |
b012e921 MB |
1763 | } |
1764 | ||
5a920b42 MP |
1765 | static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1766 | char ***l = userdata; | |
1767 | int r; | |
1768 | ||
1769 | assert(bus); | |
1770 | assert(member); | |
1771 | assert(m); | |
1772 | assert(l); | |
1773 | ||
1774 | r = sd_bus_message_enter_container(m, 'a', "(isb)"); | |
1775 | if (r < 0) | |
1776 | return r; | |
1777 | ||
1778 | for (;;) { | |
f2dec872 | 1779 | _cleanup_free_ char *pretty = NULL; |
5a920b42 | 1780 | |
b012e921 | 1781 | r = read_domain_one(m, true, &pretty); |
5a920b42 MP |
1782 | if (r < 0) |
1783 | return r; | |
1784 | if (r == 0) | |
1785 | break; | |
1786 | ||
b012e921 | 1787 | if (isempty(pretty)) |
5a920b42 MP |
1788 | continue; |
1789 | ||
f2dec872 | 1790 | r = strv_consume(l, TAKE_PTR(pretty)); |
5a920b42 MP |
1791 | if (r < 0) |
1792 | return r; | |
1793 | } | |
1794 | ||
1795 | r = sd_bus_message_exit_container(m); | |
1796 | if (r < 0) | |
1797 | return r; | |
1798 | ||
a032b68d | 1799 | strv_sort(*l); |
b012e921 MB |
1800 | |
1801 | return 0; | |
1802 | } | |
1803 | ||
6e866b33 | 1804 | static int status_global(sd_bus *bus, StatusMode mode, bool *empty_line) { |
5a920b42 | 1805 | static const struct bus_properties_map property_map[] = { |
a032b68d MB |
1806 | { "DNS", "a(iiay)", map_global_dns_servers, offsetof(GlobalInfo, dns) }, |
1807 | { "DNSEx", "a(iiayqs)", map_global_dns_servers_ex, offsetof(GlobalInfo, dns_ex) }, | |
1808 | { "FallbackDNS", "a(iiay)", map_global_dns_servers, offsetof(GlobalInfo, fallback_dns) }, | |
1809 | { "FallbackDNSEx", "a(iiayqs)", map_global_dns_servers_ex, offsetof(GlobalInfo, fallback_dns_ex) }, | |
1810 | { "CurrentDNSServer", "(iiay)", map_global_current_dns_server, offsetof(GlobalInfo, current_dns) }, | |
1811 | { "CurrentDNSServerEx", "(iiayqs)", map_global_current_dns_server_ex, offsetof(GlobalInfo, current_dns_ex) }, | |
1812 | { "Domains", "a(isb)", map_global_domains, offsetof(GlobalInfo, domains) }, | |
1813 | { "DNSSECNegativeTrustAnchors", "as", bus_map_strv_sort, offsetof(GlobalInfo, ntas) }, | |
1814 | { "LLMNR", "s", NULL, offsetof(GlobalInfo, llmnr) }, | |
1815 | { "MulticastDNS", "s", NULL, offsetof(GlobalInfo, mdns) }, | |
1816 | { "DNSOverTLS", "s", NULL, offsetof(GlobalInfo, dns_over_tls) }, | |
1817 | { "DNSSEC", "s", NULL, offsetof(GlobalInfo, dnssec) }, | |
1818 | { "DNSSECSupported", "b", NULL, offsetof(GlobalInfo, dnssec_supported) }, | |
1819 | { "ResolvConfMode", "s", NULL, offsetof(GlobalInfo, resolv_conf_mode) }, | |
5a920b42 MP |
1820 | {} |
1821 | }; | |
2897b343 | 1822 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b012e921 | 1823 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
a032b68d | 1824 | _cleanup_(global_info_clear) GlobalInfo global_info = {}; |
46cdbd49 | 1825 | _cleanup_(table_unrefp) Table *table = NULL; |
5a920b42 MP |
1826 | int r; |
1827 | ||
1828 | assert(bus); | |
1829 | assert(empty_line); | |
1830 | ||
1831 | r = bus_map_all_properties(bus, | |
1832 | "org.freedesktop.resolve1", | |
1833 | "/org/freedesktop/resolve1", | |
1834 | property_map, | |
b012e921 | 1835 | BUS_MAP_BOOLEAN_AS_BOOL, |
2897b343 | 1836 | &error, |
b012e921 | 1837 | &m, |
5a920b42 | 1838 | &global_info); |
6e866b33 MB |
1839 | if (r < 0) |
1840 | return log_error_errno(r, "Failed to get global data: %s", bus_error_message(&error, r)); | |
5a920b42 | 1841 | |
6e866b33 | 1842 | (void) pager_open(arg_pager_flags); |
b012e921 | 1843 | |
6e866b33 | 1844 | if (mode == STATUS_DNS) |
a10f5d05 | 1845 | return status_print_strv_global(global_info.dns_ex ?: global_info.dns); |
b012e921 | 1846 | |
6e866b33 MB |
1847 | if (mode == STATUS_DOMAIN) |
1848 | return status_print_strv_global(global_info.domains); | |
b012e921 | 1849 | |
6e866b33 MB |
1850 | if (mode == STATUS_NTA) |
1851 | return status_print_strv_global(global_info.ntas); | |
b012e921 MB |
1852 | |
1853 | if (mode == STATUS_LLMNR) { | |
1854 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), | |
1855 | strna(global_info.llmnr)); | |
1856 | ||
6e866b33 | 1857 | return 0; |
b012e921 MB |
1858 | } |
1859 | ||
1860 | if (mode == STATUS_MDNS) { | |
1861 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), | |
1862 | strna(global_info.mdns)); | |
1863 | ||
6e866b33 | 1864 | return 0; |
b012e921 MB |
1865 | } |
1866 | ||
1867 | if (mode == STATUS_PRIVATE) { | |
1868 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), | |
1869 | strna(global_info.dns_over_tls)); | |
1870 | ||
6e866b33 | 1871 | return 0; |
5a920b42 MP |
1872 | } |
1873 | ||
b012e921 MB |
1874 | if (mode == STATUS_DNSSEC) { |
1875 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), | |
1876 | strna(global_info.dnssec)); | |
1877 | ||
6e866b33 | 1878 | return 0; |
b012e921 | 1879 | } |
5a920b42 MP |
1880 | |
1881 | printf("%sGlobal%s\n", ansi_highlight(), ansi_normal()); | |
b012e921 | 1882 | |
46cdbd49 BR |
1883 | table = table_new("key", "value"); |
1884 | if (!table) | |
1885 | return log_oom(); | |
5a920b42 | 1886 | |
46cdbd49 BR |
1887 | table_set_header(table, false); |
1888 | ||
a032b68d MB |
1889 | _cleanup_strv_free_ char **pstatus = global_protocol_status(&global_info); |
1890 | if (!pstatus) | |
1891 | return log_oom(); | |
1892 | ||
46cdbd49 | 1893 | r = table_add_many(table, |
a032b68d | 1894 | TABLE_STRING, "Protocols:", |
46cdbd49 | 1895 | TABLE_SET_ALIGN_PERCENT, 100, |
a032b68d | 1896 | TABLE_STRV_WRAPPED, pstatus); |
46cdbd49 BR |
1897 | if (r < 0) |
1898 | return table_log_add_error(r); | |
1899 | ||
a032b68d MB |
1900 | if (global_info.resolv_conf_mode) { |
1901 | r = table_add_many(table, | |
1902 | TABLE_STRING, "resolv.conf mode:", | |
1903 | TABLE_STRING, global_info.resolv_conf_mode); | |
1904 | if (r < 0) | |
1905 | return table_log_add_error(r); | |
1906 | } | |
1907 | ||
46cdbd49 BR |
1908 | if (global_info.current_dns) { |
1909 | r = table_add_many(table, | |
1910 | TABLE_STRING, "Current DNS Server:", | |
a10f5d05 | 1911 | TABLE_STRING, global_info.current_dns_ex ?: global_info.current_dns); |
46cdbd49 BR |
1912 | if (r < 0) |
1913 | return table_log_add_error(r); | |
b012e921 MB |
1914 | } |
1915 | ||
a10f5d05 | 1916 | r = dump_list(table, "DNS Servers:", global_info.dns_ex ?: global_info.dns); |
46cdbd49 BR |
1917 | if (r < 0) |
1918 | return r; | |
1919 | ||
a10f5d05 | 1920 | r = dump_list(table, "Fallback DNS Servers:", global_info.fallback_dns_ex ?: global_info.fallback_dns); |
46cdbd49 BR |
1921 | if (r < 0) |
1922 | return r; | |
1923 | ||
1924 | r = dump_list(table, "DNS Domain:", global_info.domains); | |
1925 | if (r < 0) | |
1926 | return r; | |
5a920b42 | 1927 | |
46cdbd49 BR |
1928 | r = table_print(table, NULL); |
1929 | if (r < 0) | |
a10f5d05 | 1930 | return table_log_print_error(r); |
5a920b42 MP |
1931 | |
1932 | *empty_line = true; | |
1933 | ||
6e866b33 | 1934 | return 0; |
5a920b42 MP |
1935 | } |
1936 | ||
b012e921 | 1937 | static int status_all(sd_bus *bus, StatusMode mode) { |
5a920b42 MP |
1938 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
1939 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
5a920b42 MP |
1940 | bool empty_line = false; |
1941 | int r; | |
1942 | ||
1943 | assert(bus); | |
1944 | ||
b012e921 | 1945 | r = status_global(bus, mode, &empty_line); |
5a920b42 MP |
1946 | if (r < 0) |
1947 | return r; | |
1948 | ||
1949 | r = sd_netlink_open(&rtnl); | |
1950 | if (r < 0) | |
1951 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
1952 | ||
1953 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0); | |
1954 | if (r < 0) | |
1955 | return rtnl_log_create_error(r); | |
1956 | ||
1957 | r = sd_netlink_message_request_dump(req, true); | |
1958 | if (r < 0) | |
1959 | return rtnl_log_create_error(r); | |
1960 | ||
1961 | r = sd_netlink_call(rtnl, req, 0, &reply); | |
1962 | if (r < 0) | |
1963 | return log_error_errno(r, "Failed to enumerate links: %m"); | |
1964 | ||
a10f5d05 | 1965 | _cleanup_free_ InterfaceInfo *infos = NULL; |
8b3d4ff0 | 1966 | size_t n_infos = 0; |
a10f5d05 MB |
1967 | |
1968 | for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) { | |
5a920b42 | 1969 | const char *name; |
a10f5d05 | 1970 | int ifindex; |
5a920b42 MP |
1971 | uint16_t type; |
1972 | ||
a10f5d05 MB |
1973 | r = sd_netlink_message_get_type(i, &type); |
1974 | if (r < 0) | |
1975 | return rtnl_log_parse_error(r); | |
5a920b42 MP |
1976 | |
1977 | if (type != RTM_NEWLINK) | |
1978 | continue; | |
1979 | ||
a10f5d05 MB |
1980 | r = sd_rtnl_message_link_get_ifindex(i, &ifindex); |
1981 | if (r < 0) | |
1982 | return rtnl_log_parse_error(r); | |
5a920b42 MP |
1983 | |
1984 | if (ifindex == LOOPBACK_IFINDEX) | |
1985 | continue; | |
1986 | ||
a10f5d05 MB |
1987 | r = sd_netlink_message_read_string(i, IFLA_IFNAME, &name); |
1988 | if (r < 0) | |
1989 | return rtnl_log_parse_error(r); | |
1990 | ||
8b3d4ff0 | 1991 | if (!GREEDY_REALLOC(infos, n_infos + 1)) |
a10f5d05 MB |
1992 | return log_oom(); |
1993 | ||
1994 | infos[n_infos++] = (InterfaceInfo) { ifindex, name }; | |
1995 | } | |
1996 | ||
1997 | typesafe_qsort(infos, n_infos, interface_info_compare); | |
5a920b42 | 1998 | |
a10f5d05 MB |
1999 | r = 0; |
2000 | for (size_t i = 0; i < n_infos; i++) { | |
2001 | int q = status_ifindex(bus, infos[i].index, infos[i].name, mode, &empty_line); | |
5a920b42 MP |
2002 | if (q < 0 && r >= 0) |
2003 | r = q; | |
2004 | } | |
2005 | ||
2006 | return r; | |
2007 | } | |
2008 | ||
b012e921 MB |
2009 | static int verb_status(int argc, char **argv, void *userdata) { |
2010 | sd_bus *bus = userdata; | |
46cdbd49 BR |
2011 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
2012 | int r = 0; | |
52ad194e | 2013 | |
b012e921 MB |
2014 | if (argc > 1) { |
2015 | char **ifname; | |
2016 | bool empty_line = false; | |
52ad194e | 2017 | |
b012e921 | 2018 | STRV_FOREACH(ifname, argv + 1) { |
46cdbd49 | 2019 | int ifindex, q; |
52ad194e | 2020 | |
8b3d4ff0 | 2021 | ifindex = rtnl_resolve_interface(&rtnl, *ifname); |
46cdbd49 BR |
2022 | if (ifindex < 0) { |
2023 | log_warning_errno(ifindex, "Failed to resolve interface \"%s\", ignoring: %m", *ifname); | |
b012e921 | 2024 | continue; |
f2dec872 | 2025 | } |
52ad194e | 2026 | |
b012e921 | 2027 | q = status_ifindex(bus, ifindex, NULL, STATUS_ALL, &empty_line); |
52ad194e | 2028 | if (q < 0) |
b012e921 MB |
2029 | r = q; |
2030 | } | |
2031 | } else | |
2032 | r = status_all(bus, STATUS_ALL); | |
52ad194e | 2033 | |
b012e921 MB |
2034 | return r; |
2035 | } | |
52ad194e | 2036 | |
a10f5d05 | 2037 | static int call_dns(sd_bus *bus, char **dns, const BusLocator *locator, sd_bus_error *error, bool extended) { |
b012e921 | 2038 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL; |
b012e921 | 2039 | char **p; |
6e866b33 | 2040 | int r; |
52ad194e | 2041 | |
a10f5d05 | 2042 | r = bus_message_new_method_call(bus, &req, locator, extended ? "SetLinkDNSEx" : "SetLinkDNS"); |
b012e921 MB |
2043 | if (r < 0) |
2044 | return bus_log_create_error(r); | |
52ad194e | 2045 | |
6e866b33 | 2046 | r = sd_bus_message_append(req, "i", arg_ifindex); |
b012e921 MB |
2047 | if (r < 0) |
2048 | return bus_log_create_error(r); | |
52ad194e | 2049 | |
a10f5d05 | 2050 | r = sd_bus_message_open_container(req, 'a', extended ? "(iayqs)" : "(iay)"); |
b012e921 MB |
2051 | if (r < 0) |
2052 | return bus_log_create_error(r); | |
52ad194e | 2053 | |
6e866b33 MB |
2054 | /* If only argument is the empty string, then call SetLinkDNS() with an |
2055 | * empty list, which will clear the list of domains for an interface. */ | |
f2dec872 BR |
2056 | if (!strv_equal(dns, STRV_MAKE(""))) |
2057 | STRV_FOREACH(p, dns) { | |
a10f5d05 | 2058 | _cleanup_free_ char *name = NULL; |
6e866b33 | 2059 | struct in_addr_data data; |
a10f5d05 MB |
2060 | uint16_t port; |
2061 | int ifindex; | |
52ad194e | 2062 | |
a10f5d05 | 2063 | r = in_addr_port_ifindex_name_from_string_auto(*p, &data.family, &data.address, &port, &ifindex, &name); |
6e866b33 MB |
2064 | if (r < 0) |
2065 | return log_error_errno(r, "Failed to parse DNS server address: %s", *p); | |
52ad194e | 2066 | |
a10f5d05 MB |
2067 | if (ifindex != 0 && ifindex != arg_ifindex) |
2068 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid ifindex: %i", ifindex); | |
2069 | ||
2070 | r = sd_bus_message_open_container(req, 'r', extended ? "iayqs" : "iay"); | |
6e866b33 MB |
2071 | if (r < 0) |
2072 | return bus_log_create_error(r); | |
b012e921 | 2073 | |
6e866b33 MB |
2074 | r = sd_bus_message_append(req, "i", data.family); |
2075 | if (r < 0) | |
2076 | return bus_log_create_error(r); | |
52ad194e | 2077 | |
6e866b33 MB |
2078 | r = sd_bus_message_append_array(req, 'y', &data.address, FAMILY_ADDRESS_SIZE(data.family)); |
2079 | if (r < 0) | |
2080 | return bus_log_create_error(r); | |
52ad194e | 2081 | |
a10f5d05 MB |
2082 | if (extended) { |
2083 | r = sd_bus_message_append(req, "q", port); | |
2084 | if (r < 0) | |
2085 | return bus_log_create_error(r); | |
2086 | ||
2087 | r = sd_bus_message_append(req, "s", name); | |
2088 | if (r < 0) | |
2089 | return bus_log_create_error(r); | |
2090 | } | |
2091 | ||
6e866b33 MB |
2092 | r = sd_bus_message_close_container(req); |
2093 | if (r < 0) | |
2094 | return bus_log_create_error(r); | |
2095 | } | |
52ad194e | 2096 | |
b012e921 MB |
2097 | r = sd_bus_message_close_container(req); |
2098 | if (r < 0) | |
2099 | return bus_log_create_error(r); | |
2100 | ||
a10f5d05 MB |
2101 | r = sd_bus_call(bus, req, 0, error, NULL); |
2102 | if (r < 0 && extended && sd_bus_error_has_name(error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
2103 | sd_bus_error_free(error); | |
2104 | return call_dns(bus, dns, locator, error, false); | |
2105 | } | |
2106 | return r; | |
b012e921 MB |
2107 | } |
2108 | ||
f2dec872 | 2109 | static int verb_dns(int argc, char **argv, void *userdata) { |
b012e921 | 2110 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b012e921 | 2111 | sd_bus *bus = userdata; |
6e866b33 | 2112 | int r; |
b012e921 MB |
2113 | |
2114 | assert(bus); | |
2115 | ||
6e866b33 MB |
2116 | if (argc >= 2) { |
2117 | r = ifname_mangle(argv[1]); | |
2118 | if (r < 0) | |
2119 | return r; | |
52ad194e MB |
2120 | } |
2121 | ||
6e866b33 | 2122 | if (arg_ifindex <= 0) |
f2dec872 | 2123 | return status_all(bus, STATUS_DNS); |
6e866b33 MB |
2124 | |
2125 | if (argc < 3) | |
f2dec872 BR |
2126 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DNS, NULL); |
2127 | ||
a10f5d05 | 2128 | r = call_dns(bus, argv + 2, bus_resolve_mgr, &error, true); |
f2dec872 BR |
2129 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2130 | sd_bus_error_free(&error); | |
2131 | ||
a10f5d05 | 2132 | r = call_dns(bus, argv + 2, bus_network_mgr, &error, true); |
f2dec872 BR |
2133 | } |
2134 | if (r < 0) { | |
2135 | if (arg_ifindex_permissive && | |
2136 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2137 | return 0; | |
2138 | ||
2139 | return log_error_errno(r, "Failed to set DNS configuration: %s", bus_error_message(&error, r)); | |
2140 | } | |
2141 | ||
2142 | return 0; | |
2143 | } | |
2144 | ||
a10f5d05 | 2145 | static int call_domain(sd_bus *bus, char **domain, const BusLocator *locator, sd_bus_error *error) { |
f2dec872 BR |
2146 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL; |
2147 | char **p; | |
2148 | int r; | |
b012e921 | 2149 | |
a10f5d05 | 2150 | r = bus_message_new_method_call(bus, &req, locator, "SetLinkDomains"); |
b012e921 MB |
2151 | if (r < 0) |
2152 | return bus_log_create_error(r); | |
2153 | ||
6e866b33 | 2154 | r = sd_bus_message_append(req, "i", arg_ifindex); |
b012e921 MB |
2155 | if (r < 0) |
2156 | return bus_log_create_error(r); | |
2157 | ||
2158 | r = sd_bus_message_open_container(req, 'a', "(sb)"); | |
2159 | if (r < 0) | |
2160 | return bus_log_create_error(r); | |
2161 | ||
6e866b33 MB |
2162 | /* If only argument is the empty string, then call SetLinkDomains() with an |
2163 | * empty list, which will clear the list of domains for an interface. */ | |
f2dec872 BR |
2164 | if (!strv_equal(domain, STRV_MAKE(""))) |
2165 | STRV_FOREACH(p, domain) { | |
6e866b33 | 2166 | const char *n; |
b012e921 | 2167 | |
6e866b33 | 2168 | n = **p == '~' ? *p + 1 : *p; |
b012e921 | 2169 | |
6e866b33 MB |
2170 | r = dns_name_is_valid(n); |
2171 | if (r < 0) | |
2172 | return log_error_errno(r, "Failed to validate specified domain %s: %m", n); | |
a032b68d MB |
2173 | if (r == 0) |
2174 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2175 | "Domain not valid: %s", | |
2176 | n); | |
b012e921 | 2177 | |
6e866b33 MB |
2178 | r = sd_bus_message_append(req, "(sb)", n, **p == '~'); |
2179 | if (r < 0) | |
2180 | return bus_log_create_error(r); | |
2181 | } | |
52ad194e | 2182 | |
b012e921 MB |
2183 | r = sd_bus_message_close_container(req); |
2184 | if (r < 0) | |
2185 | return bus_log_create_error(r); | |
52ad194e | 2186 | |
f2dec872 BR |
2187 | return sd_bus_call(bus, req, 0, error, NULL); |
2188 | } | |
2189 | ||
2190 | static int verb_domain(int argc, char **argv, void *userdata) { | |
2191 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2192 | sd_bus *bus = userdata; | |
2193 | int r; | |
2194 | ||
2195 | assert(bus); | |
2196 | ||
2197 | if (argc >= 2) { | |
2198 | r = ifname_mangle(argv[1]); | |
2199 | if (r < 0) | |
2200 | return r; | |
2201 | } | |
2202 | ||
2203 | if (arg_ifindex <= 0) | |
2204 | return status_all(bus, STATUS_DOMAIN); | |
2205 | ||
2206 | if (argc < 3) | |
2207 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DOMAIN, NULL); | |
2208 | ||
a10f5d05 | 2209 | r = call_domain(bus, argv + 2, bus_resolve_mgr, &error); |
f2dec872 BR |
2210 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2211 | sd_bus_error_free(&error); | |
52ad194e | 2212 | |
a10f5d05 | 2213 | r = call_domain(bus, argv + 2, bus_network_mgr, &error); |
f2dec872 BR |
2214 | } |
2215 | if (r < 0) { | |
b012e921 MB |
2216 | if (arg_ifindex_permissive && |
2217 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2218 | return 0; | |
52ad194e | 2219 | |
b012e921 MB |
2220 | return log_error_errno(r, "Failed to set domain configuration: %s", bus_error_message(&error, r)); |
2221 | } | |
2222 | ||
2223 | return 0; | |
2224 | } | |
52ad194e | 2225 | |
6e866b33 | 2226 | static int verb_default_route(int argc, char **argv, void *userdata) { |
b012e921 MB |
2227 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2228 | sd_bus *bus = userdata; | |
6e866b33 | 2229 | int r, b; |
52ad194e | 2230 | |
b012e921 MB |
2231 | assert(bus); |
2232 | ||
6e866b33 MB |
2233 | if (argc >= 2) { |
2234 | r = ifname_mangle(argv[1]); | |
2235 | if (r < 0) | |
2236 | return r; | |
2237 | } | |
b012e921 | 2238 | |
6e866b33 MB |
2239 | if (arg_ifindex <= 0) |
2240 | return status_all(bus, STATUS_DEFAULT_ROUTE); | |
b012e921 | 2241 | |
6e866b33 MB |
2242 | if (argc < 3) |
2243 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DEFAULT_ROUTE, NULL); | |
2244 | ||
2245 | b = parse_boolean(argv[2]); | |
2246 | if (b < 0) | |
2247 | return log_error_errno(b, "Failed to parse boolean argument: %s", argv[2]); | |
2248 | ||
a10f5d05 | 2249 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDefaultRoute", &error, NULL, "ib", arg_ifindex, b); |
f2dec872 BR |
2250 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2251 | sd_bus_error_free(&error); | |
2252 | ||
a10f5d05 | 2253 | r = bus_call_method(bus, bus_network_mgr, "SetLinkDefaultRoute", &error, NULL, "ib", arg_ifindex, b); |
f2dec872 | 2254 | } |
6e866b33 | 2255 | if (r < 0) { |
6e866b33 MB |
2256 | if (arg_ifindex_permissive && |
2257 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2258 | return 0; | |
2259 | ||
2260 | return log_error_errno(r, "Failed to set default route configuration: %s", bus_error_message(&error, r)); | |
b012e921 MB |
2261 | } |
2262 | ||
6e866b33 MB |
2263 | return 0; |
2264 | } | |
2265 | ||
2266 | static int verb_llmnr(int argc, char **argv, void *userdata) { | |
2267 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2268 | sd_bus *bus = userdata; | |
2269 | int r; | |
2270 | ||
2271 | assert(bus); | |
2272 | ||
2273 | if (argc >= 2) { | |
2274 | r = ifname_mangle(argv[1]); | |
2275 | if (r < 0) | |
2276 | return r; | |
2277 | } | |
2278 | ||
2279 | if (arg_ifindex <= 0) | |
2280 | return status_all(bus, STATUS_LLMNR); | |
2281 | ||
2282 | if (argc < 3) | |
2283 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_LLMNR, NULL); | |
b012e921 | 2284 | |
a10f5d05 | 2285 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkLLMNR", &error, NULL, "is", arg_ifindex, argv[2]); |
f2dec872 BR |
2286 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2287 | sd_bus_error_free(&error); | |
2288 | ||
a10f5d05 | 2289 | r = bus_call_method(bus, bus_network_mgr, "SetLinkLLMNR", &error, NULL, "is", arg_ifindex, argv[2]); |
f2dec872 | 2290 | } |
b012e921 | 2291 | if (r < 0) { |
b012e921 MB |
2292 | if (arg_ifindex_permissive && |
2293 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2294 | return 0; | |
2295 | ||
2296 | return log_error_errno(r, "Failed to set LLMNR configuration: %s", bus_error_message(&error, r)); | |
2297 | } | |
2298 | ||
2299 | return 0; | |
2300 | } | |
2301 | ||
2302 | static int verb_mdns(int argc, char **argv, void *userdata) { | |
2303 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2304 | sd_bus *bus = userdata; | |
6e866b33 | 2305 | int r; |
b012e921 MB |
2306 | |
2307 | assert(bus); | |
2308 | ||
6e866b33 MB |
2309 | if (argc >= 2) { |
2310 | r = ifname_mangle(argv[1]); | |
2311 | if (r < 0) | |
2312 | return r; | |
b012e921 MB |
2313 | } |
2314 | ||
6e866b33 MB |
2315 | if (arg_ifindex <= 0) |
2316 | return status_all(bus, STATUS_MDNS); | |
2317 | ||
2318 | if (argc < 3) | |
2319 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_MDNS, NULL); | |
b012e921 | 2320 | |
a10f5d05 | 2321 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkMulticastDNS", &error, NULL, "is", arg_ifindex, argv[2]); |
f2dec872 BR |
2322 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2323 | sd_bus_error_free(&error); | |
2324 | ||
a10f5d05 | 2325 | r = bus_call_method( |
f2dec872 | 2326 | bus, |
a10f5d05 | 2327 | bus_network_mgr, |
f2dec872 BR |
2328 | "SetLinkMulticastDNS", |
2329 | &error, | |
2330 | NULL, | |
2331 | "is", arg_ifindex, argv[2]); | |
2332 | } | |
b012e921 | 2333 | if (r < 0) { |
b012e921 MB |
2334 | if (arg_ifindex_permissive && |
2335 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2336 | return 0; | |
2337 | ||
2338 | return log_error_errno(r, "Failed to set MulticastDNS configuration: %s", bus_error_message(&error, r)); | |
2339 | } | |
2340 | ||
2341 | return 0; | |
2342 | } | |
2343 | ||
2344 | static int verb_dns_over_tls(int argc, char **argv, void *userdata) { | |
2345 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2346 | sd_bus *bus = userdata; | |
6e866b33 | 2347 | int r; |
b012e921 MB |
2348 | |
2349 | assert(bus); | |
2350 | ||
6e866b33 MB |
2351 | if (argc >= 2) { |
2352 | r = ifname_mangle(argv[1]); | |
2353 | if (r < 0) | |
2354 | return r; | |
b012e921 MB |
2355 | } |
2356 | ||
6e866b33 MB |
2357 | if (arg_ifindex <= 0) |
2358 | return status_all(bus, STATUS_PRIVATE); | |
2359 | ||
2360 | if (argc < 3) | |
2361 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_PRIVATE, NULL); | |
b012e921 | 2362 | |
a10f5d05 | 2363 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDNSOverTLS", &error, NULL, "is", arg_ifindex, argv[2]); |
f2dec872 BR |
2364 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2365 | sd_bus_error_free(&error); | |
2366 | ||
a10f5d05 | 2367 | r = bus_call_method( |
f2dec872 | 2368 | bus, |
a10f5d05 | 2369 | bus_network_mgr, |
f2dec872 BR |
2370 | "SetLinkDNSOverTLS", |
2371 | &error, | |
2372 | NULL, | |
2373 | "is", arg_ifindex, argv[2]); | |
2374 | } | |
b012e921 | 2375 | if (r < 0) { |
b012e921 MB |
2376 | if (arg_ifindex_permissive && |
2377 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2378 | return 0; | |
2379 | ||
2380 | return log_error_errno(r, "Failed to set DNSOverTLS configuration: %s", bus_error_message(&error, r)); | |
2381 | } | |
2382 | ||
2383 | return 0; | |
2384 | } | |
2385 | ||
2386 | static int verb_dnssec(int argc, char **argv, void *userdata) { | |
2387 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2388 | sd_bus *bus = userdata; | |
6e866b33 | 2389 | int r; |
b012e921 MB |
2390 | |
2391 | assert(bus); | |
2392 | ||
6e866b33 MB |
2393 | if (argc >= 2) { |
2394 | r = ifname_mangle(argv[1]); | |
2395 | if (r < 0) | |
2396 | return r; | |
b012e921 MB |
2397 | } |
2398 | ||
6e866b33 MB |
2399 | if (arg_ifindex <= 0) |
2400 | return status_all(bus, STATUS_DNSSEC); | |
2401 | ||
2402 | if (argc < 3) | |
2403 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DNSSEC, NULL); | |
b012e921 | 2404 | |
a10f5d05 | 2405 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDNSSEC", &error, NULL, "is", arg_ifindex, argv[2]); |
f2dec872 BR |
2406 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2407 | sd_bus_error_free(&error); | |
2408 | ||
a10f5d05 | 2409 | r = bus_call_method(bus, bus_network_mgr, "SetLinkDNSSEC", &error, NULL, "is", arg_ifindex, argv[2]); |
f2dec872 | 2410 | } |
b012e921 | 2411 | if (r < 0) { |
b012e921 MB |
2412 | if (arg_ifindex_permissive && |
2413 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2414 | return 0; | |
2415 | ||
2416 | return log_error_errno(r, "Failed to set DNSSEC configuration: %s", bus_error_message(&error, r)); | |
2417 | } | |
2418 | ||
2419 | return 0; | |
2420 | } | |
2421 | ||
a10f5d05 | 2422 | static int call_nta(sd_bus *bus, char **nta, const BusLocator *locator, sd_bus_error *error) { |
f2dec872 BR |
2423 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL; |
2424 | int r; | |
2425 | ||
a10f5d05 | 2426 | r = bus_message_new_method_call(bus, &req, locator, "SetLinkDNSSECNegativeTrustAnchors"); |
f2dec872 BR |
2427 | if (r < 0) |
2428 | return bus_log_create_error(r); | |
2429 | ||
2430 | r = sd_bus_message_append(req, "i", arg_ifindex); | |
2431 | if (r < 0) | |
2432 | return bus_log_create_error(r); | |
2433 | ||
2434 | r = sd_bus_message_append_strv(req, nta); | |
2435 | if (r < 0) | |
2436 | return bus_log_create_error(r); | |
2437 | ||
2438 | return sd_bus_call(bus, req, 0, error, NULL); | |
2439 | } | |
2440 | ||
b012e921 MB |
2441 | static int verb_nta(int argc, char **argv, void *userdata) { |
2442 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
b012e921 | 2443 | sd_bus *bus = userdata; |
6e866b33 MB |
2444 | char **p; |
2445 | int r; | |
2446 | bool clear; | |
b012e921 MB |
2447 | |
2448 | assert(bus); | |
2449 | ||
6e866b33 MB |
2450 | if (argc >= 2) { |
2451 | r = ifname_mangle(argv[1]); | |
2452 | if (r < 0) | |
2453 | return r; | |
2454 | } | |
2455 | ||
2456 | if (arg_ifindex <= 0) | |
b012e921 MB |
2457 | return status_all(bus, STATUS_NTA); |
2458 | ||
6e866b33 MB |
2459 | if (argc < 3) |
2460 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_NTA, NULL); | |
b012e921 | 2461 | |
6e866b33 MB |
2462 | /* If only argument is the empty string, then call SetLinkDNSSECNegativeTrustAnchors() |
2463 | * with an empty list, which will clear the list of domains for an interface. */ | |
2464 | clear = strv_equal(argv + 2, STRV_MAKE("")); | |
b012e921 | 2465 | |
6e866b33 MB |
2466 | if (!clear) |
2467 | STRV_FOREACH(p, argv + 2) { | |
2468 | r = dns_name_is_valid(*p); | |
2469 | if (r < 0) | |
2470 | return log_error_errno(r, "Failed to validate specified domain %s: %m", *p); | |
a032b68d MB |
2471 | if (r == 0) |
2472 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2473 | "Domain not valid: %s", | |
2474 | *p); | |
52ad194e | 2475 | } |
52ad194e | 2476 | |
a10f5d05 | 2477 | r = call_nta(bus, clear ? NULL : argv + 2, bus_resolve_mgr, &error); |
f2dec872 BR |
2478 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2479 | sd_bus_error_free(&error); | |
b012e921 | 2480 | |
a10f5d05 | 2481 | r = call_nta(bus, clear ? NULL : argv + 2, bus_network_mgr, &error); |
f2dec872 | 2482 | } |
b012e921 | 2483 | if (r < 0) { |
b012e921 MB |
2484 | if (arg_ifindex_permissive && |
2485 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2486 | return 0; | |
52ad194e | 2487 | |
b012e921 | 2488 | return log_error_errno(r, "Failed to set DNSSEC NTA configuration: %s", bus_error_message(&error, r)); |
52ad194e | 2489 | } |
b012e921 MB |
2490 | |
2491 | return 0; | |
52ad194e MB |
2492 | } |
2493 | ||
b012e921 | 2494 | static int verb_revert_link(int argc, char **argv, void *userdata) { |
52ad194e | 2495 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b012e921 | 2496 | sd_bus *bus = userdata; |
6e866b33 | 2497 | int r; |
52ad194e MB |
2498 | |
2499 | assert(bus); | |
2500 | ||
6e866b33 MB |
2501 | if (argc >= 2) { |
2502 | r = ifname_mangle(argv[1]); | |
2503 | if (r < 0) | |
2504 | return r; | |
b012e921 MB |
2505 | } |
2506 | ||
6e866b33 MB |
2507 | if (arg_ifindex <= 0) |
2508 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Interface argument required."); | |
2509 | ||
a10f5d05 | 2510 | r = bus_call_method(bus, bus_resolve_mgr, "RevertLink", &error, NULL, "i", arg_ifindex); |
f2dec872 BR |
2511 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2512 | sd_bus_error_free(&error); | |
2513 | ||
a10f5d05 | 2514 | r = bus_call_method(bus, bus_network_mgr, "RevertLinkDNS", &error, NULL, "i", arg_ifindex); |
f2dec872 | 2515 | } |
b012e921 MB |
2516 | if (r < 0) { |
2517 | if (arg_ifindex_permissive && | |
2518 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2519 | return 0; | |
2520 | ||
52ad194e | 2521 | return log_error_errno(r, "Failed to revert interface configuration: %s", bus_error_message(&error, r)); |
b012e921 | 2522 | } |
52ad194e MB |
2523 | |
2524 | return 0; | |
2525 | } | |
2526 | ||
a10f5d05 MB |
2527 | static int verb_log_level(int argc, char *argv[], void *userdata) { |
2528 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2529 | sd_bus *bus = userdata; | |
2530 | int r; | |
2531 | ||
2532 | assert(bus); | |
2533 | ||
2534 | if (argc == 1) { | |
2535 | _cleanup_free_ char *level = NULL; | |
2536 | ||
2537 | r = sd_bus_get_property_string( | |
2538 | bus, | |
2539 | "org.freedesktop.resolve1", | |
2540 | "/org/freedesktop/LogControl1", | |
2541 | "org.freedesktop.LogControl1", | |
2542 | "LogLevel", | |
2543 | &error, | |
2544 | &level); | |
2545 | if (r < 0) | |
2546 | return log_error_errno(r, "Failed to get log level: %s", bus_error_message(&error, r)); | |
2547 | ||
2548 | puts(level); | |
2549 | ||
2550 | } else { | |
2551 | assert(argc == 2); | |
2552 | ||
2553 | r = sd_bus_set_property( | |
2554 | bus, | |
2555 | "org.freedesktop.resolve1", | |
2556 | "/org/freedesktop/LogControl1", | |
2557 | "org.freedesktop.LogControl1", | |
2558 | "LogLevel", | |
2559 | &error, | |
2560 | "s", | |
2561 | argv[1]); | |
2562 | if (r < 0) | |
2563 | return log_error_errno(r, "Failed to set log level: %s", bus_error_message(&error, r)); | |
2564 | } | |
2565 | ||
2566 | return 0; | |
2567 | } | |
2568 | ||
4c89c718 MP |
2569 | static void help_protocol_types(void) { |
2570 | if (arg_legend) | |
2571 | puts("Known protocol types:"); | |
52ad194e | 2572 | puts("dns\nllmnr\nllmnr-ipv4\nllmnr-ipv6\nmdns\nmdns-ipv4\nmdns-ipv6"); |
4c89c718 MP |
2573 | } |
2574 | ||
2575 | static void help_dns_types(void) { | |
4c89c718 MP |
2576 | if (arg_legend) |
2577 | puts("Known DNS RR types:"); | |
b012e921 MB |
2578 | |
2579 | DUMP_STRING_TABLE(dns_type, int, _DNS_TYPE_MAX); | |
4c89c718 MP |
2580 | } |
2581 | ||
2582 | static void help_dns_classes(void) { | |
4c89c718 MP |
2583 | if (arg_legend) |
2584 | puts("Known DNS RR classes:"); | |
b012e921 MB |
2585 | |
2586 | DUMP_STRING_TABLE(dns_class, int, _DNS_CLASS_MAX); | |
4c89c718 MP |
2587 | } |
2588 | ||
6e866b33 MB |
2589 | static int compat_help(void) { |
2590 | _cleanup_free_ char *link = NULL; | |
2591 | int r; | |
2592 | ||
2593 | r = terminal_urlify_man("resolvectl", "1", &link); | |
2594 | if (r < 0) | |
2595 | return log_oom(); | |
2596 | ||
aa27b158 MP |
2597 | printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n" |
2598 | "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n" | |
2599 | "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n" | |
2600 | "%1$s [OPTIONS...] --statistics\n" | |
2601 | "%1$s [OPTIONS...] --reset-statistics\n" | |
2602 | "\n" | |
e1f67bc7 | 2603 | "%2$sResolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%3$s\n\n" |
aa27b158 MP |
2604 | " -h --help Show this help\n" |
2605 | " --version Show package version\n" | |
5a920b42 | 2606 | " --no-pager Do not pipe output into a pager\n" |
aa27b158 MP |
2607 | " -4 Resolve IPv4 addresses\n" |
2608 | " -6 Resolve IPv6 addresses\n" | |
2609 | " -i --interface=INTERFACE Look on interface\n" | |
2610 | " -p --protocol=PROTO|help Look via protocol\n" | |
2611 | " -t --type=TYPE|help Query RR with DNS type\n" | |
2612 | " -c --class=CLASS|help Query RR with DNS class\n" | |
2613 | " --service Resolve service (SRV)\n" | |
2614 | " --service-address=BOOL Resolve address for services (default: yes)\n" | |
2615 | " --service-txt=BOOL Resolve TXT records for services (default: yes)\n" | |
2616 | " --openpgp Query OpenPGP public key\n" | |
2617 | " --tlsa Query TLS public key\n" | |
2618 | " --cname=BOOL Follow CNAME redirects (default: yes)\n" | |
2619 | " --search=BOOL Use search domains for single-label names\n" | |
2620 | " (default: yes)\n" | |
2621 | " --raw[=payload|packet] Dump the answer as binary data\n" | |
2622 | " --legend=BOOL Print headers and additional info (default: yes)\n" | |
2623 | " --statistics Show resolver statistics\n" | |
2624 | " --reset-statistics Reset resolver statistics\n" | |
5a920b42 MP |
2625 | " --status Show link and server status\n" |
2626 | " --flush-caches Flush all local DNS caches\n" | |
f5e65279 MB |
2627 | " --reset-server-features\n" |
2628 | " Forget learnt DNS server feature levels\n" | |
52ad194e MB |
2629 | " --set-dns=SERVER Set per-interface DNS server address\n" |
2630 | " --set-domain=DOMAIN Set per-interface search domain\n" | |
2631 | " --set-llmnr=MODE Set per-interface LLMNR mode\n" | |
2632 | " --set-mdns=MODE Set per-interface MulticastDNS mode\n" | |
b012e921 | 2633 | " --set-dnsovertls=MODE Set per-interface DNS-over-TLS mode\n" |
52ad194e MB |
2634 | " --set-dnssec=MODE Set per-interface DNSSEC mode\n" |
2635 | " --set-nta=DOMAIN Set per-interface DNSSEC NTA\n" | |
2636 | " --revert Revert per-interface configuration\n" | |
3a6ce677 BR |
2637 | "\nSee the %4$s for details.\n", |
2638 | program_invocation_short_name, | |
2639 | ansi_highlight(), | |
2640 | ansi_normal(), | |
2641 | link); | |
6e866b33 MB |
2642 | |
2643 | return 0; | |
4c89c718 MP |
2644 | } |
2645 | ||
6e866b33 MB |
2646 | static int native_help(void) { |
2647 | _cleanup_free_ char *link = NULL; | |
2648 | int r; | |
2649 | ||
2650 | r = terminal_urlify_man("resolvectl", "1", &link); | |
2651 | if (r < 0) | |
2652 | return log_oom(); | |
2653 | ||
e1f67bc7 | 2654 | printf("%s [OPTIONS...] COMMAND ...\n" |
b012e921 | 2655 | "\n" |
e1f67bc7 MB |
2656 | "%sSend control commands to the network name resolution manager, or%s\n" |
2657 | "%sresolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%s\n" | |
2658 | "\nCommands:\n" | |
b012e921 MB |
2659 | " query HOSTNAME|ADDRESS... Resolve domain names, IPv4 and IPv6 addresses\n" |
2660 | " service [[NAME] TYPE] DOMAIN Resolve service (SRV)\n" | |
2661 | " openpgp EMAIL@DOMAIN... Query OpenPGP public key\n" | |
2662 | " tlsa DOMAIN[:PORT]... Query TLS public key\n" | |
2663 | " status [LINK...] Show link and server status\n" | |
2664 | " statistics Show resolver statistics\n" | |
2665 | " reset-statistics Reset resolver statistics\n" | |
2666 | " flush-caches Flush all local DNS caches\n" | |
2667 | " reset-server-features Forget learnt DNS server feature levels\n" | |
2668 | " dns [LINK [SERVER...]] Get/set per-interface DNS server address\n" | |
2669 | " domain [LINK [DOMAIN...]] Get/set per-interface search domain\n" | |
6e866b33 | 2670 | " default-route [LINK [BOOL]] Get/set per-interface default route flag\n" |
b012e921 MB |
2671 | " llmnr [LINK [MODE]] Get/set per-interface LLMNR mode\n" |
2672 | " mdns [LINK [MODE]] Get/set per-interface MulticastDNS mode\n" | |
2673 | " dnsovertls [LINK [MODE]] Get/set per-interface DNS-over-TLS mode\n" | |
2674 | " dnssec [LINK [MODE]] Get/set per-interface DNSSEC mode\n" | |
2675 | " nta [LINK [DOMAIN...]] Get/set per-interface DNSSEC NTA\n" | |
2676 | " revert LINK Revert per-interface configuration\n" | |
7de6915e | 2677 | " log-level [LEVEL] Get/set logging threshold for systemd-resolved\n" |
e1f67bc7 MB |
2678 | "\nOptions:\n" |
2679 | " -h --help Show this help\n" | |
2680 | " --version Show package version\n" | |
2681 | " --no-pager Do not pipe output into a pager\n" | |
2682 | " -4 Resolve IPv4 addresses\n" | |
2683 | " -6 Resolve IPv6 addresses\n" | |
2684 | " -i --interface=INTERFACE Look on interface\n" | |
2685 | " -p --protocol=PROTO|help Look via protocol\n" | |
2686 | " -t --type=TYPE|help Query RR with DNS type\n" | |
2687 | " -c --class=CLASS|help Query RR with DNS class\n" | |
2688 | " --service-address=BOOL Resolve address for services (default: yes)\n" | |
2689 | " --service-txt=BOOL Resolve TXT records for services (default: yes)\n" | |
2690 | " --cname=BOOL Follow CNAME redirects (default: yes)\n" | |
3a6ce677 BR |
2691 | " --validate=BOOL Allow DNSSEC validation (default: yes)\n" |
2692 | " --synthesize=BOOL Allow synthetic response (default: yes)\n" | |
2693 | " --cache=BOOL Allow response from cache (default: yes)\n" | |
2694 | " --zone=BOOL Allow response from locally registered mDNS/LLMNR\n" | |
2695 | " records (default: yes)\n" | |
2696 | " --trust-anchor=BOOL Allow response from local trust anchor (default: yes)\n" | |
2697 | " --network=BOOL Allow response from network (default: yes)\n" | |
2698 | " --search=BOOL Use search domains for single-label names (default: yes)\n" | |
e1f67bc7 MB |
2699 | " --raw[=payload|packet] Dump the answer as binary data\n" |
2700 | " --legend=BOOL Print headers and additional info (default: yes)\n" | |
3a6ce677 BR |
2701 | "\nSee the %s for details.\n", |
2702 | program_invocation_short_name, | |
2703 | ansi_highlight(), | |
2704 | ansi_normal(), | |
2705 | ansi_highlight(), | |
2706 | ansi_normal(), | |
2707 | link); | |
6e866b33 MB |
2708 | |
2709 | return 0; | |
b012e921 MB |
2710 | } |
2711 | ||
2712 | static int verb_help(int argc, char **argv, void *userdata) { | |
6e866b33 | 2713 | return native_help(); |
b012e921 MB |
2714 | } |
2715 | ||
2716 | static int compat_parse_argv(int argc, char *argv[]) { | |
4c89c718 MP |
2717 | enum { |
2718 | ARG_VERSION = 0x100, | |
2719 | ARG_LEGEND, | |
2720 | ARG_SERVICE, | |
2721 | ARG_CNAME, | |
2722 | ARG_SERVICE_ADDRESS, | |
2723 | ARG_SERVICE_TXT, | |
aa27b158 MP |
2724 | ARG_OPENPGP, |
2725 | ARG_TLSA, | |
2726 | ARG_RAW, | |
4c89c718 MP |
2727 | ARG_SEARCH, |
2728 | ARG_STATISTICS, | |
2729 | ARG_RESET_STATISTICS, | |
5a920b42 MP |
2730 | ARG_STATUS, |
2731 | ARG_FLUSH_CACHES, | |
f5e65279 | 2732 | ARG_RESET_SERVER_FEATURES, |
5a920b42 | 2733 | ARG_NO_PAGER, |
52ad194e MB |
2734 | ARG_SET_DNS, |
2735 | ARG_SET_DOMAIN, | |
2736 | ARG_SET_LLMNR, | |
2737 | ARG_SET_MDNS, | |
b012e921 | 2738 | ARG_SET_PRIVATE, |
52ad194e MB |
2739 | ARG_SET_DNSSEC, |
2740 | ARG_SET_NTA, | |
2741 | ARG_REVERT_LINK, | |
4c89c718 MP |
2742 | }; |
2743 | ||
2744 | static const struct option options[] = { | |
f5e65279 MB |
2745 | { "help", no_argument, NULL, 'h' }, |
2746 | { "version", no_argument, NULL, ARG_VERSION }, | |
2747 | { "type", required_argument, NULL, 't' }, | |
2748 | { "class", required_argument, NULL, 'c' }, | |
2749 | { "legend", required_argument, NULL, ARG_LEGEND }, | |
2750 | { "interface", required_argument, NULL, 'i' }, | |
2751 | { "protocol", required_argument, NULL, 'p' }, | |
2752 | { "cname", required_argument, NULL, ARG_CNAME }, | |
2753 | { "service", no_argument, NULL, ARG_SERVICE }, | |
2754 | { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS }, | |
2755 | { "service-txt", required_argument, NULL, ARG_SERVICE_TXT }, | |
2756 | { "openpgp", no_argument, NULL, ARG_OPENPGP }, | |
2757 | { "tlsa", optional_argument, NULL, ARG_TLSA }, | |
2758 | { "raw", optional_argument, NULL, ARG_RAW }, | |
2759 | { "search", required_argument, NULL, ARG_SEARCH }, | |
2760 | { "statistics", no_argument, NULL, ARG_STATISTICS, }, | |
2761 | { "reset-statistics", no_argument, NULL, ARG_RESET_STATISTICS }, | |
2762 | { "status", no_argument, NULL, ARG_STATUS }, | |
2763 | { "flush-caches", no_argument, NULL, ARG_FLUSH_CACHES }, | |
2764 | { "reset-server-features", no_argument, NULL, ARG_RESET_SERVER_FEATURES }, | |
2765 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
52ad194e MB |
2766 | { "set-dns", required_argument, NULL, ARG_SET_DNS }, |
2767 | { "set-domain", required_argument, NULL, ARG_SET_DOMAIN }, | |
2768 | { "set-llmnr", required_argument, NULL, ARG_SET_LLMNR }, | |
2769 | { "set-mdns", required_argument, NULL, ARG_SET_MDNS }, | |
b012e921 | 2770 | { "set-dnsovertls", required_argument, NULL, ARG_SET_PRIVATE }, |
52ad194e MB |
2771 | { "set-dnssec", required_argument, NULL, ARG_SET_DNSSEC }, |
2772 | { "set-nta", required_argument, NULL, ARG_SET_NTA }, | |
2773 | { "revert", no_argument, NULL, ARG_REVERT_LINK }, | |
4c89c718 MP |
2774 | {} |
2775 | }; | |
2776 | ||
2777 | int c, r; | |
2778 | ||
2779 | assert(argc >= 0); | |
2780 | assert(argv); | |
2781 | ||
2782 | while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0) | |
2783 | switch(c) { | |
2784 | ||
2785 | case 'h': | |
6e866b33 | 2786 | return compat_help(); |
4c89c718 MP |
2787 | |
2788 | case ARG_VERSION: | |
2789 | return version(); | |
2790 | ||
2791 | case '4': | |
2792 | arg_family = AF_INET; | |
2793 | break; | |
2794 | ||
2795 | case '6': | |
2796 | arg_family = AF_INET6; | |
2797 | break; | |
2798 | ||
98393f85 | 2799 | case 'i': |
6e866b33 | 2800 | r = ifname_mangle(optarg); |
b012e921 MB |
2801 | if (r < 0) |
2802 | return r; | |
4c89c718 | 2803 | break; |
4c89c718 MP |
2804 | |
2805 | case 't': | |
2806 | if (streq(optarg, "help")) { | |
2807 | help_dns_types(); | |
2808 | return 0; | |
2809 | } | |
2810 | ||
2811 | r = dns_type_from_string(optarg); | |
3a6ce677 BR |
2812 | if (r < 0) |
2813 | return log_error_errno(r, "Failed to parse RR record type %s: %m", optarg); | |
2814 | ||
4c89c718 MP |
2815 | arg_type = (uint16_t) r; |
2816 | assert((int) arg_type == r); | |
2817 | ||
2818 | arg_mode = MODE_RESOLVE_RECORD; | |
2819 | break; | |
2820 | ||
2821 | case 'c': | |
2822 | if (streq(optarg, "help")) { | |
2823 | help_dns_classes(); | |
2824 | return 0; | |
2825 | } | |
2826 | ||
2827 | r = dns_class_from_string(optarg); | |
3a6ce677 BR |
2828 | if (r < 0) |
2829 | return log_error_errno(r, "Failed to parse RR record class %s: %m", optarg); | |
2830 | ||
4c89c718 MP |
2831 | arg_class = (uint16_t) r; |
2832 | assert((int) arg_class == r); | |
2833 | ||
2834 | break; | |
2835 | ||
2836 | case ARG_LEGEND: | |
3a6ce677 | 2837 | r = parse_boolean_argument("--legend=", optarg, &arg_legend); |
4c89c718 | 2838 | if (r < 0) |
3a6ce677 | 2839 | return r; |
4c89c718 MP |
2840 | break; |
2841 | ||
2842 | case 'p': | |
2843 | if (streq(optarg, "help")) { | |
2844 | help_protocol_types(); | |
2845 | return 0; | |
2846 | } else if (streq(optarg, "dns")) | |
2847 | arg_flags |= SD_RESOLVED_DNS; | |
2848 | else if (streq(optarg, "llmnr")) | |
2849 | arg_flags |= SD_RESOLVED_LLMNR; | |
2850 | else if (streq(optarg, "llmnr-ipv4")) | |
2851 | arg_flags |= SD_RESOLVED_LLMNR_IPV4; | |
2852 | else if (streq(optarg, "llmnr-ipv6")) | |
2853 | arg_flags |= SD_RESOLVED_LLMNR_IPV6; | |
2897b343 MP |
2854 | else if (streq(optarg, "mdns")) |
2855 | arg_flags |= SD_RESOLVED_MDNS; | |
2856 | else if (streq(optarg, "mdns-ipv4")) | |
2857 | arg_flags |= SD_RESOLVED_MDNS_IPV4; | |
2858 | else if (streq(optarg, "mdns-ipv6")) | |
2859 | arg_flags |= SD_RESOLVED_MDNS_IPV6; | |
6e866b33 MB |
2860 | else |
2861 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2862 | "Unknown protocol specifier: %s", optarg); | |
4c89c718 MP |
2863 | |
2864 | break; | |
2865 | ||
2866 | case ARG_SERVICE: | |
2867 | arg_mode = MODE_RESOLVE_SERVICE; | |
2868 | break; | |
2869 | ||
aa27b158 MP |
2870 | case ARG_OPENPGP: |
2871 | arg_mode = MODE_RESOLVE_OPENPGP; | |
2872 | break; | |
2873 | ||
2874 | case ARG_TLSA: | |
2875 | arg_mode = MODE_RESOLVE_TLSA; | |
b012e921 MB |
2876 | if (!optarg || service_family_is_valid(optarg)) |
2877 | arg_service_family = optarg; | |
6e866b33 MB |
2878 | else |
2879 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2880 | "Unknown service family \"%s\".", optarg); | |
aa27b158 MP |
2881 | break; |
2882 | ||
2883 | case ARG_RAW: | |
6e866b33 MB |
2884 | if (on_tty()) |
2885 | return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), | |
2886 | "Refusing to write binary data to tty."); | |
aa27b158 MP |
2887 | |
2888 | if (optarg == NULL || streq(optarg, "payload")) | |
2889 | arg_raw = RAW_PAYLOAD; | |
2890 | else if (streq(optarg, "packet")) | |
2891 | arg_raw = RAW_PACKET; | |
6e866b33 MB |
2892 | else |
2893 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2894 | "Unknown --raw specifier \"%s\".", | |
2895 | optarg); | |
aa27b158 MP |
2896 | |
2897 | arg_legend = false; | |
2898 | break; | |
2899 | ||
4c89c718 | 2900 | case ARG_CNAME: |
3a6ce677 | 2901 | r = parse_boolean_argument("--cname=", optarg, NULL); |
4c89c718 | 2902 | if (r < 0) |
3a6ce677 | 2903 | return r; |
aa27b158 | 2904 | SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0); |
4c89c718 MP |
2905 | break; |
2906 | ||
2907 | case ARG_SERVICE_ADDRESS: | |
3a6ce677 | 2908 | r = parse_boolean_argument("--service-address=", optarg, NULL); |
4c89c718 | 2909 | if (r < 0) |
3a6ce677 | 2910 | return r; |
aa27b158 | 2911 | SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0); |
4c89c718 MP |
2912 | break; |
2913 | ||
2914 | case ARG_SERVICE_TXT: | |
3a6ce677 | 2915 | r = parse_boolean_argument("--service-txt=", optarg, NULL); |
4c89c718 | 2916 | if (r < 0) |
3a6ce677 | 2917 | return r; |
aa27b158 | 2918 | SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0); |
4c89c718 MP |
2919 | break; |
2920 | ||
2921 | case ARG_SEARCH: | |
3a6ce677 | 2922 | r = parse_boolean_argument("--search=", optarg, NULL); |
4c89c718 | 2923 | if (r < 0) |
3a6ce677 | 2924 | return r; |
aa27b158 | 2925 | SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0); |
4c89c718 MP |
2926 | break; |
2927 | ||
2928 | case ARG_STATISTICS: | |
2929 | arg_mode = MODE_STATISTICS; | |
2930 | break; | |
2931 | ||
2932 | case ARG_RESET_STATISTICS: | |
2933 | arg_mode = MODE_RESET_STATISTICS; | |
2934 | break; | |
2935 | ||
5a920b42 MP |
2936 | case ARG_FLUSH_CACHES: |
2937 | arg_mode = MODE_FLUSH_CACHES; | |
2938 | break; | |
2939 | ||
f5e65279 MB |
2940 | case ARG_RESET_SERVER_FEATURES: |
2941 | arg_mode = MODE_RESET_SERVER_FEATURES; | |
2942 | break; | |
2943 | ||
5a920b42 MP |
2944 | case ARG_STATUS: |
2945 | arg_mode = MODE_STATUS; | |
2946 | break; | |
2947 | ||
2948 | case ARG_NO_PAGER: | |
6e866b33 | 2949 | arg_pager_flags |= PAGER_DISABLE; |
5a920b42 MP |
2950 | break; |
2951 | ||
b012e921 MB |
2952 | case ARG_SET_DNS: |
2953 | r = strv_extend(&arg_set_dns, optarg); | |
52ad194e | 2954 | if (r < 0) |
52ad194e | 2955 | return log_oom(); |
52ad194e | 2956 | |
52ad194e MB |
2957 | arg_mode = MODE_SET_LINK; |
2958 | break; | |
52ad194e | 2959 | |
b012e921 | 2960 | case ARG_SET_DOMAIN: |
52ad194e MB |
2961 | r = strv_extend(&arg_set_domain, optarg); |
2962 | if (r < 0) | |
2963 | return log_oom(); | |
2964 | ||
2965 | arg_mode = MODE_SET_LINK; | |
2966 | break; | |
52ad194e MB |
2967 | |
2968 | case ARG_SET_LLMNR: | |
b012e921 | 2969 | arg_set_llmnr = optarg; |
52ad194e MB |
2970 | arg_mode = MODE_SET_LINK; |
2971 | break; | |
2972 | ||
2973 | case ARG_SET_MDNS: | |
b012e921 MB |
2974 | arg_set_mdns = optarg; |
2975 | arg_mode = MODE_SET_LINK; | |
2976 | break; | |
52ad194e | 2977 | |
b012e921 MB |
2978 | case ARG_SET_PRIVATE: |
2979 | arg_set_dns_over_tls = optarg; | |
52ad194e MB |
2980 | arg_mode = MODE_SET_LINK; |
2981 | break; | |
2982 | ||
2983 | case ARG_SET_DNSSEC: | |
b012e921 | 2984 | arg_set_dnssec = optarg; |
52ad194e MB |
2985 | arg_mode = MODE_SET_LINK; |
2986 | break; | |
2987 | ||
2988 | case ARG_SET_NTA: | |
52ad194e MB |
2989 | r = strv_extend(&arg_set_nta, optarg); |
2990 | if (r < 0) | |
2991 | return log_oom(); | |
2992 | ||
2993 | arg_mode = MODE_SET_LINK; | |
2994 | break; | |
2995 | ||
2996 | case ARG_REVERT_LINK: | |
2997 | arg_mode = MODE_REVERT_LINK; | |
2998 | break; | |
2999 | ||
4c89c718 MP |
3000 | case '?': |
3001 | return -EINVAL; | |
3002 | ||
3003 | default: | |
3004 | assert_not_reached("Unhandled option"); | |
3005 | } | |
3006 | ||
6e866b33 MB |
3007 | if (arg_type == 0 && arg_class != 0) |
3008 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3009 | "--class= may only be used in conjunction with --type=."); | |
4c89c718 | 3010 | |
6e866b33 MB |
3011 | if (arg_type != 0 && arg_mode == MODE_RESOLVE_SERVICE) |
3012 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3013 | "--service and --type= may not be combined."); | |
4c89c718 MP |
3014 | |
3015 | if (arg_type != 0 && arg_class == 0) | |
3016 | arg_class = DNS_CLASS_IN; | |
3017 | ||
3018 | if (arg_class != 0 && arg_type == 0) | |
3019 | arg_type = DNS_TYPE_A; | |
3020 | ||
52ad194e MB |
3021 | if (IN_SET(arg_mode, MODE_SET_LINK, MODE_REVERT_LINK)) { |
3022 | ||
6e866b33 MB |
3023 | if (arg_ifindex <= 0) |
3024 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3025 | "--set-dns=, --set-domain=, --set-llmnr=, --set-mdns=, --set-dnsovertls=, --set-dnssec=, --set-nta= and --revert require --interface=."); | |
52ad194e MB |
3026 | } |
3027 | ||
4c89c718 MP |
3028 | return 1 /* work to do */; |
3029 | } | |
3030 | ||
b012e921 MB |
3031 | static int native_parse_argv(int argc, char *argv[]) { |
3032 | enum { | |
3033 | ARG_VERSION = 0x100, | |
3034 | ARG_LEGEND, | |
3035 | ARG_CNAME, | |
3a6ce677 BR |
3036 | ARG_VALIDATE, |
3037 | ARG_SYNTHESIZE, | |
3038 | ARG_CACHE, | |
3039 | ARG_ZONE, | |
3040 | ARG_TRUST_ANCHOR, | |
3041 | ARG_NETWORK, | |
b012e921 MB |
3042 | ARG_SERVICE_ADDRESS, |
3043 | ARG_SERVICE_TXT, | |
3044 | ARG_RAW, | |
3045 | ARG_SEARCH, | |
3046 | ARG_NO_PAGER, | |
3047 | }; | |
4c89c718 | 3048 | |
b012e921 MB |
3049 | static const struct option options[] = { |
3050 | { "help", no_argument, NULL, 'h' }, | |
3051 | { "version", no_argument, NULL, ARG_VERSION }, | |
3052 | { "type", required_argument, NULL, 't' }, | |
3053 | { "class", required_argument, NULL, 'c' }, | |
3054 | { "legend", required_argument, NULL, ARG_LEGEND }, | |
3055 | { "interface", required_argument, NULL, 'i' }, | |
3056 | { "protocol", required_argument, NULL, 'p' }, | |
3057 | { "cname", required_argument, NULL, ARG_CNAME }, | |
3a6ce677 BR |
3058 | { "validate", required_argument, NULL, ARG_VALIDATE }, |
3059 | { "synthesize", required_argument, NULL, ARG_SYNTHESIZE }, | |
3060 | { "cache", required_argument, NULL, ARG_CACHE }, | |
3061 | { "zone", required_argument, NULL, ARG_ZONE }, | |
3062 | { "trust-anchor", required_argument, NULL, ARG_TRUST_ANCHOR }, | |
3063 | { "network", required_argument, NULL, ARG_NETWORK }, | |
b012e921 MB |
3064 | { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS }, |
3065 | { "service-txt", required_argument, NULL, ARG_SERVICE_TXT }, | |
3066 | { "raw", optional_argument, NULL, ARG_RAW }, | |
3067 | { "search", required_argument, NULL, ARG_SEARCH }, | |
3068 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
3069 | {} | |
3070 | }; | |
4c89c718 | 3071 | |
b012e921 | 3072 | int c, r; |
4c89c718 | 3073 | |
b012e921 MB |
3074 | assert(argc >= 0); |
3075 | assert(argv); | |
4c89c718 | 3076 | |
b012e921 MB |
3077 | while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0) |
3078 | switch(c) { | |
4c89c718 | 3079 | |
b012e921 | 3080 | case 'h': |
6e866b33 | 3081 | return native_help(); |
b012e921 MB |
3082 | |
3083 | case ARG_VERSION: | |
3084 | return version(); | |
4c89c718 | 3085 | |
b012e921 MB |
3086 | case '4': |
3087 | arg_family = AF_INET; | |
3088 | break; | |
3089 | ||
3090 | case '6': | |
3091 | arg_family = AF_INET6; | |
3092 | break; | |
3093 | ||
3094 | case 'i': | |
6e866b33 | 3095 | r = ifname_mangle(optarg); |
b012e921 MB |
3096 | if (r < 0) |
3097 | return r; | |
b012e921 MB |
3098 | break; |
3099 | ||
3100 | case 't': | |
3101 | if (streq(optarg, "help")) { | |
3102 | help_dns_types(); | |
3103 | return 0; | |
3104 | } | |
3105 | ||
3106 | r = dns_type_from_string(optarg); | |
3a6ce677 BR |
3107 | if (r < 0) |
3108 | return log_error_errno(r, "Failed to parse RR record type %s: %m", optarg); | |
3109 | ||
b012e921 MB |
3110 | arg_type = (uint16_t) r; |
3111 | assert((int) arg_type == r); | |
3112 | ||
3113 | break; | |
3114 | ||
3115 | case 'c': | |
3116 | if (streq(optarg, "help")) { | |
3117 | help_dns_classes(); | |
3118 | return 0; | |
3119 | } | |
3120 | ||
3121 | r = dns_class_from_string(optarg); | |
3a6ce677 BR |
3122 | if (r < 0) |
3123 | return log_error_errno(r, "Failed to parse RR record class %s: %m", optarg); | |
3124 | ||
b012e921 MB |
3125 | arg_class = (uint16_t) r; |
3126 | assert((int) arg_class == r); | |
3127 | ||
3128 | break; | |
4c89c718 | 3129 | |
b012e921 | 3130 | case ARG_LEGEND: |
3a6ce677 | 3131 | r = parse_boolean_argument("--legend=", optarg, &arg_legend); |
b012e921 | 3132 | if (r < 0) |
3a6ce677 | 3133 | return r; |
b012e921 MB |
3134 | break; |
3135 | ||
3136 | case 'p': | |
3137 | if (streq(optarg, "help")) { | |
3138 | help_protocol_types(); | |
3139 | return 0; | |
3140 | } else if (streq(optarg, "dns")) | |
3141 | arg_flags |= SD_RESOLVED_DNS; | |
3142 | else if (streq(optarg, "llmnr")) | |
3143 | arg_flags |= SD_RESOLVED_LLMNR; | |
3144 | else if (streq(optarg, "llmnr-ipv4")) | |
3145 | arg_flags |= SD_RESOLVED_LLMNR_IPV4; | |
3146 | else if (streq(optarg, "llmnr-ipv6")) | |
3147 | arg_flags |= SD_RESOLVED_LLMNR_IPV6; | |
3148 | else if (streq(optarg, "mdns")) | |
3149 | arg_flags |= SD_RESOLVED_MDNS; | |
3150 | else if (streq(optarg, "mdns-ipv4")) | |
3151 | arg_flags |= SD_RESOLVED_MDNS_IPV4; | |
3152 | else if (streq(optarg, "mdns-ipv6")) | |
3153 | arg_flags |= SD_RESOLVED_MDNS_IPV6; | |
6e866b33 MB |
3154 | else |
3155 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3156 | "Unknown protocol specifier: %s", | |
3157 | optarg); | |
4c89c718 | 3158 | |
b012e921 | 3159 | break; |
4c89c718 | 3160 | |
b012e921 | 3161 | case ARG_RAW: |
6e866b33 MB |
3162 | if (on_tty()) |
3163 | return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), | |
3164 | "Refusing to write binary data to tty."); | |
4c89c718 | 3165 | |
b012e921 MB |
3166 | if (optarg == NULL || streq(optarg, "payload")) |
3167 | arg_raw = RAW_PAYLOAD; | |
3168 | else if (streq(optarg, "packet")) | |
3169 | arg_raw = RAW_PACKET; | |
6e866b33 MB |
3170 | else |
3171 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3172 | "Unknown --raw specifier \"%s\".", | |
3173 | optarg); | |
4c89c718 | 3174 | |
b012e921 MB |
3175 | arg_legend = false; |
3176 | break; | |
4c89c718 | 3177 | |
b012e921 | 3178 | case ARG_CNAME: |
3a6ce677 | 3179 | r = parse_boolean_argument("--cname=", optarg, NULL); |
b012e921 | 3180 | if (r < 0) |
3a6ce677 | 3181 | return r; |
b012e921 MB |
3182 | SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0); |
3183 | break; | |
4c89c718 | 3184 | |
3a6ce677 BR |
3185 | case ARG_VALIDATE: |
3186 | r = parse_boolean_argument("--validate=", optarg, NULL); | |
3187 | if (r < 0) | |
3188 | return r; | |
3189 | SET_FLAG(arg_flags, SD_RESOLVED_NO_VALIDATE, r == 0); | |
3190 | break; | |
3191 | ||
3192 | case ARG_SYNTHESIZE: | |
3193 | r = parse_boolean_argument("--synthesize=", optarg, NULL); | |
3194 | if (r < 0) | |
3195 | return r; | |
3196 | SET_FLAG(arg_flags, SD_RESOLVED_NO_SYNTHESIZE, r == 0); | |
3197 | break; | |
3198 | ||
3199 | case ARG_CACHE: | |
3200 | r = parse_boolean_argument("--cache=", optarg, NULL); | |
3201 | if (r < 0) | |
3202 | return r; | |
3203 | SET_FLAG(arg_flags, SD_RESOLVED_NO_CACHE, r == 0); | |
3204 | break; | |
3205 | ||
3206 | case ARG_ZONE: | |
3207 | r = parse_boolean_argument("--zone=", optarg, NULL); | |
3208 | if (r < 0) | |
3209 | return r; | |
3210 | SET_FLAG(arg_flags, SD_RESOLVED_NO_ZONE, r == 0); | |
3211 | break; | |
3212 | ||
3213 | case ARG_TRUST_ANCHOR: | |
3214 | r = parse_boolean_argument("--trust-anchor=", optarg, NULL); | |
3215 | if (r < 0) | |
3216 | return r; | |
3217 | SET_FLAG(arg_flags, SD_RESOLVED_NO_TRUST_ANCHOR, r == 0); | |
3218 | break; | |
3219 | ||
3220 | case ARG_NETWORK: | |
3221 | r = parse_boolean_argument("--network=", optarg, NULL); | |
3222 | if (r < 0) | |
3223 | return r; | |
3224 | SET_FLAG(arg_flags, SD_RESOLVED_NO_NETWORK, r == 0); | |
3225 | break; | |
3226 | ||
b012e921 | 3227 | case ARG_SERVICE_ADDRESS: |
3a6ce677 | 3228 | r = parse_boolean_argument("--service-address=", optarg, NULL); |
b012e921 | 3229 | if (r < 0) |
3a6ce677 | 3230 | return r; |
b012e921 MB |
3231 | SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0); |
3232 | break; | |
4c89c718 | 3233 | |
b012e921 | 3234 | case ARG_SERVICE_TXT: |
3a6ce677 | 3235 | r = parse_boolean_argument("--service-txt=", optarg, NULL); |
b012e921 | 3236 | if (r < 0) |
3a6ce677 | 3237 | return r; |
b012e921 MB |
3238 | SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0); |
3239 | break; | |
4c89c718 | 3240 | |
b012e921 | 3241 | case ARG_SEARCH: |
3a6ce677 | 3242 | r = parse_boolean_argument("--search=", optarg, NULL); |
b012e921 | 3243 | if (r < 0) |
3a6ce677 | 3244 | return r; |
b012e921 MB |
3245 | SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0); |
3246 | break; | |
4c89c718 | 3247 | |
b012e921 | 3248 | case ARG_NO_PAGER: |
6e866b33 | 3249 | arg_pager_flags |= PAGER_DISABLE; |
b012e921 | 3250 | break; |
aa27b158 | 3251 | |
b012e921 MB |
3252 | case '?': |
3253 | return -EINVAL; | |
3254 | ||
3255 | default: | |
3256 | assert_not_reached("Unhandled option"); | |
aa27b158 MP |
3257 | } |
3258 | ||
6e866b33 MB |
3259 | if (arg_type == 0 && arg_class != 0) |
3260 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3261 | "--class= may only be used in conjunction with --type=."); | |
aa27b158 | 3262 | |
b012e921 MB |
3263 | if (arg_type != 0 && arg_class == 0) |
3264 | arg_class = DNS_CLASS_IN; | |
aa27b158 | 3265 | |
b012e921 MB |
3266 | if (arg_class != 0 && arg_type == 0) |
3267 | arg_type = DNS_TYPE_A; | |
aa27b158 | 3268 | |
b012e921 MB |
3269 | return 1 /* work to do */; |
3270 | } | |
aa27b158 | 3271 | |
b012e921 MB |
3272 | static int native_main(int argc, char *argv[], sd_bus *bus) { |
3273 | ||
3274 | static const Verb verbs[] = { | |
3275 | { "help", VERB_ANY, VERB_ANY, 0, verb_help }, | |
3276 | { "status", VERB_ANY, VERB_ANY, VERB_DEFAULT, verb_status }, | |
3277 | { "query", 2, VERB_ANY, 0, verb_query }, | |
3278 | { "service", 2, 4, 0, verb_service }, | |
3279 | { "openpgp", 2, VERB_ANY, 0, verb_openpgp }, | |
3280 | { "tlsa", 2, VERB_ANY, 0, verb_tlsa }, | |
3281 | { "statistics", VERB_ANY, 1, 0, show_statistics }, | |
3282 | { "reset-statistics", VERB_ANY, 1, 0, reset_statistics }, | |
3283 | { "flush-caches", VERB_ANY, 1, 0, flush_caches }, | |
3284 | { "reset-server-features", VERB_ANY, 1, 0, reset_server_features }, | |
3285 | { "dns", VERB_ANY, VERB_ANY, 0, verb_dns }, | |
3286 | { "domain", VERB_ANY, VERB_ANY, 0, verb_domain }, | |
6e866b33 | 3287 | { "default-route", VERB_ANY, 3, 0, verb_default_route }, |
b012e921 MB |
3288 | { "llmnr", VERB_ANY, 3, 0, verb_llmnr }, |
3289 | { "mdns", VERB_ANY, 3, 0, verb_mdns }, | |
6e866b33 | 3290 | { "dnsovertls", VERB_ANY, 3, 0, verb_dns_over_tls }, |
b012e921 MB |
3291 | { "dnssec", VERB_ANY, 3, 0, verb_dnssec }, |
3292 | { "nta", VERB_ANY, VERB_ANY, 0, verb_nta }, | |
6e866b33 | 3293 | { "revert", VERB_ANY, 2, 0, verb_revert_link }, |
a10f5d05 | 3294 | { "log-level", VERB_ANY, 2, 0, verb_log_level }, |
b012e921 MB |
3295 | {} |
3296 | }; | |
aa27b158 | 3297 | |
b012e921 MB |
3298 | return dispatch_verb(argc, argv, verbs, bus); |
3299 | } | |
aa27b158 | 3300 | |
b012e921 MB |
3301 | static int translate(const char *verb, const char *single_arg, size_t num_args, char **args, sd_bus *bus) { |
3302 | char **fake, **p; | |
a10f5d05 | 3303 | size_t num; |
4c89c718 | 3304 | |
b012e921 MB |
3305 | assert(verb); |
3306 | assert(num_args == 0 || args); | |
4c89c718 | 3307 | |
b012e921 | 3308 | num = !!single_arg + num_args + 1; |
4c89c718 | 3309 | |
b012e921 MB |
3310 | p = fake = newa0(char *, num + 1); |
3311 | *p++ = (char *) verb; | |
3312 | if (single_arg) | |
3313 | *p++ = (char *) single_arg; | |
a10f5d05 | 3314 | for (size_t i = 0; i < num_args; i++) |
b012e921 | 3315 | *p++ = args[i]; |
5a920b42 | 3316 | |
b012e921 MB |
3317 | optind = 0; |
3318 | return native_main((int) num, fake, bus); | |
3319 | } | |
3320 | ||
3321 | static int compat_main(int argc, char *argv[], sd_bus *bus) { | |
3322 | int r = 0; | |
5a920b42 | 3323 | |
b012e921 MB |
3324 | switch (arg_mode) { |
3325 | case MODE_RESOLVE_HOST: | |
3326 | case MODE_RESOLVE_RECORD: | |
3327 | return translate("query", NULL, argc - optind, argv + optind, bus); | |
3328 | ||
3329 | case MODE_RESOLVE_SERVICE: | |
3330 | return translate("service", NULL, argc - optind, argv + optind, bus); | |
3331 | ||
3332 | case MODE_RESOLVE_OPENPGP: | |
3333 | return translate("openpgp", NULL, argc - optind, argv + optind, bus); | |
3334 | ||
3335 | case MODE_RESOLVE_TLSA: | |
3336 | return translate("tlsa", arg_service_family, argc - optind, argv + optind, bus); | |
3337 | ||
3338 | case MODE_STATISTICS: | |
3339 | return translate("statistics", NULL, 0, NULL, bus); | |
3340 | ||
3341 | case MODE_RESET_STATISTICS: | |
3342 | return translate("reset-statistics", NULL, 0, NULL, bus); | |
3343 | ||
3344 | case MODE_FLUSH_CACHES: | |
3345 | return translate("flush-caches", NULL, 0, NULL, bus); | |
5a920b42 | 3346 | |
f5e65279 | 3347 | case MODE_RESET_SERVER_FEATURES: |
b012e921 MB |
3348 | return translate("reset-server-features", NULL, 0, NULL, bus); |
3349 | ||
3350 | case MODE_STATUS: | |
3351 | return translate("status", NULL, argc - optind, argv + optind, bus); | |
3352 | ||
3353 | case MODE_SET_LINK: | |
6e866b33 MB |
3354 | assert(arg_ifname); |
3355 | ||
b012e921 MB |
3356 | if (arg_set_dns) { |
3357 | r = translate("dns", arg_ifname, strv_length(arg_set_dns), arg_set_dns, bus); | |
3358 | if (r < 0) | |
3359 | return r; | |
f5e65279 MB |
3360 | } |
3361 | ||
b012e921 MB |
3362 | if (arg_set_domain) { |
3363 | r = translate("domain", arg_ifname, strv_length(arg_set_domain), arg_set_domain, bus); | |
3364 | if (r < 0) | |
3365 | return r; | |
3366 | } | |
f5e65279 | 3367 | |
b012e921 MB |
3368 | if (arg_set_nta) { |
3369 | r = translate("nta", arg_ifname, strv_length(arg_set_nta), arg_set_nta, bus); | |
3370 | if (r < 0) | |
3371 | return r; | |
3372 | } | |
5a920b42 | 3373 | |
b012e921 MB |
3374 | if (arg_set_llmnr) { |
3375 | r = translate("llmnr", arg_ifname, 1, (char **) &arg_set_llmnr, bus); | |
3376 | if (r < 0) | |
3377 | return r; | |
3378 | } | |
5a920b42 | 3379 | |
b012e921 MB |
3380 | if (arg_set_mdns) { |
3381 | r = translate("mdns", arg_ifname, 1, (char **) &arg_set_mdns, bus); | |
3382 | if (r < 0) | |
3383 | return r; | |
3384 | } | |
5a920b42 | 3385 | |
b012e921 MB |
3386 | if (arg_set_dns_over_tls) { |
3387 | r = translate("dnsovertls", arg_ifname, 1, (char **) &arg_set_dns_over_tls, bus); | |
3388 | if (r < 0) | |
3389 | return r; | |
3390 | } | |
52ad194e | 3391 | |
b012e921 MB |
3392 | if (arg_set_dnssec) { |
3393 | r = translate("dnssec", arg_ifname, 1, (char **) &arg_set_dnssec, bus); | |
3394 | if (r < 0) | |
3395 | return r; | |
52ad194e MB |
3396 | } |
3397 | ||
b012e921 | 3398 | return r; |
52ad194e MB |
3399 | |
3400 | case MODE_REVERT_LINK: | |
6e866b33 MB |
3401 | assert(arg_ifname); |
3402 | ||
b012e921 MB |
3403 | return translate("revert", arg_ifname, 0, NULL, bus); |
3404 | ||
3405 | case _MODE_INVALID: | |
3406 | assert_not_reached("invalid mode"); | |
3407 | } | |
3408 | ||
3409 | return 0; | |
3410 | } | |
3411 | ||
6e866b33 MB |
3412 | static int run(int argc, char **argv) { |
3413 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
b012e921 MB |
3414 | int r; |
3415 | ||
3416 | setlocale(LC_ALL, ""); | |
3a6ce677 | 3417 | log_setup(); |
b012e921 | 3418 | |
3a6ce677 | 3419 | if (invoked_as(argv, "resolvconf")) |
b012e921 | 3420 | r = resolvconf_parse_argv(argc, argv); |
3a6ce677 | 3421 | else if (invoked_as(argv, "systemd-resolve")) |
b012e921 MB |
3422 | r = compat_parse_argv(argc, argv); |
3423 | else | |
3424 | r = native_parse_argv(argc, argv); | |
3425 | if (r <= 0) | |
6e866b33 | 3426 | return r; |
52ad194e | 3427 | |
b012e921 | 3428 | r = sd_bus_open_system(&bus); |
6e866b33 MB |
3429 | if (r < 0) |
3430 | return log_error_errno(r, "sd_bus_open_system: %m"); | |
5a920b42 | 3431 | |
6e866b33 MB |
3432 | if (STR_IN_SET(program_invocation_short_name, "systemd-resolve", "resolvconf")) |
3433 | return compat_main(argc, argv, bus); | |
52ad194e | 3434 | |
6e866b33 | 3435 | return native_main(argc, argv, bus); |
4c89c718 | 3436 | } |
6e866b33 MB |
3437 | |
3438 | DEFINE_MAIN_FUNCTION(run); |