]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
bdef7319 | 2 | |
bdef7319 | 3 | #include <getopt.h> |
ca78ad1d | 4 | #include <locale.h> |
cf0fbc49 | 5 | #include <net/if.h> |
bdef7319 ZJS |
6 | |
7 | #include "sd-bus.h" | |
76d62b63 | 8 | #include "sd-daemon.h" |
284d7641 | 9 | #include "sd-event.h" |
309a747f | 10 | #include "sd-json.h" |
be371fe0 | 11 | #include "sd-netlink.h" |
25ff515b | 12 | #include "sd-varlink.h" |
3f6fd1ba LP |
13 | |
14 | #include "af-list.h" | |
b5efdb8a | 15 | #include "alloc-util.h" |
76d62b63 | 16 | #include "argv-util.h" |
d6b4d1c7 | 17 | #include "build.h" |
14965b94 | 18 | #include "bus-common-errors.h" |
bdef7319 | 19 | #include "bus-error.h" |
9b71e4ab | 20 | #include "bus-locator.h" |
807542be | 21 | #include "bus-map-properties.h" |
a574b7d1 | 22 | #include "bus-message-util.h" |
76d62b63 | 23 | #include "bus-util.h" |
14965b94 | 24 | #include "dns-domain.h" |
fffbf1dc | 25 | #include "errno-list.h" |
284d7641 | 26 | #include "errno-util.h" |
45ec7efb | 27 | #include "escape.h" |
868258cf | 28 | #include "format-ifname.h" |
29e15e98 | 29 | #include "format-table.h" |
058946d1 | 30 | #include "hostname-util.h" |
309a747f | 31 | #include "json-util.h" |
f6aa6190 | 32 | #include "main-func.h" |
47f761fd | 33 | #include "missing-network.h" |
be371fe0 | 34 | #include "netlink-util.h" |
7e8facb3 | 35 | #include "openssl-util.h" |
be371fe0 | 36 | #include "pager.h" |
599c7c54 | 37 | #include "parse-argument.h" |
6bedfcbb | 38 | #include "parse-util.h" |
76d62b63 | 39 | #include "polkit-agent.h" |
294bf0c3 | 40 | #include "pretty-print.h" |
088c1363 | 41 | #include "resolvconf-compat.h" |
c38a03df | 42 | #include "resolve-util.h" |
c2e84cab | 43 | #include "resolvectl.h" |
51323288 | 44 | #include "resolved-def.h" |
3f6fd1ba | 45 | #include "resolved-dns-packet.h" |
68527d30 | 46 | #include "resolved-dns-rr.h" |
058946d1 | 47 | #include "resolved-util.h" |
5c3fa98d | 48 | #include "socket-netlink.h" |
eb107675 | 49 | #include "sort-util.h" |
957d9df3 | 50 | #include "stdio-util.h" |
5c828e66 | 51 | #include "string-table.h" |
284d7641 | 52 | #include "string-util.h" |
be371fe0 | 53 | #include "strv.h" |
a150ff5e | 54 | #include "terminal-util.h" |
284d7641 | 55 | #include "time-util.h" |
7c502303 | 56 | #include "utf8.h" |
25ff515b | 57 | #include "varlink-util.h" |
a87b151a | 58 | #include "verb-log-control.h" |
a7a4c60a | 59 | #include "verbs.h" |
2d4c5cbc | 60 | |
bdef7319 | 61 | static int arg_family = AF_UNSPEC; |
a661dc36 YW |
62 | static int arg_ifindex = 0; |
63 | static char *arg_ifname = NULL; | |
4b548ef3 | 64 | static uint16_t arg_type = 0; |
2d4c5cbc | 65 | static uint16_t arg_class = 0; |
b93312f5 | 66 | static bool arg_legend = true; |
51323288 | 67 | static uint64_t arg_flags = 0; |
309a747f | 68 | static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF; |
0221d68a | 69 | static PagerFlags arg_pager_flags = 0; |
088c1363 | 70 | bool arg_ifindex_permissive = false; /* If true, don't generate an error if the specified interface index doesn't exist */ |
a7a4c60a | 71 | static const char *arg_service_family = NULL; |
1d7fa677 | 72 | static bool arg_ask_password = true; |
82d1d240 | 73 | |
dab48ea6 ZJS |
74 | typedef enum RawType { |
75 | RAW_NONE, | |
76 | RAW_PAYLOAD, | |
77 | RAW_PACKET, | |
78 | } RawType; | |
dab48ea6 | 79 | static RawType arg_raw = RAW_NONE; |
a150ff5e | 80 | |
374825ec | 81 | /* Used by compat interfaces: systemd-resolve and resolvconf. */ |
088c1363 | 82 | ExecutionMode arg_mode = MODE_RESOLVE_HOST; |
a7a4c60a | 83 | char **arg_set_dns = NULL; |
088c1363 | 84 | char **arg_set_domain = NULL; |
374825ec | 85 | bool arg_disable_default_route = false; |
a7a4c60a YW |
86 | static const char *arg_set_llmnr = NULL; |
87 | static const char *arg_set_mdns = NULL; | |
c9299be2 | 88 | static const char *arg_set_dns_over_tls = NULL; |
a7a4c60a | 89 | static const char *arg_set_dnssec = NULL; |
14965b94 LP |
90 | static char **arg_set_nta = NULL; |
91 | ||
f6aa6190 YW |
92 | STATIC_DESTRUCTOR_REGISTER(arg_ifname, freep); |
93 | STATIC_DESTRUCTOR_REGISTER(arg_set_dns, strv_freep); | |
94 | STATIC_DESTRUCTOR_REGISTER(arg_set_domain, strv_freep); | |
95 | STATIC_DESTRUCTOR_REGISTER(arg_set_nta, strv_freep); | |
96 | ||
a7a4c60a YW |
97 | typedef enum StatusMode { |
98 | STATUS_ALL, | |
99 | STATUS_DNS, | |
100 | STATUS_DOMAIN, | |
f2fd3cdb | 101 | STATUS_DEFAULT_ROUTE, |
a7a4c60a YW |
102 | STATUS_LLMNR, |
103 | STATUS_MDNS, | |
d050561a | 104 | STATUS_PRIVATE, |
a7a4c60a YW |
105 | STATUS_DNSSEC, |
106 | STATUS_NTA, | |
107 | } StatusMode; | |
108 | ||
eb107675 ZJS |
109 | typedef struct InterfaceInfo { |
110 | int index; | |
111 | const char *name; | |
112 | } InterfaceInfo; | |
113 | ||
614a6770 YW |
114 | static int acquire_bus(sd_bus **ret) { |
115 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; | |
116 | int r; | |
117 | ||
118 | assert(ret); | |
119 | ||
120 | r = sd_bus_open_system(&bus); | |
121 | if (r < 0) | |
122 | return log_error_errno(r, "sd_bus_open_system: %m"); | |
123 | ||
124 | (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password); | |
125 | ||
126 | *ret = TAKE_PTR(bus); | |
127 | return 0; | |
128 | } | |
129 | ||
eb107675 ZJS |
130 | static int interface_info_compare(const InterfaceInfo *a, const InterfaceInfo *b) { |
131 | int r; | |
132 | ||
133 | r = CMP(a->index, b->index); | |
134 | if (r != 0) | |
135 | return r; | |
136 | ||
137 | return strcmp_ptr(a->name, b->name); | |
138 | } | |
139 | ||
7d4e8503 YW |
140 | int ifname_mangle_full(const char *s, bool drop_protocol_specifier) { |
141 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
142 | _cleanup_strv_free_ char **found = NULL; | |
143 | int r; | |
a661dc36 YW |
144 | |
145 | assert(s); | |
146 | ||
7d4e8503 YW |
147 | if (drop_protocol_specifier) { |
148 | _cleanup_free_ char *buf = NULL; | |
149 | int ifindex_longest_name = -ENODEV; | |
a661dc36 | 150 | |
7d4e8503 | 151 | /* When invoked as resolvconf, drop the protocol specifier(s) at the end. */ |
8e5385b4 | 152 | |
7d4e8503 YW |
153 | buf = strdup(s); |
154 | if (!buf) | |
155 | return log_oom(); | |
a661dc36 | 156 | |
7d4e8503 YW |
157 | for (;;) { |
158 | r = rtnl_resolve_interface(&rtnl, buf); | |
159 | if (r > 0) { | |
160 | if (ifindex_longest_name <= 0) | |
161 | ifindex_longest_name = r; | |
a661dc36 | 162 | |
7d4e8503 YW |
163 | r = strv_extend(&found, buf); |
164 | if (r < 0) | |
165 | return log_oom(); | |
166 | } | |
a661dc36 | 167 | |
7d4e8503 YW |
168 | char *dot = strrchr(buf, '.'); |
169 | if (!dot) | |
170 | break; | |
a661dc36 | 171 | |
7d4e8503 YW |
172 | *dot = '\0'; |
173 | } | |
7875170f | 174 | |
7d4e8503 YW |
175 | unsigned n = strv_length(found); |
176 | if (n > 1) { | |
177 | _cleanup_free_ char *joined = NULL; | |
7875170f | 178 | |
7d4e8503 YW |
179 | joined = strv_join(found, ", "); |
180 | log_warning("Found multiple interfaces (%s) matching with '%s'. Using '%s' (ifindex=%i).", | |
181 | strna(joined), s, found[0], ifindex_longest_name); | |
7875170f | 182 | |
7d4e8503 YW |
183 | } else if (n == 1) { |
184 | const char *proto; | |
185 | ||
186 | proto = ASSERT_PTR(startswith(s, found[0])); | |
187 | if (!isempty(proto)) | |
188 | log_info("Dropped protocol specifier '%s' from '%s'. Using '%s' (ifindex=%i).", | |
189 | proto, s, found[0], ifindex_longest_name); | |
190 | } | |
191 | ||
192 | r = ifindex_longest_name; | |
7875170f | 193 | } else |
7d4e8503 YW |
194 | r = rtnl_resolve_interface(&rtnl, s); |
195 | if (r < 0) { | |
196 | if (ERRNO_IS_DEVICE_ABSENT(r) && arg_ifindex_permissive) { | |
197 | log_debug_errno(r, "Interface '%s' not found, but -f specified, ignoring: %m", s); | |
198 | return 0; /* done */ | |
199 | } | |
200 | return log_error_errno(r, "Failed to resolve interface \"%s\": %m", s); | |
201 | } | |
202 | ||
203 | if (arg_ifindex > 0 && arg_ifindex != r) | |
204 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified multiple different interfaces. Refusing."); | |
205 | ||
206 | arg_ifindex = r; | |
207 | return free_and_strdup_warn(&arg_ifname, found ? found[0] : s); /* found */ | |
7875170f MC |
208 | } |
209 | ||
78c6a153 | 210 | static void print_source(uint64_t flags, usec_t rtt) { |
51323288 LP |
211 | if (!arg_legend) |
212 | return; | |
213 | ||
23441a3d | 214 | if (sd_json_format_enabled(arg_json_format_flags)) |
3557f1a6 LP |
215 | return; |
216 | ||
78c6a153 | 217 | if (flags == 0) |
51323288 LP |
218 | return; |
219 | ||
ec4b9671 | 220 | printf("\n%s-- Information acquired via", ansi_grey()); |
51323288 | 221 | |
283ec789 YW |
222 | printf(" protocol%s%s%s%s%s", |
223 | flags & SD_RESOLVED_DNS ? " DNS" :"", | |
224 | flags & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "", | |
225 | flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "", | |
226 | flags & SD_RESOLVED_MDNS_IPV4 ? " mDNS/IPv4" : "", | |
227 | flags & SD_RESOLVED_MDNS_IPV6 ? " mDNS/IPv6" : ""); | |
51323288 | 228 | |
ec4b9671 | 229 | printf(" in %s.%s\n" |
43fc4baa | 230 | "%s-- Data is authenticated: %s; Data was acquired via local or encrypted transport: %s%s\n", |
5291f26d ZJS |
231 | FORMAT_TIMESPAN(rtt, 100), |
232 | ansi_normal(), | |
43fc4baa LP |
233 | ansi_grey(), |
234 | yes_no(flags & SD_RESOLVED_AUTHENTICATED), | |
235 | yes_no(flags & SD_RESOLVED_CONFIDENTIAL), | |
236 | ansi_normal()); | |
5c1790d1 LP |
237 | |
238 | if ((flags & (SD_RESOLVED_FROM_MASK|SD_RESOLVED_SYNTHETIC)) != 0) | |
239 | printf("%s-- Data from:%s%s%s%s%s%s\n", | |
240 | ansi_grey(), | |
241 | FLAGS_SET(flags, SD_RESOLVED_SYNTHETIC) ? " synthetic" : "", | |
242 | FLAGS_SET(flags, SD_RESOLVED_FROM_CACHE) ? " cache" : "", | |
243 | FLAGS_SET(flags, SD_RESOLVED_FROM_ZONE) ? " zone" : "", | |
244 | FLAGS_SET(flags, SD_RESOLVED_FROM_TRUST_ANCHOR) ? " trust-anchor" : "", | |
245 | FLAGS_SET(flags, SD_RESOLVED_FROM_NETWORK) ? " network" : "", | |
246 | ansi_normal()); | |
51323288 | 247 | } |
bdef7319 | 248 | |
ff4a77c3 | 249 | static void print_ifindex_comment(int printed_so_far, int ifindex) { |
01afd0f7 YW |
250 | char ifname[IF_NAMESIZE]; |
251 | int r; | |
74998408 | 252 | |
ff4a77c3 LP |
253 | if (ifindex <= 0) |
254 | return; | |
931851e8 | 255 | |
01afd0f7 YW |
256 | r = format_ifname(ifindex, ifname); |
257 | if (r < 0) | |
258 | return (void) log_warning_errno(r, "Failed to resolve interface name for index %i, ignoring: %m", ifindex); | |
259 | ||
260 | printf("%*s%s-- link: %s%s", | |
261 | 60 > printed_so_far ? 60 - printed_so_far : 0, " ", /* Align comment to the 60th column */ | |
262 | ansi_grey(), ifname, ansi_normal()); | |
51323288 | 263 | } |
bdef7319 | 264 | |
bbb86efa ZJS |
265 | static int resolve_host_error(const char *name, int r, const sd_bus_error *error) { |
266 | if (sd_bus_error_has_name(error, BUS_ERROR_DNS_NXDOMAIN)) | |
267 | return log_error_errno(r, "%s: %s", name, bus_error_message(error, r)); | |
268 | ||
269 | return log_error_errno(r, "%s: resolve call failed: %s", name, bus_error_message(error, r)); | |
270 | } | |
271 | ||
79266746 | 272 | static int resolve_host(sd_bus *bus, const char *name) { |
4afd3348 LP |
273 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; |
274 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
79266746 | 275 | const char *canonical = NULL; |
bdef7319 | 276 | unsigned c = 0; |
51323288 | 277 | uint64_t flags; |
74998408 | 278 | usec_t ts; |
a661dc36 | 279 | int r; |
bdef7319 ZJS |
280 | |
281 | assert(name); | |
282 | ||
23441a3d | 283 | if (sd_json_format_enabled(arg_json_format_flags)) |
3557f1a6 LP |
284 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type=A or --type=AAAA to acquire address record information in JSON format."); |
285 | ||
a661dc36 | 286 | log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); |
bdef7319 | 287 | |
d96f9abc | 288 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveHostname"); |
02dd6e18 LP |
289 | if (r < 0) |
290 | return bus_log_create_error(r); | |
bdef7319 | 291 | |
51323288 | 292 | r = sd_bus_message_append(req, "isit", arg_ifindex, name, arg_family, arg_flags); |
02dd6e18 LP |
293 | if (r < 0) |
294 | return bus_log_create_error(r); | |
bdef7319 | 295 | |
74998408 TG |
296 | ts = now(CLOCK_MONOTONIC); |
297 | ||
4cbfd62b | 298 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); |
45ec7efb | 299 | if (r < 0) |
bbb86efa | 300 | return resolve_host_error(name, r, &error); |
bdef7319 | 301 | |
74998408 TG |
302 | ts = now(CLOCK_MONOTONIC) - ts; |
303 | ||
78c6a153 | 304 | r = sd_bus_message_enter_container(reply, 'a', "(iiay)"); |
02dd6e18 LP |
305 | if (r < 0) |
306 | return bus_log_parse_error(r); | |
bdef7319 | 307 | |
78c6a153 | 308 | while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) { |
bdef7319 | 309 | _cleanup_free_ char *pretty = NULL; |
ff4a77c3 | 310 | int ifindex, family, k; |
a574b7d1 | 311 | union in_addr_union a; |
78c6a153 LP |
312 | |
313 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
bdef7319 | 314 | |
a574b7d1 | 315 | r = sd_bus_message_read(reply, "i", &ifindex); |
02dd6e18 LP |
316 | if (r < 0) |
317 | return bus_log_parse_error(r); | |
bdef7319 | 318 | |
a574b7d1 YW |
319 | sd_bus_error_free(&error); |
320 | r = bus_message_read_in_addr_auto(reply, &error, &family, &a); | |
321 | if (r < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) | |
322 | return log_error_errno(r, "%s: systemd-resolved returned invalid result: %s", name, bus_error_message(&error, r)); | |
bdef7319 | 323 | |
bdef7319 | 324 | r = sd_bus_message_exit_container(reply); |
02dd6e18 LP |
325 | if (r < 0) |
326 | return bus_log_parse_error(r); | |
bdef7319 | 327 | |
a574b7d1 YW |
328 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) { |
329 | log_debug_errno(r, "%s: systemd-resolved returned invalid result, ignoring: %s", name, bus_error_message(&error, r)); | |
bdef7319 ZJS |
330 | continue; |
331 | } | |
332 | ||
a574b7d1 | 333 | r = in_addr_ifindex_to_string(family, &a, ifindex, &pretty); |
78c6a153 LP |
334 | if (r < 0) |
335 | return log_error_errno(r, "Failed to print address for %s: %m", name); | |
bdef7319 | 336 | |
38585af3 | 337 | k = printf("%*s%s %s%s%s", |
ff4a77c3 | 338 | (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ", |
38585af3 | 339 | ansi_highlight(), pretty, ansi_normal()); |
ff4a77c3 LP |
340 | |
341 | print_ifindex_comment(k, ifindex); | |
342 | fputc('\n', stdout); | |
bdef7319 ZJS |
343 | |
344 | c++; | |
345 | } | |
79266746 LP |
346 | if (r < 0) |
347 | return bus_log_parse_error(r); | |
348 | ||
349 | r = sd_bus_message_exit_container(reply); | |
350 | if (r < 0) | |
351 | return bus_log_parse_error(r); | |
352 | ||
51323288 | 353 | r = sd_bus_message_read(reply, "st", &canonical, &flags); |
79266746 LP |
354 | if (r < 0) |
355 | return bus_log_parse_error(r); | |
356 | ||
ece174c5 | 357 | if (!streq(name, canonical)) |
79266746 LP |
358 | printf("%*s%s (%s)\n", |
359 | (int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ", | |
360 | canonical); | |
bdef7319 | 361 | |
d7a0f1f4 FS |
362 | if (c == 0) |
363 | return log_error_errno(SYNTHETIC_ERRNO(ESRCH), | |
364 | "%s: no addresses found", name); | |
79266746 | 365 | |
78c6a153 | 366 | print_source(flags, ts); |
51323288 | 367 | |
79266746 LP |
368 | return 0; |
369 | } | |
370 | ||
371 | static int resolve_address(sd_bus *bus, int family, const union in_addr_union *address, int ifindex) { | |
4afd3348 LP |
372 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; |
373 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
79266746 | 374 | _cleanup_free_ char *pretty = NULL; |
51323288 | 375 | uint64_t flags; |
79266746 | 376 | unsigned c = 0; |
74998408 | 377 | usec_t ts; |
79266746 LP |
378 | int r; |
379 | ||
380 | assert(bus); | |
381 | assert(IN_SET(family, AF_INET, AF_INET6)); | |
382 | assert(address); | |
383 | ||
23441a3d | 384 | if (sd_json_format_enabled(arg_json_format_flags)) |
3557f1a6 LP |
385 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); |
386 | ||
78c6a153 LP |
387 | if (ifindex <= 0) |
388 | ifindex = arg_ifindex; | |
389 | ||
145fab1e | 390 | r = in_addr_ifindex_to_string(family, address, ifindex, &pretty); |
79266746 LP |
391 | if (r < 0) |
392 | return log_oom(); | |
393 | ||
0889b815 | 394 | log_debug("Resolving %s.", pretty); |
79266746 | 395 | |
d96f9abc | 396 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveAddress"); |
79266746 LP |
397 | if (r < 0) |
398 | return bus_log_create_error(r); | |
399 | ||
51323288 | 400 | r = sd_bus_message_append(req, "ii", ifindex, family); |
79266746 LP |
401 | if (r < 0) |
402 | return bus_log_create_error(r); | |
403 | ||
404 | r = sd_bus_message_append_array(req, 'y', address, FAMILY_ADDRESS_SIZE(family)); | |
405 | if (r < 0) | |
406 | return bus_log_create_error(r); | |
407 | ||
51323288 | 408 | r = sd_bus_message_append(req, "t", arg_flags); |
79266746 LP |
409 | if (r < 0) |
410 | return bus_log_create_error(r); | |
411 | ||
74998408 TG |
412 | ts = now(CLOCK_MONOTONIC); |
413 | ||
4cbfd62b | 414 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); |
4ae25393 YW |
415 | if (r < 0) |
416 | return log_error_errno(r, "%s: resolve call failed: %s", pretty, bus_error_message(&error, r)); | |
79266746 | 417 | |
74998408 TG |
418 | ts = now(CLOCK_MONOTONIC) - ts; |
419 | ||
78c6a153 | 420 | r = sd_bus_message_enter_container(reply, 'a', "(is)"); |
79266746 LP |
421 | if (r < 0) |
422 | return bus_log_create_error(r); | |
423 | ||
78c6a153 LP |
424 | while ((r = sd_bus_message_enter_container(reply, 'r', "is")) > 0) { |
425 | const char *n; | |
0889b815 | 426 | int k; |
78c6a153 LP |
427 | |
428 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
79266746 | 429 | |
78c6a153 LP |
430 | r = sd_bus_message_read(reply, "is", &ifindex, &n); |
431 | if (r < 0) | |
432 | return r; | |
433 | ||
434 | r = sd_bus_message_exit_container(reply); | |
435 | if (r < 0) | |
436 | return r; | |
437 | ||
38585af3 LP |
438 | k = printf("%*s%s %s%s%s", |
439 | (int) strlen(pretty), c == 0 ? pretty : "", | |
440 | c == 0 ? ":" : " ", | |
441 | ansi_highlight(), n, ansi_normal()); | |
78c6a153 | 442 | |
0889b815 LP |
443 | print_ifindex_comment(k, ifindex); |
444 | fputc('\n', stdout); | |
79266746 LP |
445 | |
446 | c++; | |
bdef7319 | 447 | } |
79266746 LP |
448 | if (r < 0) |
449 | return bus_log_parse_error(r); | |
bdef7319 | 450 | |
02dd6e18 LP |
451 | r = sd_bus_message_exit_container(reply); |
452 | if (r < 0) | |
453 | return bus_log_parse_error(r); | |
454 | ||
51323288 LP |
455 | r = sd_bus_message_read(reply, "t", &flags); |
456 | if (r < 0) | |
457 | return bus_log_parse_error(r); | |
458 | ||
d7a0f1f4 FS |
459 | if (c == 0) |
460 | return log_error_errno(SYNTHETIC_ERRNO(ESRCH), | |
461 | "%s: no names found", pretty); | |
79266746 | 462 | |
78c6a153 | 463 | print_source(flags, ts); |
51323288 | 464 | |
79266746 LP |
465 | return 0; |
466 | } | |
467 | ||
dab48ea6 ZJS |
468 | static int output_rr_packet(const void *d, size_t l, int ifindex) { |
469 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
dab48ea6 | 470 | int r; |
dab48ea6 | 471 | |
3557f1a6 LP |
472 | assert(d || l == 0); |
473 | ||
ab26cdf7 | 474 | r = dns_resource_record_new_from_raw(&rr, d, l); |
dab48ea6 ZJS |
475 | if (r < 0) |
476 | return log_error_errno(r, "Failed to parse RR: %m"); | |
477 | ||
23441a3d | 478 | if (sd_json_format_enabled(arg_json_format_flags)) { |
309a747f | 479 | _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; |
3557f1a6 LP |
480 | r = dns_resource_record_to_json(rr, &j); |
481 | if (r < 0) | |
482 | return log_error_errno(r, "Failed to convert RR to JSON: %m"); | |
483 | ||
fbcfa943 LP |
484 | if (!j) |
485 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "JSON formatting for records of type %s (%u) not available.", dns_type_to_string(rr->key->type), rr->key->type); | |
486 | ||
309a747f | 487 | r = sd_json_variant_dump(j, arg_json_format_flags, NULL, NULL); |
3557f1a6 LP |
488 | if (r < 0) |
489 | return r; | |
490 | ||
491 | } else if (arg_raw == RAW_PAYLOAD) { | |
141894f5 | 492 | const void *data; |
dab48ea6 ZJS |
493 | ssize_t k; |
494 | ||
495 | k = dns_resource_record_payload(rr, &data); | |
231a0417 LP |
496 | if (k == -EINVAL) |
497 | return log_error_errno(k, "Dumping of binary payload not available for RRs of this type: %s", dns_type_to_string(rr->key->type)); | |
dab48ea6 ZJS |
498 | if (k < 0) |
499 | return log_error_errno(k, "Cannot dump RR: %m"); | |
500 | fwrite(data, 1, k, stdout); | |
501 | } else { | |
502 | const char *s; | |
0889b815 | 503 | int k; |
dab48ea6 ZJS |
504 | |
505 | s = dns_resource_record_to_string(rr); | |
506 | if (!s) | |
507 | return log_oom(); | |
508 | ||
0889b815 LP |
509 | k = printf("%s", s); |
510 | print_ifindex_comment(k, ifindex); | |
511 | fputc('\n', stdout); | |
dab48ea6 ZJS |
512 | } |
513 | ||
514 | return 0; | |
515 | } | |
516 | ||
018b642a LP |
517 | static int idna_candidate(const char *name, char **ret) { |
518 | _cleanup_free_ char *idnafied = NULL; | |
519 | int r; | |
520 | ||
521 | assert(name); | |
522 | assert(ret); | |
523 | ||
524 | r = dns_name_apply_idna(name, &idnafied); | |
525 | if (r < 0) | |
526 | return log_error_errno(r, "Failed to apply IDNA to name '%s': %m", name); | |
527 | if (r > 0 && !streq(name, idnafied)) { | |
528 | *ret = TAKE_PTR(idnafied); | |
529 | return true; | |
530 | } | |
531 | ||
532 | *ret = NULL; | |
533 | return false; | |
534 | } | |
535 | ||
058946d1 ZJS |
536 | static bool single_label_nonsynthetic(const char *name) { |
537 | _cleanup_free_ char *first_label = NULL; | |
538 | int r; | |
539 | ||
540 | if (!dns_name_is_single_label(name)) | |
541 | return false; | |
542 | ||
17f244e8 LP |
543 | if (is_localhost(name) || |
544 | is_gateway_hostname(name) || | |
545 | is_outbound_hostname(name) || | |
546 | is_dns_stub_hostname(name) || | |
547 | is_dns_proxy_stub_hostname(name)) | |
058946d1 ZJS |
548 | return false; |
549 | ||
550 | r = resolve_system_hostname(NULL, &first_label); | |
551 | if (r < 0) { | |
552 | log_warning_errno(r, "Failed to determine the hostname: %m"); | |
553 | return false; | |
554 | } | |
555 | ||
556 | return !streq(name, first_label); | |
557 | } | |
558 | ||
a60f4d0b | 559 | static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type, bool warn_missing) { |
4afd3348 LP |
560 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; |
561 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
018b642a LP |
562 | _cleanup_free_ char *idnafied = NULL; |
563 | bool needs_authentication = false; | |
2d4c5cbc | 564 | unsigned n = 0; |
51323288 | 565 | uint64_t flags; |
74998408 | 566 | usec_t ts; |
018b642a | 567 | int r; |
2d4c5cbc LP |
568 | |
569 | assert(name); | |
570 | ||
a661dc36 | 571 | log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(class), dns_type_to_string(type), isempty(arg_ifname) ? "*" : arg_ifname); |
2d4c5cbc | 572 | |
200b4f3d | 573 | if (dns_name_dot_suffixed(name) == 0 && single_label_nonsynthetic(name)) |
20e994b3 ZJS |
574 | log_notice("(Note that search domains are not appended when --type= is specified. " |
575 | "Please specify fully qualified domain names, or remove --type= switch from invocation in order to request regular hostname resolution.)"); | |
018b642a LP |
576 | |
577 | r = idna_candidate(name, &idnafied); | |
578 | if (r < 0) | |
579 | return r; | |
580 | if (r > 0) | |
20e994b3 | 581 | log_notice("(Note that IDNA translation is not applied when --type= is specified. " |
018b642a LP |
582 | "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.", |
583 | idnafied); | |
584 | ||
d96f9abc | 585 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveRecord"); |
2d4c5cbc LP |
586 | if (r < 0) |
587 | return bus_log_create_error(r); | |
588 | ||
c1dafe4f | 589 | r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, class, type, arg_flags); |
2d4c5cbc LP |
590 | if (r < 0) |
591 | return bus_log_create_error(r); | |
592 | ||
74998408 TG |
593 | ts = now(CLOCK_MONOTONIC); |
594 | ||
4cbfd62b | 595 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); |
2d4c5cbc | 596 | if (r < 0) { |
a60f4d0b SS |
597 | if (warn_missing || r != -ENXIO) |
598 | log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r)); | |
2d4c5cbc LP |
599 | return r; |
600 | } | |
601 | ||
74998408 TG |
602 | ts = now(CLOCK_MONOTONIC) - ts; |
603 | ||
78c6a153 | 604 | r = sd_bus_message_enter_container(reply, 'a', "(iqqay)"); |
51323288 LP |
605 | if (r < 0) |
606 | return bus_log_parse_error(r); | |
607 | ||
78c6a153 | 608 | while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) { |
2d4c5cbc | 609 | uint16_t c, t; |
78c6a153 | 610 | int ifindex; |
2d4c5cbc LP |
611 | const void *d; |
612 | size_t l; | |
613 | ||
78c6a153 LP |
614 | assert_cc(sizeof(int) == sizeof(int32_t)); |
615 | ||
616 | r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t); | |
2d4c5cbc LP |
617 | if (r < 0) |
618 | return bus_log_parse_error(r); | |
619 | ||
620 | r = sd_bus_message_read_array(reply, 'y', &d, &l); | |
621 | if (r < 0) | |
622 | return bus_log_parse_error(r); | |
623 | ||
624 | r = sd_bus_message_exit_container(reply); | |
625 | if (r < 0) | |
626 | return bus_log_parse_error(r); | |
627 | ||
dab48ea6 ZJS |
628 | if (arg_raw == RAW_PACKET) { |
629 | uint64_t u64 = htole64(l); | |
2d4c5cbc | 630 | |
dab48ea6 ZJS |
631 | fwrite(&u64, sizeof(u64), 1, stdout); |
632 | fwrite(d, 1, l, stdout); | |
2e74028a | 633 | } else { |
dab48ea6 ZJS |
634 | r = output_rr_packet(d, l, ifindex); |
635 | if (r < 0) | |
636 | return r; | |
2e74028a | 637 | } |
78c6a153 | 638 | |
41815a4a LP |
639 | if (dns_type_needs_authentication(t)) |
640 | needs_authentication = true; | |
641 | ||
2d4c5cbc LP |
642 | n++; |
643 | } | |
644 | if (r < 0) | |
645 | return bus_log_parse_error(r); | |
646 | ||
647 | r = sd_bus_message_exit_container(reply); | |
648 | if (r < 0) | |
649 | return bus_log_parse_error(r); | |
650 | ||
51323288 LP |
651 | r = sd_bus_message_read(reply, "t", &flags); |
652 | if (r < 0) | |
653 | return bus_log_parse_error(r); | |
654 | ||
2d4c5cbc | 655 | if (n == 0) { |
a60f4d0b SS |
656 | if (warn_missing) |
657 | log_error("%s: no records found", name); | |
2d4c5cbc LP |
658 | return -ESRCH; |
659 | } | |
660 | ||
78c6a153 | 661 | print_source(flags, ts); |
51323288 | 662 | |
41815a4a LP |
663 | if ((flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) { |
664 | fflush(stdout); | |
665 | ||
666 | fprintf(stderr, "\n%s" | |
667 | "WARNING: The resources shown contain cryptographic key data which could not be\n" | |
668 | " authenticated. It is not suitable to authenticate any communication.\n" | |
669 | " This is usually indication that DNSSEC authentication was not enabled\n" | |
670 | " or is not available for the selected protocol or DNS servers.%s\n", | |
671 | ansi_highlight_red(), | |
672 | ansi_normal()); | |
673 | } | |
674 | ||
2d4c5cbc LP |
675 | return 0; |
676 | } | |
677 | ||
c1dafe4f LP |
678 | static int resolve_rfc4501(sd_bus *bus, const char *name) { |
679 | uint16_t type = 0, class = 0; | |
680 | const char *p, *q, *n; | |
681 | int r; | |
682 | ||
683 | assert(bus); | |
684 | assert(name); | |
685 | assert(startswith(name, "dns:")); | |
686 | ||
687 | /* Parse RFC 4501 dns: URIs */ | |
688 | ||
689 | p = name + 4; | |
690 | ||
691 | if (p[0] == '/') { | |
692 | const char *e; | |
693 | ||
694 | if (p[1] != '/') | |
695 | goto invalid; | |
696 | ||
697 | e = strchr(p + 2, '/'); | |
698 | if (!e) | |
699 | goto invalid; | |
700 | ||
701 | if (e != p + 2) | |
702 | log_warning("DNS authority specification not supported; ignoring specified authority."); | |
703 | ||
704 | p = e + 1; | |
705 | } | |
706 | ||
707 | q = strchr(p, '?'); | |
708 | if (q) { | |
2f82562b | 709 | n = strndupa_safe(p, q - p); |
c1dafe4f LP |
710 | q++; |
711 | ||
712 | for (;;) { | |
713 | const char *f; | |
714 | ||
715 | f = startswith_no_case(q, "class="); | |
716 | if (f) { | |
717 | _cleanup_free_ char *t = NULL; | |
718 | const char *e; | |
719 | ||
baaa35ad ZJS |
720 | if (class != 0) |
721 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
722 | "DNS class specified twice."); | |
c1dafe4f LP |
723 | |
724 | e = strchrnul(f, ';'); | |
725 | t = strndup(f, e - f); | |
726 | if (!t) | |
727 | return log_oom(); | |
728 | ||
729 | r = dns_class_from_string(t); | |
baaa35ad | 730 | if (r < 0) |
7211c853 | 731 | return log_error_errno(r, "Unknown DNS class %s.", t); |
c1dafe4f LP |
732 | |
733 | class = r; | |
734 | ||
735 | if (*e == ';') { | |
736 | q = e + 1; | |
737 | continue; | |
738 | } | |
739 | ||
740 | break; | |
741 | } | |
742 | ||
743 | f = startswith_no_case(q, "type="); | |
744 | if (f) { | |
745 | _cleanup_free_ char *t = NULL; | |
746 | const char *e; | |
747 | ||
baaa35ad ZJS |
748 | if (type != 0) |
749 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
750 | "DNS type specified twice."); | |
c1dafe4f LP |
751 | |
752 | e = strchrnul(f, ';'); | |
753 | t = strndup(f, e - f); | |
754 | if (!t) | |
755 | return log_oom(); | |
756 | ||
757 | r = dns_type_from_string(t); | |
baaa35ad | 758 | if (r < 0) |
7211c853 | 759 | return log_error_errno(r, "Unknown DNS type %s: %m", t); |
c1dafe4f LP |
760 | |
761 | type = r; | |
762 | ||
763 | if (*e == ';') { | |
764 | q = e + 1; | |
765 | continue; | |
766 | } | |
767 | ||
768 | break; | |
769 | } | |
770 | ||
771 | goto invalid; | |
772 | } | |
773 | } else | |
774 | n = p; | |
775 | ||
c1dafe4f | 776 | if (class == 0) |
4ac2ca1b ZJS |
777 | class = arg_class ?: DNS_CLASS_IN; |
778 | if (type == 0) | |
779 | type = arg_type ?: DNS_TYPE_A; | |
c1dafe4f | 780 | |
a60f4d0b | 781 | return resolve_record(bus, n, class, type, true); |
c1dafe4f LP |
782 | |
783 | invalid: | |
baaa35ad ZJS |
784 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
785 | "Invalid DNS URI: %s", name); | |
c1dafe4f LP |
786 | } |
787 | ||
a7a4c60a | 788 | static int verb_query(int argc, char **argv, void *userdata) { |
614a6770 | 789 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
f75ecb9f | 790 | int ret = 0, r; |
a7a4c60a | 791 | |
614a6770 YW |
792 | r = acquire_bus(&bus); |
793 | if (r < 0) | |
794 | return r; | |
795 | ||
a7a4c60a | 796 | if (arg_type != 0) |
f75ecb9f YW |
797 | STRV_FOREACH(p, strv_skip(argv, 1)) |
798 | RET_GATHER(ret, resolve_record(bus, *p, arg_class, arg_type, true)); | |
a7a4c60a YW |
799 | |
800 | else | |
f75ecb9f | 801 | STRV_FOREACH(p, strv_skip(argv, 1)) { |
a7a4c60a | 802 | if (startswith(*p, "dns:")) |
f75ecb9f | 803 | RET_GATHER(ret, resolve_rfc4501(bus, *p)); |
a7a4c60a YW |
804 | else { |
805 | int family, ifindex; | |
806 | union in_addr_union a; | |
807 | ||
f75ecb9f YW |
808 | r = in_addr_ifindex_from_string_auto(*p, &family, &a, &ifindex); |
809 | if (r >= 0) | |
810 | RET_GATHER(ret, resolve_address(bus, family, &a, ifindex)); | |
a7a4c60a | 811 | else |
f75ecb9f | 812 | RET_GATHER(ret, resolve_host(bus, *p)); |
a7a4c60a | 813 | } |
a7a4c60a YW |
814 | } |
815 | ||
f75ecb9f | 816 | return ret; |
a7a4c60a YW |
817 | } |
818 | ||
45ec7efb LP |
819 | static int resolve_service(sd_bus *bus, const char *name, const char *type, const char *domain) { |
820 | const char *canonical_name, *canonical_type, *canonical_domain; | |
4afd3348 LP |
821 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL; |
822 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
45ec7efb LP |
823 | size_t indent, sz; |
824 | uint64_t flags; | |
825 | const char *p; | |
826 | unsigned c; | |
827 | usec_t ts; | |
828 | int r; | |
829 | ||
830 | assert(bus); | |
831 | assert(domain); | |
832 | ||
3c6f7c34 LP |
833 | name = empty_to_null(name); |
834 | type = empty_to_null(type); | |
45ec7efb | 835 | |
45ec7efb | 836 | if (name) |
a661dc36 | 837 | 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); |
45ec7efb | 838 | else if (type) |
a661dc36 | 839 | log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); |
45ec7efb | 840 | else |
a661dc36 | 841 | log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname); |
45ec7efb | 842 | |
d96f9abc | 843 | r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveService"); |
45ec7efb LP |
844 | if (r < 0) |
845 | return bus_log_create_error(r); | |
846 | ||
847 | r = sd_bus_message_append(req, "isssit", arg_ifindex, name, type, domain, arg_family, arg_flags); | |
848 | if (r < 0) | |
849 | return bus_log_create_error(r); | |
850 | ||
851 | ts = now(CLOCK_MONOTONIC); | |
852 | ||
4cbfd62b | 853 | r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply); |
45ec7efb LP |
854 | if (r < 0) |
855 | return log_error_errno(r, "Resolve call failed: %s", bus_error_message(&error, r)); | |
856 | ||
857 | ts = now(CLOCK_MONOTONIC) - ts; | |
858 | ||
859 | r = sd_bus_message_enter_container(reply, 'a', "(qqqsa(iiay)s)"); | |
860 | if (r < 0) | |
861 | return bus_log_parse_error(r); | |
862 | ||
863 | indent = | |
864 | (name ? strlen(name) + 1 : 0) + | |
865 | (type ? strlen(type) + 1 : 0) + | |
866 | strlen(domain) + 2; | |
867 | ||
868 | c = 0; | |
869 | while ((r = sd_bus_message_enter_container(reply, 'r', "qqqsa(iiay)s")) > 0) { | |
870 | uint16_t priority, weight, port; | |
871 | const char *hostname, *canonical; | |
872 | ||
873 | r = sd_bus_message_read(reply, "qqqs", &priority, &weight, &port, &hostname); | |
874 | if (r < 0) | |
875 | return bus_log_parse_error(r); | |
876 | ||
877 | if (name) | |
878 | printf("%*s%s", (int) strlen(name), c == 0 ? name : "", c == 0 ? "/" : " "); | |
879 | if (type) | |
880 | printf("%*s%s", (int) strlen(type), c == 0 ? type : "", c == 0 ? "/" : " "); | |
881 | ||
882 | printf("%*s%s %s:%u [priority=%u, weight=%u]\n", | |
883 | (int) strlen(domain), c == 0 ? domain : "", | |
884 | c == 0 ? ":" : " ", | |
885 | hostname, port, | |
886 | priority, weight); | |
887 | ||
888 | r = sd_bus_message_enter_container(reply, 'a', "(iiay)"); | |
889 | if (r < 0) | |
890 | return bus_log_parse_error(r); | |
891 | ||
892 | while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) { | |
893 | _cleanup_free_ char *pretty = NULL; | |
0889b815 | 894 | int ifindex, family, k; |
aea29a30 | 895 | union in_addr_union a; |
45ec7efb LP |
896 | |
897 | assert_cc(sizeof(int) == sizeof(int32_t)); | |
898 | ||
a574b7d1 | 899 | r = sd_bus_message_read(reply, "i", &ifindex); |
45ec7efb LP |
900 | if (r < 0) |
901 | return bus_log_parse_error(r); | |
902 | ||
a574b7d1 YW |
903 | sd_bus_error_free(&error); |
904 | r = bus_message_read_in_addr_auto(reply, &error, &family, &a); | |
905 | if (r < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) | |
906 | return log_error_errno(r, "%s: systemd-resolved returned invalid result: %s", name, bus_error_message(&error, r)); | |
45ec7efb LP |
907 | |
908 | r = sd_bus_message_exit_container(reply); | |
909 | if (r < 0) | |
910 | return bus_log_parse_error(r); | |
911 | ||
a574b7d1 YW |
912 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) { |
913 | log_debug_errno(r, "%s: systemd-resolved returned invalid result, ignoring: %s", name, bus_error_message(&error, r)); | |
45ec7efb LP |
914 | continue; |
915 | } | |
916 | ||
a574b7d1 | 917 | r = in_addr_ifindex_to_string(family, &a, ifindex, &pretty); |
45ec7efb LP |
918 | if (r < 0) |
919 | return log_error_errno(r, "Failed to print address for %s: %m", name); | |
920 | ||
0889b815 LP |
921 | k = printf("%*s%s", (int) indent, "", pretty); |
922 | print_ifindex_comment(k, ifindex); | |
923 | fputc('\n', stdout); | |
45ec7efb LP |
924 | } |
925 | if (r < 0) | |
926 | return bus_log_parse_error(r); | |
927 | ||
928 | r = sd_bus_message_exit_container(reply); | |
929 | if (r < 0) | |
930 | return bus_log_parse_error(r); | |
931 | ||
932 | r = sd_bus_message_read(reply, "s", &canonical); | |
933 | if (r < 0) | |
934 | return bus_log_parse_error(r); | |
935 | ||
936 | if (!streq(hostname, canonical)) | |
937 | printf("%*s(%s)\n", (int) indent, "", canonical); | |
938 | ||
939 | r = sd_bus_message_exit_container(reply); | |
940 | if (r < 0) | |
941 | return bus_log_parse_error(r); | |
942 | ||
943 | c++; | |
944 | } | |
945 | if (r < 0) | |
946 | return bus_log_parse_error(r); | |
947 | ||
948 | r = sd_bus_message_exit_container(reply); | |
949 | if (r < 0) | |
950 | return bus_log_parse_error(r); | |
951 | ||
952 | r = sd_bus_message_enter_container(reply, 'a', "ay"); | |
953 | if (r < 0) | |
954 | return bus_log_parse_error(r); | |
955 | ||
45ec7efb LP |
956 | while ((r = sd_bus_message_read_array(reply, 'y', (const void**) &p, &sz)) > 0) { |
957 | _cleanup_free_ char *escaped = NULL; | |
958 | ||
959 | escaped = cescape_length(p, sz); | |
960 | if (!escaped) | |
961 | return log_oom(); | |
962 | ||
963 | printf("%*s%s\n", (int) indent, "", escaped); | |
45ec7efb LP |
964 | } |
965 | if (r < 0) | |
966 | return bus_log_parse_error(r); | |
967 | ||
968 | r = sd_bus_message_exit_container(reply); | |
969 | if (r < 0) | |
970 | return bus_log_parse_error(r); | |
971 | ||
972 | r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags); | |
973 | if (r < 0) | |
974 | return bus_log_parse_error(r); | |
975 | ||
3c6f7c34 LP |
976 | canonical_name = empty_to_null(canonical_name); |
977 | canonical_type = empty_to_null(canonical_type); | |
45ec7efb LP |
978 | |
979 | if (!streq_ptr(name, canonical_name) || | |
980 | !streq_ptr(type, canonical_type) || | |
981 | !streq_ptr(domain, canonical_domain)) { | |
982 | ||
983 | printf("%*s(", (int) indent, ""); | |
984 | ||
985 | if (canonical_name) | |
986 | printf("%s/", canonical_name); | |
987 | if (canonical_type) | |
988 | printf("%s/", canonical_type); | |
989 | ||
990 | printf("%s)\n", canonical_domain); | |
991 | } | |
992 | ||
993 | print_source(flags, ts); | |
994 | ||
995 | return 0; | |
996 | } | |
997 | ||
a7a4c60a | 998 | static int verb_service(int argc, char **argv, void *userdata) { |
614a6770 YW |
999 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
1000 | int r; | |
1001 | ||
1002 | r = acquire_bus(&bus); | |
1003 | if (r < 0) | |
1004 | return r; | |
a7a4c60a | 1005 | |
23441a3d | 1006 | if (sd_json_format_enabled(arg_json_format_flags)) |
3557f1a6 LP |
1007 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); |
1008 | ||
a7a4c60a YW |
1009 | if (argc == 2) |
1010 | return resolve_service(bus, NULL, NULL, argv[1]); | |
1011 | else if (argc == 3) | |
1012 | return resolve_service(bus, NULL, argv[1], argv[2]); | |
1013 | else | |
1014 | return resolve_service(bus, argv[1], argv[2], argv[3]); | |
1015 | } | |
1016 | ||
4ac2ca1b ZJS |
1017 | static int resolve_openpgp(sd_bus *bus, const char *address) { |
1018 | const char *domain, *full; | |
1019 | int r; | |
1020 | _cleanup_free_ char *hashed = NULL; | |
1021 | ||
1022 | assert(bus); | |
1023 | assert(address); | |
1024 | ||
1025 | domain = strrchr(address, '@'); | |
baaa35ad ZJS |
1026 | if (!domain) |
1027 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1028 | "Address does not contain '@': \"%s\"", address); | |
1029 | if (domain == address || domain[1] == '\0') | |
1030 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1031 | "Address starts or ends with '@': \"%s\"", address); | |
4ac2ca1b ZJS |
1032 | domain++; |
1033 | ||
a60f4d0b | 1034 | r = string_hashsum_sha256(address, domain - 1 - address, &hashed); |
4ac2ca1b ZJS |
1035 | if (r < 0) |
1036 | return log_error_errno(r, "Hashing failed: %m"); | |
1037 | ||
a60f4d0b SS |
1038 | strshorten(hashed, 56); |
1039 | ||
4ac2ca1b ZJS |
1040 | full = strjoina(hashed, "._openpgpkey.", domain); |
1041 | log_debug("Looking up \"%s\".", full); | |
1042 | ||
a60f4d0b SS |
1043 | r = resolve_record(bus, full, |
1044 | arg_class ?: DNS_CLASS_IN, | |
1045 | arg_type ?: DNS_TYPE_OPENPGPKEY, false); | |
1046 | ||
1047 | if (IN_SET(r, -ENXIO, -ESRCH)) { /* NXDOMAIN or NODATA? */ | |
670e95ae | 1048 | hashed = mfree(hashed); |
a60f4d0b SS |
1049 | r = string_hashsum_sha224(address, domain - 1 - address, &hashed); |
1050 | if (r < 0) | |
1051 | return log_error_errno(r, "Hashing failed: %m"); | |
1052 | ||
1053 | full = strjoina(hashed, "._openpgpkey.", domain); | |
1054 | log_debug("Looking up \"%s\".", full); | |
1055 | ||
1056 | return resolve_record(bus, full, | |
1057 | arg_class ?: DNS_CLASS_IN, | |
1058 | arg_type ?: DNS_TYPE_OPENPGPKEY, true); | |
1059 | } | |
1060 | ||
1061 | return r; | |
4ac2ca1b ZJS |
1062 | } |
1063 | ||
a7a4c60a | 1064 | static int verb_openpgp(int argc, char **argv, void *userdata) { |
614a6770 YW |
1065 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
1066 | int r, ret = 0; | |
1067 | ||
1068 | r = acquire_bus(&bus); | |
1069 | if (r < 0) | |
1070 | return r; | |
a7a4c60a | 1071 | |
23441a3d | 1072 | if (sd_json_format_enabled(arg_json_format_flags)) |
3557f1a6 LP |
1073 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); |
1074 | ||
f75ecb9f YW |
1075 | STRV_FOREACH(p, strv_skip(argv, 1)) |
1076 | RET_GATHER(ret, resolve_openpgp(bus, *p)); | |
a7a4c60a | 1077 | |
f75ecb9f | 1078 | return ret; |
a7a4c60a YW |
1079 | } |
1080 | ||
ebbc70e5 | 1081 | static int resolve_tlsa(sd_bus *bus, const char *family, const char *address) { |
82d1d240 ZJS |
1082 | const char *port; |
1083 | uint16_t port_num = 443; | |
1084 | _cleanup_free_ char *full = NULL; | |
1085 | int r; | |
1086 | ||
1087 | assert(bus); | |
1088 | assert(address); | |
1089 | ||
1090 | port = strrchr(address, ':'); | |
1091 | if (port) { | |
10452f7c SS |
1092 | r = parse_ip_port(port + 1, &port_num); |
1093 | if (r < 0) | |
82d1d240 ZJS |
1094 | return log_error_errno(r, "Invalid port \"%s\".", port + 1); |
1095 | ||
2f82562b | 1096 | address = strndupa_safe(address, port - address); |
82d1d240 ZJS |
1097 | } |
1098 | ||
ebbc70e5 | 1099 | r = asprintf(&full, "_%u._%s.%s", |
82d1d240 | 1100 | port_num, |
ebbc70e5 | 1101 | family, |
82d1d240 ZJS |
1102 | address); |
1103 | if (r < 0) | |
1104 | return log_oom(); | |
1105 | ||
1106 | log_debug("Looking up \"%s\".", full); | |
1107 | ||
1108 | return resolve_record(bus, full, | |
1109 | arg_class ?: DNS_CLASS_IN, | |
a60f4d0b | 1110 | arg_type ?: DNS_TYPE_TLSA, true); |
82d1d240 ZJS |
1111 | } |
1112 | ||
ebbc70e5 YW |
1113 | static bool service_family_is_valid(const char *s) { |
1114 | return STR_IN_SET(s, "tcp", "udp", "sctp"); | |
1115 | } | |
1116 | ||
a7a4c60a | 1117 | static int verb_tlsa(int argc, char **argv, void *userdata) { |
614a6770 | 1118 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
ebbc70e5 | 1119 | const char *family = "tcp"; |
f75ecb9f | 1120 | char **args; |
614a6770 | 1121 | int r, ret = 0; |
f75ecb9f YW |
1122 | |
1123 | assert(argc >= 2); | |
a7a4c60a | 1124 | |
614a6770 YW |
1125 | r = acquire_bus(&bus); |
1126 | if (r < 0) | |
1127 | return r; | |
1128 | ||
23441a3d | 1129 | if (sd_json_format_enabled(arg_json_format_flags)) |
3557f1a6 LP |
1130 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Use --json=pretty with --type= to acquire resource record information in JSON format."); |
1131 | ||
ebbc70e5 YW |
1132 | if (service_family_is_valid(argv[1])) { |
1133 | family = argv[1]; | |
f75ecb9f YW |
1134 | args = strv_skip(argv, 2); |
1135 | } else | |
1136 | args = strv_skip(argv, 1); | |
a7a4c60a | 1137 | |
f75ecb9f YW |
1138 | STRV_FOREACH(p, args) |
1139 | RET_GATHER(ret, resolve_tlsa(bus, family, *p)); | |
a7a4c60a | 1140 | |
f75ecb9f | 1141 | return ret; |
a7a4c60a YW |
1142 | } |
1143 | ||
1144 | static int show_statistics(int argc, char **argv, void *userdata) { | |
29e15e98 | 1145 | _cleanup_(table_unrefp) Table *table = NULL; |
309a747f | 1146 | sd_json_variant *reply = NULL; |
25ff515b | 1147 | _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
bc837621 | 1148 | int r; |
a150ff5e | 1149 | |
cf01bbb7 YW |
1150 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
1151 | ||
25ff515b | 1152 | r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor"); |
593f665c | 1153 | if (r < 0) |
bc837621 | 1154 | return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m"); |
593f665c | 1155 | |
cf01bbb7 YW |
1156 | r = varlink_callbo_and_log( |
1157 | vl, | |
1158 | "io.systemd.Resolve.Monitor.DumpStatistics", | |
1159 | &reply, | |
1160 | SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password)); | |
a150ff5e | 1161 | if (r < 0) |
71d0ecc5 | 1162 | return r; |
a150ff5e | 1163 | |
23441a3d | 1164 | if (sd_json_format_enabled(arg_json_format_flags)) |
309a747f | 1165 | return sd_json_variant_dump(reply, arg_json_format_flags, NULL, NULL); |
a150ff5e | 1166 | |
bc837621 | 1167 | struct statistics { |
309a747f LP |
1168 | sd_json_variant *transactions; |
1169 | sd_json_variant *cache; | |
1170 | sd_json_variant *dnssec; | |
bc837621 KV |
1171 | } statistics; |
1172 | ||
309a747f LP |
1173 | static const sd_json_dispatch_field statistics_dispatch_table[] = { |
1174 | { "transactions", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, transactions), SD_JSON_MANDATORY }, | |
1175 | { "cache", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, cache), SD_JSON_MANDATORY }, | |
1176 | { "dnssec", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct statistics, dnssec), SD_JSON_MANDATORY }, | |
bc837621 KV |
1177 | {}, |
1178 | }; | |
1179 | ||
309a747f | 1180 | r = sd_json_dispatch(reply, statistics_dispatch_table, SD_JSON_LOG, &statistics); |
f7700834 | 1181 | if (r < 0) |
bc837621 KV |
1182 | return r; |
1183 | ||
1184 | struct transactions { | |
1185 | uint64_t n_current_transactions; | |
1186 | uint64_t n_transactions_total; | |
1187 | uint64_t n_timeouts_total; | |
1188 | uint64_t n_timeouts_served_stale_total; | |
1189 | uint64_t n_failure_responses_total; | |
1190 | uint64_t n_failure_responses_served_stale_total; | |
1191 | } transactions; | |
1192 | ||
309a747f LP |
1193 | static const sd_json_dispatch_field transactions_dispatch_table[] = { |
1194 | { "currentTransactions", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_current_transactions), SD_JSON_MANDATORY }, | |
1195 | { "totalTransactions", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_transactions_total), SD_JSON_MANDATORY }, | |
1196 | { "totalTimeouts", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_timeouts_total), SD_JSON_MANDATORY }, | |
1197 | { "totalTimeoutsServedStale", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_timeouts_served_stale_total), SD_JSON_MANDATORY }, | |
1198 | { "totalFailedResponses", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_total), SD_JSON_MANDATORY }, | |
1199 | { "totalFailedResponsesServedStale", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct transactions, n_failure_responses_served_stale_total), SD_JSON_MANDATORY }, | |
bc837621 KV |
1200 | {}, |
1201 | }; | |
a150ff5e | 1202 | |
309a747f | 1203 | r = sd_json_dispatch(statistics.transactions, transactions_dispatch_table, SD_JSON_LOG, &transactions); |
a150ff5e | 1204 | if (r < 0) |
bc837621 | 1205 | return r; |
a150ff5e | 1206 | |
bc837621 KV |
1207 | struct cache { |
1208 | uint64_t cache_size; | |
1209 | uint64_t n_cache_hit; | |
1210 | uint64_t n_cache_miss; | |
1211 | } cache; | |
a150ff5e | 1212 | |
309a747f LP |
1213 | static const sd_json_dispatch_field cache_dispatch_table[] = { |
1214 | { "size", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, cache_size), SD_JSON_MANDATORY }, | |
1215 | { "hits", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, n_cache_hit), SD_JSON_MANDATORY }, | |
1216 | { "misses", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct cache, n_cache_miss), SD_JSON_MANDATORY }, | |
bc837621 KV |
1217 | {}, |
1218 | }; | |
1219 | ||
309a747f | 1220 | r = sd_json_dispatch(statistics.cache, cache_dispatch_table, SD_JSON_LOG, &cache); |
f7700834 | 1221 | if (r < 0) |
bc837621 KV |
1222 | return r; |
1223 | ||
1224 | struct dnsssec { | |
1225 | uint64_t n_dnssec_secure; | |
1226 | uint64_t n_dnssec_insecure; | |
1227 | uint64_t n_dnssec_bogus; | |
1228 | uint64_t n_dnssec_indeterminate; | |
1229 | } dnsssec; | |
1230 | ||
309a747f LP |
1231 | static const sd_json_dispatch_field dnssec_dispatch_table[] = { |
1232 | { "secure", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_secure), SD_JSON_MANDATORY }, | |
1233 | { "insecure", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_insecure), SD_JSON_MANDATORY }, | |
1234 | { "bogus", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_bogus), SD_JSON_MANDATORY }, | |
1235 | { "indeterminate", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct dnsssec, n_dnssec_indeterminate), SD_JSON_MANDATORY }, | |
bc837621 KV |
1236 | {}, |
1237 | }; | |
a150ff5e | 1238 | |
309a747f | 1239 | r = sd_json_dispatch(statistics.dnssec, dnssec_dispatch_table, SD_JSON_LOG, &dnsssec); |
a150ff5e | 1240 | if (r < 0) |
bc837621 | 1241 | return r; |
a150ff5e | 1242 | |
37a50123 | 1243 | table = table_new_vertical(); |
29e15e98 YW |
1244 | if (!table) |
1245 | return log_oom(); | |
1246 | ||
29e15e98 YW |
1247 | r = table_add_many(table, |
1248 | TABLE_STRING, "Transactions", | |
1249 | TABLE_SET_COLOR, ansi_highlight(), | |
37a50123 | 1250 | TABLE_SET_ALIGN_PERCENT, 0, |
29e15e98 | 1251 | TABLE_EMPTY, |
37a50123 | 1252 | TABLE_FIELD, "Current Transactions", |
29e15e98 | 1253 | TABLE_SET_ALIGN_PERCENT, 100, |
bc837621 | 1254 | TABLE_UINT64, transactions.n_current_transactions, |
37a50123 LP |
1255 | TABLE_SET_ALIGN_PERCENT, 100, |
1256 | TABLE_FIELD, "Total Transactions", | |
bc837621 | 1257 | TABLE_UINT64, transactions.n_transactions_total, |
29e15e98 YW |
1258 | TABLE_EMPTY, TABLE_EMPTY, |
1259 | TABLE_STRING, "Cache", | |
1260 | TABLE_SET_COLOR, ansi_highlight(), | |
1261 | TABLE_SET_ALIGN_PERCENT, 0, | |
1262 | TABLE_EMPTY, | |
37a50123 | 1263 | TABLE_FIELD, "Current Cache Size", |
29e15e98 | 1264 | TABLE_SET_ALIGN_PERCENT, 100, |
bc837621 | 1265 | TABLE_UINT64, cache.cache_size, |
37a50123 | 1266 | TABLE_FIELD, "Cache Hits", |
bc837621 | 1267 | TABLE_UINT64, cache.n_cache_hit, |
37a50123 | 1268 | TABLE_FIELD, "Cache Misses", |
bc837621 KV |
1269 | TABLE_UINT64, cache.n_cache_miss, |
1270 | TABLE_EMPTY, TABLE_EMPTY, | |
1271 | TABLE_STRING, "Failure Transactions", | |
1272 | TABLE_SET_COLOR, ansi_highlight(), | |
1273 | TABLE_SET_ALIGN_PERCENT, 0, | |
1274 | TABLE_EMPTY, | |
1275 | TABLE_FIELD, "Total Timeouts", | |
1276 | TABLE_SET_ALIGN_PERCENT, 100, | |
1277 | TABLE_UINT64, transactions.n_timeouts_total, | |
1278 | TABLE_FIELD, "Total Timeouts (Stale Data Served)", | |
1279 | TABLE_UINT64, transactions.n_timeouts_served_stale_total, | |
1280 | TABLE_FIELD, "Total Failure Responses", | |
1281 | TABLE_UINT64, transactions.n_failure_responses_total, | |
1282 | TABLE_FIELD, "Total Failure Responses (Stale Data Served)", | |
1283 | TABLE_UINT64, transactions.n_failure_responses_served_stale_total, | |
29e15e98 YW |
1284 | TABLE_EMPTY, TABLE_EMPTY, |
1285 | TABLE_STRING, "DNSSEC Verdicts", | |
1286 | TABLE_SET_COLOR, ansi_highlight(), | |
1287 | TABLE_SET_ALIGN_PERCENT, 0, | |
1288 | TABLE_EMPTY, | |
37a50123 | 1289 | TABLE_FIELD, "Secure", |
29e15e98 | 1290 | TABLE_SET_ALIGN_PERCENT, 100, |
bc837621 | 1291 | TABLE_UINT64, dnsssec.n_dnssec_secure, |
37a50123 | 1292 | TABLE_FIELD, "Insecure", |
bc837621 | 1293 | TABLE_UINT64, dnsssec.n_dnssec_insecure, |
37a50123 | 1294 | TABLE_FIELD, "Bogus", |
bc837621 | 1295 | TABLE_UINT64, dnsssec.n_dnssec_bogus, |
e30b4c13 | 1296 | TABLE_FIELD, "Indeterminate", |
bc837621 KV |
1297 | TABLE_UINT64, dnsssec.n_dnssec_indeterminate |
1298 | ); | |
29e15e98 | 1299 | if (r < 0) |
85840949 | 1300 | return table_log_add_error(r); |
29e15e98 YW |
1301 | |
1302 | r = table_print(table, NULL); | |
1303 | if (r < 0) | |
4b6607d9 | 1304 | return table_log_print_error(r); |
a150ff5e LP |
1305 | |
1306 | return 0; | |
1307 | } | |
1308 | ||
a7a4c60a | 1309 | static int reset_statistics(int argc, char **argv, void *userdata) { |
309a747f | 1310 | sd_json_variant *reply = NULL; |
25ff515b | 1311 | _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
a150ff5e LP |
1312 | int r; |
1313 | ||
cf01bbb7 YW |
1314 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
1315 | ||
25ff515b | 1316 | r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor"); |
a150ff5e | 1317 | if (r < 0) |
bc837621 KV |
1318 | return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m"); |
1319 | ||
cf01bbb7 YW |
1320 | r = varlink_callbo_and_log( |
1321 | vl, | |
1322 | "io.systemd.Resolve.Monitor.ResetStatistics", | |
1323 | &reply, | |
1324 | SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password)); | |
bc837621 | 1325 | if (r < 0) |
71d0ecc5 | 1326 | return r; |
bc837621 | 1327 | |
23441a3d | 1328 | if (sd_json_format_enabled(arg_json_format_flags)) |
309a747f | 1329 | return sd_json_variant_dump(reply, arg_json_format_flags, NULL, NULL); |
a150ff5e LP |
1330 | |
1331 | return 0; | |
1332 | } | |
1333 | ||
a7a4c60a | 1334 | static int flush_caches(int argc, char **argv, void *userdata) { |
614a6770 | 1335 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
ba35662f LP |
1336 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1337 | int r; | |
1338 | ||
614a6770 YW |
1339 | r = acquire_bus(&bus); |
1340 | if (r < 0) | |
1341 | return r; | |
1342 | ||
d96f9abc | 1343 | r = bus_call_method(bus, bus_resolve_mgr, "FlushCaches", &error, NULL, NULL); |
ba35662f LP |
1344 | if (r < 0) |
1345 | return log_error_errno(r, "Failed to flush caches: %s", bus_error_message(&error, r)); | |
1346 | ||
1347 | return 0; | |
1348 | } | |
1349 | ||
a7a4c60a | 1350 | static int reset_server_features(int argc, char **argv, void *userdata) { |
614a6770 | 1351 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
d55b0463 LP |
1352 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1353 | int r; | |
1354 | ||
614a6770 YW |
1355 | r = acquire_bus(&bus); |
1356 | if (r < 0) | |
1357 | return r; | |
1358 | ||
d96f9abc | 1359 | r = bus_call_method(bus, bus_resolve_mgr, "ResetServerFeatures", &error, NULL, NULL); |
d55b0463 LP |
1360 | if (r < 0) |
1361 | return log_error_errno(r, "Failed to reset server features: %s", bus_error_message(&error, r)); | |
1362 | ||
1363 | return 0; | |
1364 | } | |
1365 | ||
90293a95 YW |
1366 | typedef enum { |
1367 | READ_DNS_WITH_IFINDEX = 1 << 0, /* read "ifindex" reply that also carries an interface index */ | |
1368 | READ_DNS_EXTENDED = 1 << 1, /* read "extended" reply, i.e. with port number and server name */ | |
1369 | READ_DNS_ONLY_GLOBAL = 1 << 2, /* suppress entries with an (non-loopback) ifindex set (i.e. which are specific to some interface) */ | |
1370 | } ReadDNSFlag; | |
889a1b9f | 1371 | |
5c45b341 LP |
1372 | static const char *dns_server_property_signature(ReadDNSFlag flags) { |
1373 | switch (flags & (READ_DNS_WITH_IFINDEX|READ_DNS_EXTENDED)) { | |
1374 | ||
1375 | case 0: | |
1376 | return "iay"; | |
1377 | ||
1378 | case READ_DNS_WITH_IFINDEX: | |
1379 | return "iiay"; | |
1380 | ||
1381 | case READ_DNS_EXTENDED: | |
1382 | return "iayqs"; | |
1383 | ||
1384 | case READ_DNS_WITH_IFINDEX|READ_DNS_EXTENDED: | |
1385 | return "iiayqs"; | |
1386 | ||
1387 | default: | |
1388 | assert_not_reached(); | |
1389 | } | |
1390 | } | |
1391 | ||
90293a95 | 1392 | static int read_dns_server_one(sd_bus_message *m, ReadDNSFlag flags, char **ret) { |
a574b7d1 | 1393 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
58f48a56 | 1394 | _cleanup_free_ char *pretty = NULL; |
a574b7d1 | 1395 | union in_addr_union a; |
a747e71c | 1396 | const char *name = NULL; |
a5e6c849 | 1397 | int32_t ifindex = 0; |
06a5d24d | 1398 | int family, r; |
a747e71c | 1399 | uint16_t port = 0; |
58f48a56 YW |
1400 | |
1401 | assert(m); | |
1402 | assert(ret); | |
1403 | ||
5707fb12 LP |
1404 | r = sd_bus_message_enter_container( |
1405 | m, | |
1406 | 'r', | |
5c45b341 | 1407 | dns_server_property_signature(flags)); |
58f48a56 YW |
1408 | if (r <= 0) |
1409 | return r; | |
1410 | ||
90293a95 | 1411 | if (FLAGS_SET(flags, READ_DNS_WITH_IFINDEX)) { |
58f48a56 YW |
1412 | r = sd_bus_message_read(m, "i", &ifindex); |
1413 | if (r < 0) | |
1414 | return r; | |
1415 | } | |
1416 | ||
06a5d24d YW |
1417 | r = bus_message_read_in_addr_auto(m, &error, &family, &a); |
1418 | if (r < 0) { | |
1419 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) { | |
1420 | /* CurrentDNSServer provides AF_UNSPEC when no current server assigned. */ | |
1421 | log_debug("Invalid DNS server, ignoring: %s", bus_error_message(&error, r)); | |
1422 | ||
1423 | for (;;) { | |
1424 | r = sd_bus_message_skip(m, NULL); | |
1425 | if (r == -ENXIO) /* End of the container */ | |
1426 | break; | |
1427 | if (r < 0) | |
1428 | return r; | |
1429 | } | |
1430 | ||
1431 | r = sd_bus_message_exit_container(m); | |
1432 | if (r < 0) | |
1433 | return r; | |
1434 | ||
1435 | *ret = NULL; | |
1436 | return 1; | |
1437 | } | |
1438 | ||
1439 | return r; | |
1440 | } | |
58f48a56 | 1441 | |
90293a95 | 1442 | if (FLAGS_SET(flags, READ_DNS_EXTENDED)) { |
5c45b341 | 1443 | r = sd_bus_message_read(m, "qs", &port, &name); |
a747e71c YW |
1444 | if (r < 0) |
1445 | return r; | |
1446 | } | |
1447 | ||
58f48a56 YW |
1448 | r = sd_bus_message_exit_container(m); |
1449 | if (r < 0) | |
1450 | return r; | |
1451 | ||
90293a95 | 1452 | if (FLAGS_SET(flags, READ_DNS_ONLY_GLOBAL) && ifindex > 0 && ifindex != LOOPBACK_IFINDEX) { |
889a1b9f | 1453 | /* This one has an (non-loopback) ifindex set, and we were told to suppress those. Hence do so. */ |
58f48a56 YW |
1454 | *ret = NULL; |
1455 | return 1; | |
1456 | } | |
1457 | ||
a574b7d1 | 1458 | r = in_addr_port_ifindex_name_to_string(family, &a, port, ifindex, name, &pretty); |
58f48a56 YW |
1459 | if (r < 0) |
1460 | return r; | |
1461 | ||
1462 | *ret = TAKE_PTR(pretty); | |
58f48a56 YW |
1463 | return 1; |
1464 | } | |
1465 | ||
45817e92 | 1466 | static int map_dns_servers_internal(sd_bus *bus, const char *member, sd_bus_message *m, ReadDNSFlag flags, sd_bus_error *error, void *userdata) { |
99534007 | 1467 | char ***l = ASSERT_PTR(userdata); |
be371fe0 LP |
1468 | int r; |
1469 | ||
1470 | assert(bus); | |
1471 | assert(member); | |
1472 | assert(m); | |
be371fe0 | 1473 | |
5c45b341 LP |
1474 | const char *sig = strjoina("(", dns_server_property_signature(flags), ")"); |
1475 | ||
1476 | r = sd_bus_message_enter_container(m, 'a', sig); | |
be371fe0 LP |
1477 | if (r < 0) |
1478 | return r; | |
1479 | ||
1480 | for (;;) { | |
6abdec98 | 1481 | _cleanup_free_ char *pretty = NULL; |
be371fe0 | 1482 | |
90293a95 | 1483 | r = read_dns_server_one(m, flags, &pretty); |
be371fe0 LP |
1484 | if (r < 0) |
1485 | return r; | |
1486 | if (r == 0) | |
1487 | break; | |
1488 | ||
58f48a56 | 1489 | if (isempty(pretty)) |
be371fe0 | 1490 | continue; |
be371fe0 | 1491 | |
6abdec98 | 1492 | r = strv_consume(l, TAKE_PTR(pretty)); |
be371fe0 LP |
1493 | if (r < 0) |
1494 | return r; | |
1495 | } | |
1496 | ||
1497 | r = sd_bus_message_exit_container(m); | |
1498 | if (r < 0) | |
1499 | return r; | |
1500 | ||
1501 | return 0; | |
1502 | } | |
1503 | ||
a747e71c | 1504 | static int map_link_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
45817e92 | 1505 | return map_dns_servers_internal(bus, member, m, /* flags = */ 0, error, userdata); |
a747e71c YW |
1506 | } |
1507 | ||
1508 | static int map_link_dns_servers_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
45817e92 | 1509 | return map_dns_servers_internal(bus, member, m, READ_DNS_EXTENDED, error, userdata); |
a747e71c YW |
1510 | } |
1511 | ||
446c6415 YW |
1512 | static int map_link_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1513 | assert(m); | |
1514 | assert(userdata); | |
1515 | ||
90293a95 | 1516 | return read_dns_server_one(m, /* flags = */ 0, userdata); |
a747e71c YW |
1517 | } |
1518 | ||
1519 | static int map_link_current_dns_server_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
1520 | assert(m); | |
1521 | assert(userdata); | |
1522 | ||
90293a95 | 1523 | return read_dns_server_one(m, READ_DNS_EXTENDED, userdata); |
446c6415 YW |
1524 | } |
1525 | ||
c513bb6e YW |
1526 | static int read_domain_one(sd_bus_message *m, bool with_ifindex, char **ret) { |
1527 | _cleanup_free_ char *str = NULL; | |
1528 | int ifindex, route_only, r; | |
1529 | const char *domain; | |
1530 | ||
1531 | assert(m); | |
1532 | assert(ret); | |
1533 | ||
1534 | if (with_ifindex) | |
1535 | r = sd_bus_message_read(m, "(isb)", &ifindex, &domain, &route_only); | |
1536 | else | |
1537 | r = sd_bus_message_read(m, "(sb)", &domain, &route_only); | |
1538 | if (r <= 0) | |
1539 | return r; | |
1540 | ||
1541 | if (with_ifindex && ifindex != 0) { | |
1542 | /* only show the global ones here */ | |
1543 | *ret = NULL; | |
1544 | return 1; | |
1545 | } | |
1546 | ||
1547 | if (route_only) | |
b910cc72 | 1548 | str = strjoin("~", domain); |
c513bb6e YW |
1549 | else |
1550 | str = strdup(domain); | |
1551 | if (!str) | |
1552 | return -ENOMEM; | |
1553 | ||
1554 | *ret = TAKE_PTR(str); | |
c513bb6e YW |
1555 | return 1; |
1556 | } | |
1557 | ||
5c45b341 LP |
1558 | static int map_domains_internal( |
1559 | sd_bus *bus, | |
1560 | const char *member, | |
1561 | sd_bus_message *m, | |
1562 | bool with_ifindex, | |
1563 | sd_bus_error *error, | |
1564 | void *userdata) { | |
1565 | ||
99534007 | 1566 | char ***l = ASSERT_PTR(userdata); |
be371fe0 LP |
1567 | int r; |
1568 | ||
1569 | assert(bus); | |
1570 | assert(member); | |
1571 | assert(m); | |
be371fe0 | 1572 | |
5c45b341 | 1573 | r = sd_bus_message_enter_container(m, 'a', with_ifindex ? "(isb)" : "(sb)"); |
be371fe0 LP |
1574 | if (r < 0) |
1575 | return r; | |
1576 | ||
1577 | for (;;) { | |
6abdec98 | 1578 | _cleanup_free_ char *pretty = NULL; |
be371fe0 | 1579 | |
5c45b341 | 1580 | r = read_domain_one(m, with_ifindex, &pretty); |
be371fe0 LP |
1581 | if (r < 0) |
1582 | return r; | |
1583 | if (r == 0) | |
1584 | break; | |
1585 | ||
c513bb6e YW |
1586 | if (isempty(pretty)) |
1587 | continue; | |
be371fe0 | 1588 | |
6abdec98 | 1589 | r = strv_consume(l, TAKE_PTR(pretty)); |
be371fe0 LP |
1590 | if (r < 0) |
1591 | return r; | |
1592 | } | |
1593 | ||
1594 | r = sd_bus_message_exit_container(m); | |
1595 | if (r < 0) | |
1596 | return r; | |
1597 | ||
1598 | return 0; | |
1599 | } | |
1600 | ||
5c45b341 LP |
1601 | static int map_link_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1602 | return map_domains_internal(bus, member, m, /* with_ifindex= */ false, error, userdata); | |
1603 | } | |
1604 | ||
45817e92 | 1605 | static int status_print_strv_full(int ifindex, const char *ifname, const char *delegate_id, char **p) { |
7c502303 ZJS |
1606 | const unsigned indent = strlen("Global: "); /* Use the same indentation everywhere to make things nice */ |
1607 | int pos1, pos2; | |
1608 | ||
1609 | if (ifname) | |
1610 | printf("%s%nLink %i (%s)%n%s:", ansi_highlight(), &pos1, ifindex, ifname, &pos2, ansi_normal()); | |
45817e92 LP |
1611 | else if (delegate_id) |
1612 | printf("%s%nDelegate %s%n%s:", ansi_highlight(), &pos1, delegate_id, &pos2, ansi_normal()); | |
7c502303 ZJS |
1613 | else |
1614 | printf("%s%nGlobal%n%s:", ansi_highlight(), &pos1, &pos2, ansi_normal()); | |
1615 | ||
1616 | size_t cols = columns(), position = pos2 - pos1 + 2; | |
a7a4c60a | 1617 | |
7c502303 ZJS |
1618 | STRV_FOREACH(i, p) { |
1619 | size_t our_len = utf8_console_width(*i); /* This returns -1 on invalid utf-8 (which shouldn't happen). | |
1620 | * If that happens, we'll just print one item per line. */ | |
a7a4c60a | 1621 | |
b0e3d799 | 1622 | if (position <= indent || size_add(size_add(position, 1), our_len) < cols) { |
7c502303 | 1623 | printf(" %s", *i); |
b0e3d799 | 1624 | position = size_add(size_add(position, 1), our_len); |
7c502303 | 1625 | } else { |
f996072f | 1626 | printf("\n%*s%s", (int) indent, "", *i); |
b0e3d799 | 1627 | position = size_add(our_len, indent); |
7c502303 ZJS |
1628 | } |
1629 | } | |
a7a4c60a YW |
1630 | |
1631 | printf("\n"); | |
1632 | ||
1633 | return 0; | |
1634 | } | |
1635 | ||
45817e92 LP |
1636 | static int status_print_strv_ifindex(int ifindex, const char *ifname, char **p) { |
1637 | return status_print_strv_full(ifindex, ifname, NULL, p); | |
1638 | } | |
1639 | ||
1640 | static int status_print_strv_delegate(const char *delegate_id, char **p) { | |
1641 | return status_print_strv_full(0, NULL, delegate_id, p); | |
1642 | } | |
1643 | ||
7c502303 | 1644 | static int status_print_strv_global(char **p) { |
45817e92 | 1645 | return status_print_strv_full(0, NULL, NULL, p); |
7c502303 ZJS |
1646 | } |
1647 | ||
80b8c3d7 | 1648 | typedef struct LinkInfo { |
906119c0 YW |
1649 | uint64_t scopes_mask; |
1650 | const char *llmnr; | |
1651 | const char *mdns; | |
1652 | const char *dns_over_tls; | |
1653 | const char *dnssec; | |
1654 | char *current_dns; | |
a747e71c | 1655 | char *current_dns_ex; |
906119c0 | 1656 | char **dns; |
a747e71c | 1657 | char **dns_ex; |
906119c0 YW |
1658 | char **domains; |
1659 | char **ntas; | |
1660 | bool dnssec_supported; | |
f2fd3cdb | 1661 | bool default_route; |
80b8c3d7 | 1662 | } LinkInfo; |
906119c0 | 1663 | |
80b8c3d7 ZJS |
1664 | typedef struct GlobalInfo { |
1665 | char *current_dns; | |
1666 | char *current_dns_ex; | |
1667 | char **dns; | |
1668 | char **dns_ex; | |
1669 | char **fallback_dns; | |
1670 | char **fallback_dns_ex; | |
1671 | char **domains; | |
1672 | char **ntas; | |
1673 | const char *llmnr; | |
1674 | const char *mdns; | |
1675 | const char *dns_over_tls; | |
1676 | const char *dnssec; | |
1677 | const char *resolv_conf_mode; | |
1678 | bool dnssec_supported; | |
1679 | } GlobalInfo; | |
1680 | ||
da854058 LP |
1681 | static void link_info_done(LinkInfo *p) { |
1682 | assert(p); | |
1683 | ||
80b8c3d7 ZJS |
1684 | free(p->current_dns); |
1685 | free(p->current_dns_ex); | |
1686 | strv_free(p->dns); | |
1687 | strv_free(p->dns_ex); | |
1688 | strv_free(p->domains); | |
1689 | strv_free(p->ntas); | |
1690 | } | |
1691 | ||
da854058 LP |
1692 | static void global_info_done(GlobalInfo *p) { |
1693 | assert(p); | |
1694 | ||
906119c0 | 1695 | free(p->current_dns); |
a747e71c | 1696 | free(p->current_dns_ex); |
906119c0 | 1697 | strv_free(p->dns); |
a747e71c | 1698 | strv_free(p->dns_ex); |
80b8c3d7 ZJS |
1699 | strv_free(p->fallback_dns); |
1700 | strv_free(p->fallback_dns_ex); | |
906119c0 YW |
1701 | strv_free(p->domains); |
1702 | strv_free(p->ntas); | |
1703 | } | |
be371fe0 | 1704 | |
37a50123 | 1705 | static int dump_list(Table *table, const char *field, char * const *l) { |
29e15e98 YW |
1706 | int r; |
1707 | ||
1708 | if (strv_isempty(l)) | |
1709 | return 0; | |
1710 | ||
1711 | r = table_add_many(table, | |
37a50123 | 1712 | TABLE_FIELD, field, |
f08a64c5 | 1713 | TABLE_STRV_WRAPPED, l); |
29e15e98 YW |
1714 | if (r < 0) |
1715 | return table_log_add_error(r); | |
1716 | ||
1717 | return 0; | |
1718 | } | |
1719 | ||
fe37e5a5 ZJS |
1720 | static int strv_extend_extended_bool(char ***strv, const char *name, const char *value) { |
1721 | int r; | |
1722 | ||
1723 | if (value) { | |
1724 | r = parse_boolean(value); | |
1725 | if (r >= 0) | |
1726 | return strv_extendf(strv, "%s%s", plus_minus(r), name); | |
1727 | } | |
1728 | ||
1729 | return strv_extendf(strv, "%s=%s", name, value ?: "???"); | |
1730 | } | |
1731 | ||
7d1e1afe | 1732 | static char** link_protocol_status(const LinkInfo *info) { |
fe37e5a5 ZJS |
1733 | _cleanup_strv_free_ char **s = NULL; |
1734 | ||
1735 | if (strv_extendf(&s, "%sDefaultRoute", plus_minus(info->default_route)) < 0) | |
1736 | return NULL; | |
1737 | ||
1738 | if (strv_extend_extended_bool(&s, "LLMNR", info->llmnr) < 0) | |
1739 | return NULL; | |
1740 | ||
1741 | if (strv_extend_extended_bool(&s, "mDNS", info->mdns) < 0) | |
1742 | return NULL; | |
1743 | ||
1744 | if (strv_extend_extended_bool(&s, "DNSOverTLS", info->dns_over_tls) < 0) | |
1745 | return NULL; | |
1746 | ||
1747 | if (strv_extendf(&s, "DNSSEC=%s/%s", | |
1748 | info->dnssec ?: "???", | |
1749 | info->dnssec_supported ? "supported" : "unsupported") < 0) | |
1750 | return NULL; | |
1751 | ||
7d1e1afe | 1752 | return TAKE_PTR(s); |
fe37e5a5 ZJS |
1753 | } |
1754 | ||
7d1e1afe | 1755 | static char** global_protocol_status(const GlobalInfo *info) { |
fe37e5a5 ZJS |
1756 | _cleanup_strv_free_ char **s = NULL; |
1757 | ||
1758 | if (strv_extend_extended_bool(&s, "LLMNR", info->llmnr) < 0) | |
1759 | return NULL; | |
1760 | ||
1761 | if (strv_extend_extended_bool(&s, "mDNS", info->mdns) < 0) | |
1762 | return NULL; | |
1763 | ||
1764 | if (strv_extend_extended_bool(&s, "DNSOverTLS", info->dns_over_tls) < 0) | |
1765 | return NULL; | |
1766 | ||
1767 | if (strv_extendf(&s, "DNSSEC=%s/%s", | |
1768 | info->dnssec ?: "???", | |
1769 | info->dnssec_supported ? "supported" : "unsupported") < 0) | |
1770 | return NULL; | |
1771 | ||
7d1e1afe | 1772 | return TAKE_PTR(s); |
fe37e5a5 ZJS |
1773 | } |
1774 | ||
906119c0 | 1775 | static int status_ifindex(sd_bus *bus, int ifindex, const char *name, StatusMode mode, bool *empty_line) { |
be371fe0 | 1776 | static const struct bus_properties_map property_map[] = { |
80b8c3d7 ZJS |
1777 | { "ScopesMask", "t", NULL, offsetof(LinkInfo, scopes_mask) }, |
1778 | { "DNS", "a(iay)", map_link_dns_servers, offsetof(LinkInfo, dns) }, | |
1779 | { "DNSEx", "a(iayqs)", map_link_dns_servers_ex, offsetof(LinkInfo, dns_ex) }, | |
1780 | { "CurrentDNSServer", "(iay)", map_link_current_dns_server, offsetof(LinkInfo, current_dns) }, | |
1781 | { "CurrentDNSServerEx", "(iayqs)", map_link_current_dns_server_ex, offsetof(LinkInfo, current_dns_ex) }, | |
1782 | { "Domains", "a(sb)", map_link_domains, offsetof(LinkInfo, domains) }, | |
1783 | { "DefaultRoute", "b", NULL, offsetof(LinkInfo, default_route) }, | |
1784 | { "LLMNR", "s", NULL, offsetof(LinkInfo, llmnr) }, | |
1785 | { "MulticastDNS", "s", NULL, offsetof(LinkInfo, mdns) }, | |
1786 | { "DNSOverTLS", "s", NULL, offsetof(LinkInfo, dns_over_tls) }, | |
1787 | { "DNSSEC", "s", NULL, offsetof(LinkInfo, dnssec) }, | |
af781878 | 1788 | { "DNSSECNegativeTrustAnchors", "as", bus_map_strv_sort, offsetof(LinkInfo, ntas) }, |
80b8c3d7 | 1789 | { "DNSSECSupported", "b", NULL, offsetof(LinkInfo, dnssec_supported) }, |
be371fe0 LP |
1790 | {} |
1791 | }; | |
f9e0eefc | 1792 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f37f8a61 | 1793 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
da854058 | 1794 | _cleanup_(link_info_done) LinkInfo link_info = {}; |
29e15e98 | 1795 | _cleanup_(table_unrefp) Table *table = NULL; |
957d9df3 | 1796 | _cleanup_free_ char *p = NULL; |
01afd0f7 | 1797 | char ifi[DECIMAL_STR_MAX(int)], ifname[IF_NAMESIZE]; |
be371fe0 LP |
1798 | int r; |
1799 | ||
1800 | assert(bus); | |
1801 | assert(ifindex > 0); | |
be371fe0 LP |
1802 | |
1803 | if (!name) { | |
01afd0f7 YW |
1804 | r = format_ifname(ifindex, ifname); |
1805 | if (r < 0) | |
1806 | return log_error_errno(r, "Failed to resolve interface name for %i: %m", ifindex); | |
be371fe0 LP |
1807 | |
1808 | name = ifname; | |
1809 | } | |
1810 | ||
957d9df3 | 1811 | xsprintf(ifi, "%i", ifindex); |
be371fe0 LP |
1812 | r = sd_bus_path_encode("/org/freedesktop/resolve1/link", ifi, &p); |
1813 | if (r < 0) | |
1814 | return log_oom(); | |
1815 | ||
1816 | r = bus_map_all_properties(bus, | |
1817 | "org.freedesktop.resolve1", | |
1818 | p, | |
1819 | property_map, | |
a7e4861c | 1820 | BUS_MAP_BOOLEAN_AS_BOOL, |
f9e0eefc | 1821 | &error, |
f37f8a61 | 1822 | &m, |
be371fe0 | 1823 | &link_info); |
906119c0 YW |
1824 | if (r < 0) |
1825 | return log_error_errno(r, "Failed to get link data for %i: %s", ifindex, bus_error_message(&error, r)); | |
be371fe0 | 1826 | |
384c2c32 | 1827 | pager_open(arg_pager_flags); |
be371fe0 | 1828 | |
2b735c7d LP |
1829 | switch (mode) { |
1830 | ||
1831 | case STATUS_DNS: | |
a747e71c | 1832 | return status_print_strv_ifindex(ifindex, name, link_info.dns_ex ?: link_info.dns); |
a7a4c60a | 1833 | |
2b735c7d | 1834 | case STATUS_DOMAIN: |
906119c0 | 1835 | return status_print_strv_ifindex(ifindex, name, link_info.domains); |
a7a4c60a | 1836 | |
2b735c7d | 1837 | case STATUS_NTA: |
906119c0 | 1838 | return status_print_strv_ifindex(ifindex, name, link_info.ntas); |
a7a4c60a | 1839 | |
2b735c7d | 1840 | case STATUS_DEFAULT_ROUTE: |
f2fd3cdb LP |
1841 | printf("%sLink %i (%s)%s: %s\n", |
1842 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1843 | yes_no(link_info.default_route)); | |
1844 | ||
1845 | return 0; | |
f2fd3cdb | 1846 | |
2b735c7d | 1847 | case STATUS_LLMNR: |
a7a4c60a YW |
1848 | printf("%sLink %i (%s)%s: %s\n", |
1849 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1850 | strna(link_info.llmnr)); | |
1851 | ||
906119c0 | 1852 | return 0; |
a7a4c60a | 1853 | |
2b735c7d | 1854 | case STATUS_MDNS: |
a7a4c60a YW |
1855 | printf("%sLink %i (%s)%s: %s\n", |
1856 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1857 | strna(link_info.mdns)); | |
1858 | ||
906119c0 | 1859 | return 0; |
a7a4c60a | 1860 | |
2b735c7d | 1861 | case STATUS_PRIVATE: |
d050561a IT |
1862 | printf("%sLink %i (%s)%s: %s\n", |
1863 | ansi_highlight(), ifindex, name, ansi_normal(), | |
c9299be2 | 1864 | strna(link_info.dns_over_tls)); |
d050561a | 1865 | |
906119c0 | 1866 | return 0; |
d050561a | 1867 | |
2b735c7d | 1868 | case STATUS_DNSSEC: |
a7a4c60a YW |
1869 | printf("%sLink %i (%s)%s: %s\n", |
1870 | ansi_highlight(), ifindex, name, ansi_normal(), | |
1871 | strna(link_info.dnssec)); | |
1872 | ||
906119c0 | 1873 | return 0; |
2b735c7d LP |
1874 | |
1875 | case STATUS_ALL: | |
1876 | break; | |
1877 | ||
1878 | default: | |
1879 | return 0; | |
a7a4c60a YW |
1880 | } |
1881 | ||
1882 | if (empty_line && *empty_line) | |
be371fe0 LP |
1883 | fputc('\n', stdout); |
1884 | ||
1885 | printf("%sLink %i (%s)%s\n", | |
1886 | ansi_highlight(), ifindex, name, ansi_normal()); | |
1887 | ||
37a50123 | 1888 | table = table_new_vertical(); |
29e15e98 YW |
1889 | if (!table) |
1890 | return log_oom(); | |
1891 | ||
29e15e98 | 1892 | r = table_add_many(table, |
37a50123 LP |
1893 | TABLE_FIELD, "Current Scopes", |
1894 | TABLE_SET_MINIMUM_WIDTH, 19); | |
29e15e98 YW |
1895 | if (r < 0) |
1896 | return table_log_add_error(r); | |
1897 | ||
be371fe0 | 1898 | if (link_info.scopes_mask == 0) |
29e15e98 YW |
1899 | r = table_add_cell(table, NULL, TABLE_STRING, "none"); |
1900 | else { | |
1901 | _cleanup_free_ char *buf = NULL; | |
1902 | size_t len; | |
1903 | ||
1904 | if (asprintf(&buf, "%s%s%s%s%s", | |
1905 | link_info.scopes_mask & SD_RESOLVED_DNS ? "DNS " : "", | |
1906 | link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV4 ? "LLMNR/IPv4 " : "", | |
1907 | link_info.scopes_mask & SD_RESOLVED_LLMNR_IPV6 ? "LLMNR/IPv6 " : "", | |
1908 | link_info.scopes_mask & SD_RESOLVED_MDNS_IPV4 ? "mDNS/IPv4 " : "", | |
1909 | link_info.scopes_mask & SD_RESOLVED_MDNS_IPV6 ? "mDNS/IPv6 " : "") < 0) | |
1910 | return log_oom(); | |
be371fe0 | 1911 | |
29e15e98 YW |
1912 | len = strlen(buf); |
1913 | assert(len > 0); | |
1914 | buf[len - 1] = '\0'; | |
be371fe0 | 1915 | |
29e15e98 YW |
1916 | r = table_add_cell(table, NULL, TABLE_STRING, buf); |
1917 | } | |
1918 | if (r < 0) | |
1919 | return table_log_add_error(r); | |
1920 | ||
7d1e1afe | 1921 | _cleanup_strv_free_ char **pstatus = link_protocol_status(&link_info); |
fe37e5a5 ZJS |
1922 | if (!pstatus) |
1923 | return log_oom(); | |
1924 | ||
29e15e98 | 1925 | r = table_add_many(table, |
37a50123 | 1926 | TABLE_FIELD, "Protocols", |
7d1e1afe | 1927 | TABLE_STRV_WRAPPED, pstatus); |
29e15e98 YW |
1928 | if (r < 0) |
1929 | return table_log_add_error(r); | |
1930 | ||
1931 | if (link_info.current_dns) { | |
1932 | r = table_add_many(table, | |
37a50123 | 1933 | TABLE_FIELD, "Current DNS Server", |
a747e71c | 1934 | TABLE_STRING, link_info.current_dns_ex ?: link_info.current_dns); |
29e15e98 YW |
1935 | if (r < 0) |
1936 | return table_log_add_error(r); | |
be371fe0 LP |
1937 | } |
1938 | ||
37a50123 | 1939 | r = dump_list(table, "DNS Servers", link_info.dns_ex ?: link_info.dns); |
29e15e98 YW |
1940 | if (r < 0) |
1941 | return r; | |
1942 | ||
37a50123 | 1943 | r = dump_list(table, "DNS Domain", link_info.domains); |
29e15e98 YW |
1944 | if (r < 0) |
1945 | return r; | |
1946 | ||
e5868783 LP |
1947 | r = table_add_many(table, |
1948 | TABLE_FIELD, "Default Route", | |
1949 | TABLE_BOOLEAN, link_info.default_route); | |
1950 | if (r < 0) | |
1951 | return table_log_add_error(r); | |
1952 | ||
29e15e98 YW |
1953 | r = table_print(table, NULL); |
1954 | if (r < 0) | |
4b6607d9 | 1955 | return table_log_print_error(r); |
29e15e98 | 1956 | |
a7a4c60a YW |
1957 | if (empty_line) |
1958 | *empty_line = true; | |
be371fe0 | 1959 | |
906119c0 | 1960 | return 0; |
be371fe0 LP |
1961 | } |
1962 | ||
5707fb12 LP |
1963 | static int map_global_dns_servers_internal( |
1964 | sd_bus *bus, | |
1965 | const char *member, | |
1966 | sd_bus_message *m, | |
1967 | sd_bus_error *error, | |
1968 | void *userdata, | |
90293a95 | 1969 | ReadDNSFlag flags) { |
5707fb12 | 1970 | |
99534007 | 1971 | char ***l = ASSERT_PTR(userdata); |
be371fe0 LP |
1972 | int r; |
1973 | ||
1974 | assert(bus); | |
1975 | assert(member); | |
1976 | assert(m); | |
be371fe0 | 1977 | |
90293a95 | 1978 | r = sd_bus_message_enter_container(m, 'a', FLAGS_SET(flags, READ_DNS_EXTENDED) ? "(iiayqs)" : "(iiay)"); |
be371fe0 LP |
1979 | if (r < 0) |
1980 | return r; | |
1981 | ||
1982 | for (;;) { | |
6abdec98 | 1983 | _cleanup_free_ char *pretty = NULL; |
be371fe0 | 1984 | |
90293a95 | 1985 | r = read_dns_server_one(m, flags, &pretty); |
be371fe0 LP |
1986 | if (r < 0) |
1987 | return r; | |
1988 | if (r == 0) | |
1989 | break; | |
1990 | ||
58f48a56 | 1991 | if (isempty(pretty)) |
be371fe0 LP |
1992 | continue; |
1993 | ||
6abdec98 | 1994 | r = strv_consume(l, TAKE_PTR(pretty)); |
be371fe0 LP |
1995 | if (r < 0) |
1996 | return r; | |
1997 | } | |
1998 | ||
1999 | r = sd_bus_message_exit_container(m); | |
2000 | if (r < 0) | |
2001 | return r; | |
2002 | ||
2003 | return 0; | |
2004 | } | |
2005 | ||
a747e71c | 2006 | static int map_global_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
90293a95 | 2007 | return map_global_dns_servers_internal(bus, member, m, error, userdata, READ_DNS_WITH_IFINDEX | READ_DNS_ONLY_GLOBAL); |
a747e71c YW |
2008 | } |
2009 | ||
2010 | static int map_global_dns_servers_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
90293a95 | 2011 | return map_global_dns_servers_internal(bus, member, m, error, userdata, READ_DNS_WITH_IFINDEX | READ_DNS_ONLY_GLOBAL | READ_DNS_EXTENDED); |
a747e71c YW |
2012 | } |
2013 | ||
446c6415 | 2014 | static int map_global_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
90293a95 | 2015 | return read_dns_server_one(m, READ_DNS_WITH_IFINDEX | READ_DNS_ONLY_GLOBAL, userdata); |
a747e71c YW |
2016 | } |
2017 | ||
2018 | static int map_global_current_dns_server_ex(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
90293a95 | 2019 | return read_dns_server_one(m, READ_DNS_WITH_IFINDEX | READ_DNS_ONLY_GLOBAL | READ_DNS_EXTENDED, userdata); |
446c6415 YW |
2020 | } |
2021 | ||
be371fe0 | 2022 | static int map_global_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
5c45b341 | 2023 | return map_domains_internal(bus, member, m, /* with_ifindex= */ true, error, userdata); |
a7a4c60a YW |
2024 | } |
2025 | ||
906119c0 | 2026 | static int status_global(sd_bus *bus, StatusMode mode, bool *empty_line) { |
be371fe0 | 2027 | static const struct bus_properties_map property_map[] = { |
80b8c3d7 ZJS |
2028 | { "DNS", "a(iiay)", map_global_dns_servers, offsetof(GlobalInfo, dns) }, |
2029 | { "DNSEx", "a(iiayqs)", map_global_dns_servers_ex, offsetof(GlobalInfo, dns_ex) }, | |
2030 | { "FallbackDNS", "a(iiay)", map_global_dns_servers, offsetof(GlobalInfo, fallback_dns) }, | |
2031 | { "FallbackDNSEx", "a(iiayqs)", map_global_dns_servers_ex, offsetof(GlobalInfo, fallback_dns_ex) }, | |
2032 | { "CurrentDNSServer", "(iiay)", map_global_current_dns_server, offsetof(GlobalInfo, current_dns) }, | |
2033 | { "CurrentDNSServerEx", "(iiayqs)", map_global_current_dns_server_ex, offsetof(GlobalInfo, current_dns_ex) }, | |
2034 | { "Domains", "a(isb)", map_global_domains, offsetof(GlobalInfo, domains) }, | |
af781878 | 2035 | { "DNSSECNegativeTrustAnchors", "as", bus_map_strv_sort, offsetof(GlobalInfo, ntas) }, |
80b8c3d7 ZJS |
2036 | { "LLMNR", "s", NULL, offsetof(GlobalInfo, llmnr) }, |
2037 | { "MulticastDNS", "s", NULL, offsetof(GlobalInfo, mdns) }, | |
2038 | { "DNSOverTLS", "s", NULL, offsetof(GlobalInfo, dns_over_tls) }, | |
2039 | { "DNSSEC", "s", NULL, offsetof(GlobalInfo, dnssec) }, | |
2040 | { "DNSSECSupported", "b", NULL, offsetof(GlobalInfo, dnssec_supported) }, | |
2041 | { "ResolvConfMode", "s", NULL, offsetof(GlobalInfo, resolv_conf_mode) }, | |
be371fe0 LP |
2042 | {} |
2043 | }; | |
f9e0eefc | 2044 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f37f8a61 | 2045 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
da854058 | 2046 | _cleanup_(global_info_done) GlobalInfo global_info = {}; |
29e15e98 | 2047 | _cleanup_(table_unrefp) Table *table = NULL; |
be371fe0 LP |
2048 | int r; |
2049 | ||
2050 | assert(bus); | |
2051 | assert(empty_line); | |
2052 | ||
2053 | r = bus_map_all_properties(bus, | |
2054 | "org.freedesktop.resolve1", | |
2055 | "/org/freedesktop/resolve1", | |
2056 | property_map, | |
a7e4861c | 2057 | BUS_MAP_BOOLEAN_AS_BOOL, |
f9e0eefc | 2058 | &error, |
f37f8a61 | 2059 | &m, |
be371fe0 | 2060 | &global_info); |
906119c0 YW |
2061 | if (r < 0) |
2062 | return log_error_errno(r, "Failed to get global data: %s", bus_error_message(&error, r)); | |
be371fe0 | 2063 | |
384c2c32 | 2064 | pager_open(arg_pager_flags); |
be371fe0 | 2065 | |
2b735c7d LP |
2066 | switch (mode) { |
2067 | ||
2068 | case STATUS_DNS: | |
a747e71c | 2069 | return status_print_strv_global(global_info.dns_ex ?: global_info.dns); |
a7a4c60a | 2070 | |
2b735c7d | 2071 | case STATUS_DOMAIN: |
906119c0 | 2072 | return status_print_strv_global(global_info.domains); |
a7a4c60a | 2073 | |
2b735c7d | 2074 | case STATUS_NTA: |
906119c0 | 2075 | return status_print_strv_global(global_info.ntas); |
a7a4c60a | 2076 | |
2b735c7d | 2077 | case STATUS_LLMNR: |
a7a4c60a YW |
2078 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), |
2079 | strna(global_info.llmnr)); | |
2080 | ||
906119c0 | 2081 | return 0; |
a7a4c60a | 2082 | |
2b735c7d | 2083 | case STATUS_MDNS: |
a7a4c60a YW |
2084 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), |
2085 | strna(global_info.mdns)); | |
2086 | ||
906119c0 | 2087 | return 0; |
a7a4c60a | 2088 | |
2b735c7d | 2089 | case STATUS_PRIVATE: |
d050561a | 2090 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), |
c9299be2 | 2091 | strna(global_info.dns_over_tls)); |
d050561a | 2092 | |
906119c0 | 2093 | return 0; |
d050561a | 2094 | |
2b735c7d | 2095 | case STATUS_DNSSEC: |
a7a4c60a YW |
2096 | printf("%sGlobal%s: %s\n", ansi_highlight(), ansi_normal(), |
2097 | strna(global_info.dnssec)); | |
2098 | ||
906119c0 | 2099 | return 0; |
2b735c7d LP |
2100 | |
2101 | case STATUS_ALL: | |
2102 | break; | |
2103 | ||
2104 | default: | |
2105 | return 0; | |
a7a4c60a YW |
2106 | } |
2107 | ||
be371fe0 | 2108 | printf("%sGlobal%s\n", ansi_highlight(), ansi_normal()); |
11d6e9e9 | 2109 | |
37a50123 | 2110 | table = table_new_vertical(); |
29e15e98 YW |
2111 | if (!table) |
2112 | return log_oom(); | |
be371fe0 | 2113 | |
7d1e1afe | 2114 | _cleanup_strv_free_ char **pstatus = global_protocol_status(&global_info); |
fe37e5a5 ZJS |
2115 | if (!pstatus) |
2116 | return log_oom(); | |
2117 | ||
29e15e98 | 2118 | r = table_add_many(table, |
37a50123 LP |
2119 | TABLE_FIELD, "Protocols", |
2120 | TABLE_SET_MINIMUM_WIDTH, 19, | |
7d1e1afe | 2121 | TABLE_STRV_WRAPPED, pstatus); |
29e15e98 YW |
2122 | if (r < 0) |
2123 | return table_log_add_error(r); | |
2124 | ||
147a5046 LP |
2125 | if (global_info.resolv_conf_mode) { |
2126 | r = table_add_many(table, | |
37a50123 | 2127 | TABLE_FIELD, "resolv.conf mode", |
147a5046 LP |
2128 | TABLE_STRING, global_info.resolv_conf_mode); |
2129 | if (r < 0) | |
2130 | return table_log_add_error(r); | |
2131 | } | |
2132 | ||
29e15e98 YW |
2133 | if (global_info.current_dns) { |
2134 | r = table_add_many(table, | |
37a50123 | 2135 | TABLE_FIELD, "Current DNS Server", |
a747e71c | 2136 | TABLE_STRING, global_info.current_dns_ex ?: global_info.current_dns); |
29e15e98 YW |
2137 | if (r < 0) |
2138 | return table_log_add_error(r); | |
4b320ac5 YW |
2139 | } |
2140 | ||
ef503f1c | 2141 | r = dump_list(table, "DNS Servers", global_info.dns_ex ?: global_info.dns); |
29e15e98 YW |
2142 | if (r < 0) |
2143 | return r; | |
2144 | ||
ef503f1c | 2145 | r = dump_list(table, "Fallback DNS Servers", global_info.fallback_dns_ex ?: global_info.fallback_dns); |
29e15e98 YW |
2146 | if (r < 0) |
2147 | return r; | |
2148 | ||
ef503f1c | 2149 | r = dump_list(table, "DNS Domain", global_info.domains); |
29e15e98 YW |
2150 | if (r < 0) |
2151 | return r; | |
be371fe0 | 2152 | |
29e15e98 YW |
2153 | r = table_print(table, NULL); |
2154 | if (r < 0) | |
4b6607d9 | 2155 | return table_log_print_error(r); |
be371fe0 LP |
2156 | |
2157 | *empty_line = true; | |
2158 | ||
906119c0 | 2159 | return 0; |
be371fe0 LP |
2160 | } |
2161 | ||
45817e92 | 2162 | static int status_links(sd_bus *bus, StatusMode mode, bool *empty_line) { |
be371fe0 LP |
2163 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
2164 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
f75ecb9f | 2165 | int ret = 0, r; |
be371fe0 LP |
2166 | |
2167 | assert(bus); | |
2168 | ||
be371fe0 LP |
2169 | r = sd_netlink_open(&rtnl); |
2170 | if (r < 0) | |
2171 | return log_error_errno(r, "Failed to connect to netlink: %m"); | |
2172 | ||
2173 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0); | |
2174 | if (r < 0) | |
2175 | return rtnl_log_create_error(r); | |
2176 | ||
24c0f385 | 2177 | r = sd_netlink_message_set_request_dump(req, true); |
be371fe0 LP |
2178 | if (r < 0) |
2179 | return rtnl_log_create_error(r); | |
2180 | ||
2181 | r = sd_netlink_call(rtnl, req, 0, &reply); | |
2182 | if (r < 0) | |
2183 | return log_error_errno(r, "Failed to enumerate links: %m"); | |
2184 | ||
eb107675 | 2185 | _cleanup_free_ InterfaceInfo *infos = NULL; |
319a4f4b | 2186 | size_t n_infos = 0; |
eb107675 | 2187 | |
c9d243cd | 2188 | for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) { |
be371fe0 | 2189 | const char *name; |
eb107675 | 2190 | int ifindex; |
be371fe0 LP |
2191 | uint16_t type; |
2192 | ||
eb107675 ZJS |
2193 | r = sd_netlink_message_get_type(i, &type); |
2194 | if (r < 0) | |
2195 | return rtnl_log_parse_error(r); | |
be371fe0 LP |
2196 | |
2197 | if (type != RTM_NEWLINK) | |
2198 | continue; | |
2199 | ||
eb107675 ZJS |
2200 | r = sd_rtnl_message_link_get_ifindex(i, &ifindex); |
2201 | if (r < 0) | |
2202 | return rtnl_log_parse_error(r); | |
be371fe0 LP |
2203 | |
2204 | if (ifindex == LOOPBACK_IFINDEX) | |
2205 | continue; | |
2206 | ||
eb107675 ZJS |
2207 | r = sd_netlink_message_read_string(i, IFLA_IFNAME, &name); |
2208 | if (r < 0) | |
2209 | return rtnl_log_parse_error(r); | |
be371fe0 | 2210 | |
319a4f4b | 2211 | if (!GREEDY_REALLOC(infos, n_infos + 1)) |
eb107675 ZJS |
2212 | return log_oom(); |
2213 | ||
2214 | infos[n_infos++] = (InterfaceInfo) { ifindex, name }; | |
2215 | } | |
2216 | ||
2217 | typesafe_qsort(infos, n_infos, interface_info_compare); | |
2218 | ||
f75ecb9f | 2219 | FOREACH_ARRAY(info, infos, n_infos) |
45817e92 LP |
2220 | RET_GATHER(ret, status_ifindex(bus, info->index, info->name, mode, empty_line)); |
2221 | ||
2222 | return ret; | |
2223 | } | |
2224 | ||
2225 | typedef struct DelegateInfo { | |
2226 | char *current_dns; | |
2227 | char **dns; | |
2228 | char **domains; | |
2229 | bool default_route; | |
2230 | } DelegateInfo; | |
2231 | ||
2232 | static void delegate_info_done(DelegateInfo *p) { | |
2233 | assert(p); | |
2234 | ||
2235 | free(p->current_dns); | |
2236 | strv_free(p->dns); | |
2237 | strv_free(p->domains); | |
2238 | } | |
2239 | ||
2240 | static int map_delegate_dns_servers(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2241 | return map_dns_servers_internal(bus, member, m, READ_DNS_WITH_IFINDEX|READ_DNS_EXTENDED, error, userdata); | |
2242 | } | |
2243 | ||
2244 | static int map_delegate_current_dns_server(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2245 | return read_dns_server_one(m, READ_DNS_WITH_IFINDEX|READ_DNS_EXTENDED, userdata); | |
2246 | } | |
2247 | ||
2248 | static int map_delegate_domains(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { | |
2249 | return map_domains_internal(bus, member, m, /* with_ifindex= */ false, error, userdata); | |
2250 | } | |
2251 | ||
2252 | static int status_delegate_one(sd_bus *bus, const char *id, StatusMode mode, bool *empty_line) { | |
2253 | ||
2254 | static const struct bus_properties_map property_map[] = { | |
2255 | { "DNS", "a(iiayqs)", map_delegate_dns_servers, offsetof(DelegateInfo, dns) }, | |
2256 | { "CurrentDNSServer", "(iiayqs)", map_delegate_current_dns_server, offsetof(DelegateInfo, current_dns) }, | |
2257 | { "Domains", "a(sb)", map_delegate_domains, offsetof(DelegateInfo, domains) }, | |
2258 | { "DefaultRoute", "b", NULL, offsetof(DelegateInfo, default_route) }, | |
2259 | {} | |
2260 | }; | |
2261 | ||
2262 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2263 | _cleanup_(delegate_info_done) DelegateInfo delegate_info = {}; | |
2264 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
2265 | _cleanup_free_ char *p = NULL; | |
2266 | int r; | |
2267 | ||
2268 | assert(bus); | |
2269 | assert(id); | |
2270 | ||
2271 | r = sd_bus_path_encode("/org/freedesktop/resolve1/dns_delegate", id, &p); | |
2272 | if (r < 0) | |
2273 | return log_oom(); | |
2274 | ||
2275 | r = bus_map_all_properties( | |
2276 | bus, | |
2277 | "org.freedesktop.resolve1", | |
2278 | p, | |
2279 | property_map, | |
2280 | BUS_MAP_BOOLEAN_AS_BOOL, | |
2281 | &error, | |
2282 | &m, | |
2283 | &delegate_info); | |
2284 | if (r < 0) | |
2285 | return log_error_errno(r, "Failed to get delegate data for %s: %s", id, bus_error_message(&error, r)); | |
2286 | ||
2287 | pager_open(arg_pager_flags); | |
2288 | ||
2289 | switch (mode) { | |
2290 | ||
2291 | case STATUS_DNS: | |
2292 | return status_print_strv_delegate(id, delegate_info.dns); | |
2293 | ||
2294 | case STATUS_DOMAIN: | |
2295 | return status_print_strv_delegate(id, delegate_info.domains); | |
2296 | ||
2297 | case STATUS_DEFAULT_ROUTE: | |
2298 | printf("%sDelegate %s%s: %s\n", | |
2299 | ansi_highlight(), id, ansi_normal(), | |
2300 | yes_no(delegate_info.default_route)); | |
2301 | ||
2302 | return 0; | |
2303 | ||
2304 | case STATUS_ALL: | |
2305 | break; | |
2306 | ||
2307 | default: | |
2308 | return 0; | |
2309 | } | |
2310 | ||
2311 | if (empty_line && *empty_line) | |
2312 | fputc('\n', stdout); | |
2313 | ||
2314 | printf("%sDelegate %s%s\n", | |
2315 | ansi_highlight(), id, ansi_normal()); | |
2316 | ||
2317 | _cleanup_(table_unrefp) Table *table = table_new_vertical(); | |
2318 | if (!table) | |
2319 | return log_oom(); | |
2320 | ||
2321 | if (delegate_info.current_dns) { | |
2322 | r = table_add_many(table, | |
2323 | TABLE_FIELD, "Current DNS Server", | |
2324 | TABLE_STRING, delegate_info.current_dns); | |
2325 | if (r < 0) | |
2326 | return table_log_add_error(r); | |
2327 | } | |
2328 | ||
2329 | r = dump_list(table, "DNS Servers", delegate_info.dns); | |
2330 | if (r < 0) | |
2331 | return r; | |
2332 | ||
2333 | r = dump_list(table, "DNS Domain", delegate_info.domains); | |
2334 | if (r < 0) | |
2335 | return r; | |
2336 | ||
2337 | r = table_add_many(table, | |
2338 | TABLE_FIELD, "Default Route", | |
2339 | TABLE_SET_MINIMUM_WIDTH, 19, | |
2340 | TABLE_BOOLEAN, delegate_info.default_route); | |
134fde86 AAF |
2341 | if (r < 0) |
2342 | return table_log_add_error(r); | |
45817e92 LP |
2343 | |
2344 | r = table_print(table, NULL); | |
2345 | if (r < 0) | |
2346 | return table_log_print_error(r); | |
2347 | ||
2348 | if (empty_line) | |
2349 | *empty_line = true; | |
2350 | ||
2351 | return 0; | |
2352 | } | |
2353 | ||
2354 | static int status_delegates(sd_bus *bus, StatusMode mode, bool *empty_line) { | |
2355 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
2356 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2357 | int r, ret = 0; | |
2358 | ||
2359 | assert(bus); | |
2360 | ||
2361 | r = bus_call_method(bus, bus_resolve_mgr, "ListDelegates", &error, &reply, NULL); | |
2362 | if (r < 0) { | |
2363 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) { | |
2364 | log_debug("Delegates not supported, skipping."); | |
2365 | return 0; | |
2366 | } | |
2367 | return log_error_errno(r, "Failed to list delegates: %s", bus_error_message(&error, r)); | |
2368 | } | |
2369 | ||
2370 | r = sd_bus_message_enter_container(reply, 'a', "(so)"); | |
2371 | if (r < 0) | |
2372 | return bus_log_parse_error(r); | |
2373 | ||
2374 | _cleanup_strv_free_ char **l = NULL; | |
2375 | for (;;) { | |
2376 | const char *id; | |
2377 | ||
2378 | r = sd_bus_message_read(reply, "(so)", &id, NULL); | |
2379 | if (r < 0) | |
2380 | return bus_log_parse_error(r); | |
2381 | if (r == 0) | |
2382 | break; | |
2383 | ||
2384 | if (strv_extend(&l, id) < 0) | |
2385 | return log_oom(); | |
2386 | } | |
2387 | ||
2388 | r = sd_bus_message_exit_container(reply); | |
2389 | if (r < 0) | |
2390 | return bus_log_parse_error(r); | |
2391 | ||
2392 | strv_sort(l); | |
2393 | ||
2394 | STRV_FOREACH(i, l) | |
2395 | RET_GATHER(ret, status_delegate_one(bus, *i, mode, empty_line)); | |
be371fe0 | 2396 | |
f75ecb9f | 2397 | return ret; |
be371fe0 LP |
2398 | } |
2399 | ||
45817e92 LP |
2400 | static int status_all(sd_bus *bus, StatusMode mode) { |
2401 | bool empty_line = false; | |
2402 | int r; | |
2403 | ||
2404 | assert(bus); | |
2405 | ||
2406 | r = status_global(bus, mode, &empty_line); | |
2407 | if (r < 0) | |
2408 | return r; | |
2409 | ||
2410 | r = status_links(bus, mode, &empty_line); | |
2411 | if (r < 0) | |
2412 | return r; | |
2413 | ||
2414 | r = status_delegates(bus, mode, &empty_line); | |
2415 | if (r < 0) | |
2416 | return r; | |
2417 | ||
2418 | return 0; | |
2419 | } | |
2420 | ||
a7a4c60a | 2421 | static int verb_status(int argc, char **argv, void *userdata) { |
614a6770 | 2422 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
957d9df3 | 2423 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
f75ecb9f | 2424 | bool empty_line = false; |
614a6770 YW |
2425 | int r, ret = 0; |
2426 | ||
2427 | r = acquire_bus(&bus); | |
2428 | if (r < 0) | |
2429 | return r; | |
14965b94 | 2430 | |
f75ecb9f YW |
2431 | if (argc <= 1) |
2432 | return status_all(bus, STATUS_ALL); | |
14965b94 | 2433 | |
f75ecb9f YW |
2434 | STRV_FOREACH(ifname, strv_skip(argv, 1)) { |
2435 | int ifindex; | |
14965b94 | 2436 | |
f75ecb9f YW |
2437 | ifindex = rtnl_resolve_interface(&rtnl, *ifname); |
2438 | if (ifindex < 0) { | |
2439 | log_warning_errno(ifindex, "Failed to resolve interface \"%s\", ignoring: %m", *ifname); | |
2440 | continue; | |
a7a4c60a | 2441 | } |
14965b94 | 2442 | |
f75ecb9f YW |
2443 | RET_GATHER(ret, status_ifindex(bus, ifindex, NULL, STATUS_ALL, &empty_line)); |
2444 | } | |
2445 | ||
2446 | return ret; | |
a7a4c60a | 2447 | } |
14965b94 | 2448 | |
b1881e83 | 2449 | static int call_dns(sd_bus *bus, char **dns, const BusLocator *locator, sd_bus_error *error, bool extended) { |
a7a4c60a | 2450 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL; |
a661dc36 | 2451 | int r; |
14965b94 | 2452 | |
1d7fa677 YW |
2453 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
2454 | ||
b1881e83 | 2455 | r = bus_message_new_method_call(bus, &req, locator, extended ? "SetLinkDNSEx" : "SetLinkDNS"); |
a7a4c60a YW |
2456 | if (r < 0) |
2457 | return bus_log_create_error(r); | |
14965b94 | 2458 | |
a661dc36 | 2459 | r = sd_bus_message_append(req, "i", arg_ifindex); |
a7a4c60a YW |
2460 | if (r < 0) |
2461 | return bus_log_create_error(r); | |
14965b94 | 2462 | |
b1881e83 | 2463 | r = sd_bus_message_open_container(req, 'a', extended ? "(iayqs)" : "(iay)"); |
a7a4c60a YW |
2464 | if (r < 0) |
2465 | return bus_log_create_error(r); | |
14965b94 | 2466 | |
06c28aa0 FB |
2467 | /* If only argument is the empty string, then call SetLinkDNS() with an |
2468 | * empty list, which will clear the list of domains for an interface. */ | |
65856bf2 YW |
2469 | if (!strv_equal(dns, STRV_MAKE(""))) |
2470 | STRV_FOREACH(p, dns) { | |
b1881e83 | 2471 | _cleanup_free_ char *name = NULL; |
06c28aa0 | 2472 | struct in_addr_data data; |
b1881e83 YW |
2473 | uint16_t port; |
2474 | int ifindex; | |
14965b94 | 2475 | |
b1881e83 | 2476 | r = in_addr_port_ifindex_name_from_string_auto(*p, &data.family, &data.address, &port, &ifindex, &name); |
06c28aa0 FB |
2477 | if (r < 0) |
2478 | return log_error_errno(r, "Failed to parse DNS server address: %s", *p); | |
14965b94 | 2479 | |
b1881e83 YW |
2480 | if (ifindex != 0 && ifindex != arg_ifindex) |
2481 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid ifindex: %i", ifindex); | |
2482 | ||
2483 | r = sd_bus_message_open_container(req, 'r', extended ? "iayqs" : "iay"); | |
06c28aa0 FB |
2484 | if (r < 0) |
2485 | return bus_log_create_error(r); | |
14965b94 | 2486 | |
06c28aa0 FB |
2487 | r = sd_bus_message_append(req, "i", data.family); |
2488 | if (r < 0) | |
2489 | return bus_log_create_error(r); | |
14965b94 | 2490 | |
06c28aa0 FB |
2491 | r = sd_bus_message_append_array(req, 'y', &data.address, FAMILY_ADDRESS_SIZE(data.family)); |
2492 | if (r < 0) | |
2493 | return bus_log_create_error(r); | |
a7a4c60a | 2494 | |
b1881e83 YW |
2495 | if (extended) { |
2496 | r = sd_bus_message_append(req, "q", port); | |
2497 | if (r < 0) | |
2498 | return bus_log_create_error(r); | |
2499 | ||
2500 | r = sd_bus_message_append(req, "s", name); | |
2501 | if (r < 0) | |
2502 | return bus_log_create_error(r); | |
2503 | } | |
2504 | ||
06c28aa0 FB |
2505 | r = sd_bus_message_close_container(req); |
2506 | if (r < 0) | |
2507 | return bus_log_create_error(r); | |
2508 | } | |
14965b94 | 2509 | |
a7a4c60a YW |
2510 | r = sd_bus_message_close_container(req); |
2511 | if (r < 0) | |
2512 | return bus_log_create_error(r); | |
2513 | ||
b1881e83 | 2514 | r = sd_bus_call(bus, req, 0, error, NULL); |
f527c6fa YW |
2515 | if (r < 0 && extended && sd_bus_error_has_name(error, SD_BUS_ERROR_UNKNOWN_METHOD)) { |
2516 | sd_bus_error_free(error); | |
b1881e83 | 2517 | return call_dns(bus, dns, locator, error, false); |
f527c6fa | 2518 | } |
b1881e83 | 2519 | return r; |
a7a4c60a YW |
2520 | } |
2521 | ||
65856bf2 | 2522 | static int verb_dns(int argc, char **argv, void *userdata) { |
614a6770 | 2523 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
a7a4c60a | 2524 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a661dc36 | 2525 | int r; |
a7a4c60a | 2526 | |
614a6770 YW |
2527 | r = acquire_bus(&bus); |
2528 | if (r < 0) | |
2529 | return r; | |
2530 | ||
d1293049 | 2531 | if (argc >= 2) { |
df87a53d | 2532 | r = ifname_mangle(argv[1]); |
d1293049 LP |
2533 | if (r < 0) |
2534 | return r; | |
2535 | } | |
a7a4c60a | 2536 | |
d1293049 | 2537 | if (arg_ifindex <= 0) |
65856bf2 | 2538 | return status_all(bus, STATUS_DNS); |
14965b94 | 2539 | |
d1293049 | 2540 | if (argc < 3) |
65856bf2 YW |
2541 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DNS, NULL); |
2542 | ||
f75ecb9f YW |
2543 | char **args = strv_skip(argv, 2); |
2544 | r = call_dns(bus, args, bus_resolve_mgr, &error, true); | |
65856bf2 YW |
2545 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2546 | sd_bus_error_free(&error); | |
2547 | ||
f75ecb9f | 2548 | r = call_dns(bus, args, bus_network_mgr, &error, true); |
65856bf2 YW |
2549 | } |
2550 | if (r < 0) { | |
2551 | if (arg_ifindex_permissive && | |
2552 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2553 | return 0; | |
2554 | ||
2555 | return log_error_errno(r, "Failed to set DNS configuration: %s", bus_error_message(&error, r)); | |
2556 | } | |
2557 | ||
2558 | return 0; | |
2559 | } | |
2560 | ||
d96f9abc | 2561 | static int call_domain(sd_bus *bus, char **domain, const BusLocator *locator, sd_bus_error *error) { |
65856bf2 | 2562 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL; |
65856bf2 | 2563 | int r; |
14965b94 | 2564 | |
1d7fa677 YW |
2565 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
2566 | ||
d96f9abc | 2567 | r = bus_message_new_method_call(bus, &req, locator, "SetLinkDomains"); |
a7a4c60a YW |
2568 | if (r < 0) |
2569 | return bus_log_create_error(r); | |
14965b94 | 2570 | |
a661dc36 | 2571 | r = sd_bus_message_append(req, "i", arg_ifindex); |
a7a4c60a YW |
2572 | if (r < 0) |
2573 | return bus_log_create_error(r); | |
14965b94 | 2574 | |
a7a4c60a YW |
2575 | r = sd_bus_message_open_container(req, 'a', "(sb)"); |
2576 | if (r < 0) | |
2577 | return bus_log_create_error(r); | |
2578 | ||
06c28aa0 FB |
2579 | /* If only argument is the empty string, then call SetLinkDomains() with an |
2580 | * empty list, which will clear the list of domains for an interface. */ | |
65856bf2 YW |
2581 | if (!strv_equal(domain, STRV_MAKE(""))) |
2582 | STRV_FOREACH(p, domain) { | |
06c28aa0 | 2583 | const char *n; |
14965b94 | 2584 | |
06c28aa0 | 2585 | n = **p == '~' ? *p + 1 : *p; |
14965b94 | 2586 | |
06c28aa0 FB |
2587 | r = dns_name_is_valid(n); |
2588 | if (r < 0) | |
2589 | return log_error_errno(r, "Failed to validate specified domain %s: %m", n); | |
d7a0f1f4 FS |
2590 | if (r == 0) |
2591 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2592 | "Domain not valid: %s", | |
2593 | n); | |
a7a4c60a | 2594 | |
06c28aa0 FB |
2595 | r = sd_bus_message_append(req, "(sb)", n, **p == '~'); |
2596 | if (r < 0) | |
2597 | return bus_log_create_error(r); | |
2598 | } | |
14965b94 | 2599 | |
a7a4c60a YW |
2600 | r = sd_bus_message_close_container(req); |
2601 | if (r < 0) | |
2602 | return bus_log_create_error(r); | |
2603 | ||
65856bf2 YW |
2604 | return sd_bus_call(bus, req, 0, error, NULL); |
2605 | } | |
2606 | ||
2607 | static int verb_domain(int argc, char **argv, void *userdata) { | |
614a6770 | 2608 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
65856bf2 | 2609 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
65856bf2 YW |
2610 | int r; |
2611 | ||
614a6770 YW |
2612 | r = acquire_bus(&bus); |
2613 | if (r < 0) | |
2614 | return r; | |
2615 | ||
65856bf2 YW |
2616 | if (argc >= 2) { |
2617 | r = ifname_mangle(argv[1]); | |
2618 | if (r < 0) | |
2619 | return r; | |
2620 | } | |
2621 | ||
2622 | if (arg_ifindex <= 0) | |
2623 | return status_all(bus, STATUS_DOMAIN); | |
2624 | ||
2625 | if (argc < 3) | |
2626 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DOMAIN, NULL); | |
2627 | ||
f75ecb9f YW |
2628 | char **args = strv_skip(argv, 2); |
2629 | r = call_domain(bus, args, bus_resolve_mgr, &error); | |
65856bf2 YW |
2630 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2631 | sd_bus_error_free(&error); | |
14965b94 | 2632 | |
f75ecb9f | 2633 | r = call_domain(bus, args, bus_network_mgr, &error); |
65856bf2 YW |
2634 | } |
2635 | if (r < 0) { | |
a7a4c60a YW |
2636 | if (arg_ifindex_permissive && |
2637 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2638 | return 0; | |
14965b94 | 2639 | |
a7a4c60a | 2640 | return log_error_errno(r, "Failed to set domain configuration: %s", bus_error_message(&error, r)); |
14965b94 | 2641 | } |
a7a4c60a YW |
2642 | |
2643 | return 0; | |
14965b94 LP |
2644 | } |
2645 | ||
f2fd3cdb | 2646 | static int verb_default_route(int argc, char **argv, void *userdata) { |
614a6770 | 2647 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
f2fd3cdb | 2648 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f2fd3cdb LP |
2649 | int r, b; |
2650 | ||
614a6770 YW |
2651 | r = acquire_bus(&bus); |
2652 | if (r < 0) | |
2653 | return r; | |
2654 | ||
f2fd3cdb LP |
2655 | if (argc >= 2) { |
2656 | r = ifname_mangle(argv[1]); | |
2657 | if (r < 0) | |
2658 | return r; | |
2659 | } | |
2660 | ||
2661 | if (arg_ifindex <= 0) | |
2662 | return status_all(bus, STATUS_DEFAULT_ROUTE); | |
2663 | ||
2664 | if (argc < 3) | |
2665 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DEFAULT_ROUTE, NULL); | |
2666 | ||
2667 | b = parse_boolean(argv[2]); | |
2668 | if (b < 0) | |
2669 | return log_error_errno(b, "Failed to parse boolean argument: %s", argv[2]); | |
2670 | ||
1d7fa677 YW |
2671 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
2672 | ||
d96f9abc | 2673 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDefaultRoute", &error, NULL, "ib", arg_ifindex, b); |
65856bf2 YW |
2674 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2675 | sd_bus_error_free(&error); | |
2676 | ||
d96f9abc | 2677 | r = bus_call_method(bus, bus_network_mgr, "SetLinkDefaultRoute", &error, NULL, "ib", arg_ifindex, b); |
65856bf2 | 2678 | } |
f2fd3cdb | 2679 | if (r < 0) { |
f2fd3cdb LP |
2680 | if (arg_ifindex_permissive && |
2681 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2682 | return 0; | |
2683 | ||
2684 | return log_error_errno(r, "Failed to set default route configuration: %s", bus_error_message(&error, r)); | |
2685 | } | |
2686 | ||
2687 | return 0; | |
2688 | } | |
2689 | ||
a7a4c60a | 2690 | static int verb_llmnr(int argc, char **argv, void *userdata) { |
614a6770 | 2691 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
14965b94 | 2692 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
c38a03df YW |
2693 | _cleanup_free_ char *global_llmnr_support_str = NULL; |
2694 | ResolveSupport global_llmnr_support, llmnr_support; | |
a661dc36 | 2695 | int r; |
14965b94 | 2696 | |
614a6770 YW |
2697 | r = acquire_bus(&bus); |
2698 | if (r < 0) | |
2699 | return r; | |
2700 | ||
d1293049 | 2701 | if (argc >= 2) { |
df87a53d | 2702 | r = ifname_mangle(argv[1]); |
d1293049 LP |
2703 | if (r < 0) |
2704 | return r; | |
2705 | } | |
a7a4c60a | 2706 | |
d1293049 LP |
2707 | if (arg_ifindex <= 0) |
2708 | return status_all(bus, STATUS_LLMNR); | |
a7a4c60a | 2709 | |
d1293049 | 2710 | if (argc < 3) |
a661dc36 | 2711 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_LLMNR, NULL); |
a7a4c60a | 2712 | |
c38a03df YW |
2713 | llmnr_support = resolve_support_from_string(argv[2]); |
2714 | if (llmnr_support < 0) | |
2715 | return log_error_errno(llmnr_support, "Invalid LLMNR setting: %s", argv[2]); | |
2716 | ||
2717 | r = bus_get_property_string(bus, bus_resolve_mgr, "LLMNR", &error, &global_llmnr_support_str); | |
2718 | if (r < 0) | |
2719 | return log_error_errno(r, "Failed to get the global LLMNR support state: %s", bus_error_message(&error, r)); | |
2720 | ||
2721 | global_llmnr_support = resolve_support_from_string(global_llmnr_support_str); | |
2722 | if (global_llmnr_support < 0) | |
2723 | return log_error_errno(global_llmnr_support, "Received invalid global LLMNR setting: %s", global_llmnr_support_str); | |
2724 | ||
2725 | if (global_llmnr_support < llmnr_support) | |
2726 | log_warning("Setting LLMNR support level \"%s\" for \"%s\", but the global support level is \"%s\".", | |
2727 | argv[2], arg_ifname, global_llmnr_support_str); | |
2728 | ||
1d7fa677 YW |
2729 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
2730 | ||
d96f9abc | 2731 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkLLMNR", &error, NULL, "is", arg_ifindex, argv[2]); |
65856bf2 YW |
2732 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2733 | sd_bus_error_free(&error); | |
2734 | ||
d96f9abc | 2735 | r = bus_call_method(bus, bus_network_mgr, "SetLinkLLMNR", &error, NULL, "is", arg_ifindex, argv[2]); |
65856bf2 | 2736 | } |
088c1363 LP |
2737 | if (r < 0) { |
2738 | if (arg_ifindex_permissive && | |
2739 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2740 | return 0; | |
2741 | ||
a7a4c60a | 2742 | return log_error_errno(r, "Failed to set LLMNR configuration: %s", bus_error_message(&error, r)); |
088c1363 | 2743 | } |
14965b94 LP |
2744 | |
2745 | return 0; | |
2746 | } | |
2747 | ||
a7a4c60a | 2748 | static int verb_mdns(int argc, char **argv, void *userdata) { |
614a6770 | 2749 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
a7a4c60a | 2750 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
c38a03df YW |
2751 | _cleanup_free_ char *global_mdns_support_str = NULL; |
2752 | ResolveSupport global_mdns_support, mdns_support; | |
a661dc36 | 2753 | int r; |
ba82da3b | 2754 | |
614a6770 YW |
2755 | r = acquire_bus(&bus); |
2756 | if (r < 0) | |
2757 | return r; | |
2758 | ||
d1293049 | 2759 | if (argc >= 2) { |
df87a53d | 2760 | r = ifname_mangle(argv[1]); |
d1293049 LP |
2761 | if (r < 0) |
2762 | return r; | |
2763 | } | |
a7a4c60a | 2764 | |
d1293049 LP |
2765 | if (arg_ifindex <= 0) |
2766 | return status_all(bus, STATUS_MDNS); | |
a7a4c60a | 2767 | |
d1293049 | 2768 | if (argc < 3) |
a661dc36 | 2769 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_MDNS, NULL); |
a7a4c60a | 2770 | |
c38a03df YW |
2771 | mdns_support = resolve_support_from_string(argv[2]); |
2772 | if (mdns_support < 0) | |
2773 | return log_error_errno(mdns_support, "Invalid mDNS setting: %s", argv[2]); | |
2774 | ||
2775 | r = bus_get_property_string(bus, bus_resolve_mgr, "MulticastDNS", &error, &global_mdns_support_str); | |
2776 | if (r < 0) | |
2777 | return log_error_errno(r, "Failed to get the global mDNS support state: %s", bus_error_message(&error, r)); | |
2778 | ||
2779 | global_mdns_support = resolve_support_from_string(global_mdns_support_str); | |
2780 | if (global_mdns_support < 0) | |
2781 | return log_error_errno(global_mdns_support, "Received invalid global mDNS setting: %s", global_mdns_support_str); | |
2782 | ||
2783 | if (global_mdns_support < mdns_support) | |
2784 | log_warning("Setting mDNS support level \"%s\" for \"%s\", but the global support level is \"%s\".", | |
2785 | argv[2], arg_ifname, global_mdns_support_str); | |
2786 | ||
1d7fa677 YW |
2787 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
2788 | ||
d96f9abc | 2789 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkMulticastDNS", &error, NULL, "is", arg_ifindex, argv[2]); |
65856bf2 YW |
2790 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2791 | sd_bus_error_free(&error); | |
2792 | ||
d96f9abc | 2793 | r = bus_call_method( |
65856bf2 | 2794 | bus, |
d96f9abc | 2795 | bus_network_mgr, |
65856bf2 YW |
2796 | "SetLinkMulticastDNS", |
2797 | &error, | |
2798 | NULL, | |
2799 | "is", arg_ifindex, argv[2]); | |
2800 | } | |
a7a4c60a | 2801 | if (r < 0) { |
a7a4c60a YW |
2802 | if (arg_ifindex_permissive && |
2803 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2804 | return 0; | |
2805 | ||
2806 | return log_error_errno(r, "Failed to set MulticastDNS configuration: %s", bus_error_message(&error, r)); | |
2807 | } | |
2808 | ||
2809 | return 0; | |
2810 | } | |
2811 | ||
c9299be2 | 2812 | static int verb_dns_over_tls(int argc, char **argv, void *userdata) { |
614a6770 | 2813 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
d050561a | 2814 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a661dc36 | 2815 | int r; |
d050561a | 2816 | |
614a6770 YW |
2817 | r = acquire_bus(&bus); |
2818 | if (r < 0) | |
2819 | return r; | |
2820 | ||
d1293049 | 2821 | if (argc >= 2) { |
df87a53d | 2822 | r = ifname_mangle(argv[1]); |
d1293049 LP |
2823 | if (r < 0) |
2824 | return r; | |
2825 | } | |
d050561a | 2826 | |
d1293049 LP |
2827 | if (arg_ifindex <= 0) |
2828 | return status_all(bus, STATUS_PRIVATE); | |
d050561a | 2829 | |
d1293049 | 2830 | if (argc < 3) |
a661dc36 | 2831 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_PRIVATE, NULL); |
d050561a | 2832 | |
1d7fa677 YW |
2833 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
2834 | ||
d96f9abc | 2835 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDNSOverTLS", &error, NULL, "is", arg_ifindex, argv[2]); |
65856bf2 YW |
2836 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2837 | sd_bus_error_free(&error); | |
2838 | ||
d96f9abc | 2839 | r = bus_call_method( |
65856bf2 | 2840 | bus, |
d96f9abc | 2841 | bus_network_mgr, |
65856bf2 YW |
2842 | "SetLinkDNSOverTLS", |
2843 | &error, | |
2844 | NULL, | |
2845 | "is", arg_ifindex, argv[2]); | |
2846 | } | |
d050561a | 2847 | if (r < 0) { |
d050561a IT |
2848 | if (arg_ifindex_permissive && |
2849 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2850 | return 0; | |
2851 | ||
c9299be2 | 2852 | return log_error_errno(r, "Failed to set DNSOverTLS configuration: %s", bus_error_message(&error, r)); |
d050561a IT |
2853 | } |
2854 | ||
2855 | return 0; | |
2856 | } | |
2857 | ||
a7a4c60a | 2858 | static int verb_dnssec(int argc, char **argv, void *userdata) { |
614a6770 | 2859 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
a7a4c60a | 2860 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a661dc36 | 2861 | int r; |
a7a4c60a | 2862 | |
614a6770 YW |
2863 | r = acquire_bus(&bus); |
2864 | if (r < 0) | |
2865 | return r; | |
2866 | ||
d1293049 | 2867 | if (argc >= 2) { |
df87a53d | 2868 | r = ifname_mangle(argv[1]); |
d1293049 LP |
2869 | if (r < 0) |
2870 | return r; | |
2871 | } | |
a7a4c60a | 2872 | |
d1293049 LP |
2873 | if (arg_ifindex <= 0) |
2874 | return status_all(bus, STATUS_DNSSEC); | |
a7a4c60a | 2875 | |
d1293049 | 2876 | if (argc < 3) |
a661dc36 | 2877 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_DNSSEC, NULL); |
a7a4c60a | 2878 | |
1d7fa677 YW |
2879 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
2880 | ||
d96f9abc | 2881 | r = bus_call_method(bus, bus_resolve_mgr, "SetLinkDNSSEC", &error, NULL, "is", arg_ifindex, argv[2]); |
65856bf2 YW |
2882 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2883 | sd_bus_error_free(&error); | |
2884 | ||
d96f9abc | 2885 | r = bus_call_method(bus, bus_network_mgr, "SetLinkDNSSEC", &error, NULL, "is", arg_ifindex, argv[2]); |
65856bf2 | 2886 | } |
a7a4c60a | 2887 | if (r < 0) { |
a7a4c60a YW |
2888 | if (arg_ifindex_permissive && |
2889 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2890 | return 0; | |
2891 | ||
2892 | return log_error_errno(r, "Failed to set DNSSEC configuration: %s", bus_error_message(&error, r)); | |
2893 | } | |
2894 | ||
2895 | return 0; | |
2896 | } | |
2897 | ||
d96f9abc | 2898 | static int call_nta(sd_bus *bus, char **nta, const BusLocator *locator, sd_bus_error *error) { |
65856bf2 YW |
2899 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL; |
2900 | int r; | |
2901 | ||
1d7fa677 YW |
2902 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
2903 | ||
d96f9abc | 2904 | r = bus_message_new_method_call(bus, &req, locator, "SetLinkDNSSECNegativeTrustAnchors"); |
65856bf2 YW |
2905 | if (r < 0) |
2906 | return bus_log_create_error(r); | |
2907 | ||
2908 | r = sd_bus_message_append(req, "i", arg_ifindex); | |
2909 | if (r < 0) | |
2910 | return bus_log_create_error(r); | |
2911 | ||
2912 | r = sd_bus_message_append_strv(req, nta); | |
2913 | if (r < 0) | |
2914 | return bus_log_create_error(r); | |
2915 | ||
2916 | return sd_bus_call(bus, req, 0, error, NULL); | |
2917 | } | |
2918 | ||
a7a4c60a | 2919 | static int verb_nta(int argc, char **argv, void *userdata) { |
614a6770 | 2920 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
a7a4c60a | 2921 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
f75ecb9f | 2922 | char **args; |
06c28aa0 | 2923 | bool clear; |
f75ecb9f | 2924 | int r; |
a7a4c60a | 2925 | |
614a6770 YW |
2926 | r = acquire_bus(&bus); |
2927 | if (r < 0) | |
2928 | return r; | |
2929 | ||
d1293049 | 2930 | if (argc >= 2) { |
df87a53d | 2931 | r = ifname_mangle(argv[1]); |
d1293049 LP |
2932 | if (r < 0) |
2933 | return r; | |
2934 | } | |
a7a4c60a | 2935 | |
d1293049 LP |
2936 | if (arg_ifindex <= 0) |
2937 | return status_all(bus, STATUS_NTA); | |
a7a4c60a | 2938 | |
d1293049 | 2939 | if (argc < 3) |
a661dc36 | 2940 | return status_ifindex(bus, arg_ifindex, NULL, STATUS_NTA, NULL); |
a7a4c60a | 2941 | |
1d7fa677 YW |
2942 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
2943 | ||
06c28aa0 FB |
2944 | /* If only argument is the empty string, then call SetLinkDNSSECNegativeTrustAnchors() |
2945 | * with an empty list, which will clear the list of domains for an interface. */ | |
f75ecb9f YW |
2946 | args = strv_skip(argv, 2); |
2947 | clear = strv_equal(args, STRV_MAKE("")); | |
06c28aa0 FB |
2948 | |
2949 | if (!clear) | |
f75ecb9f | 2950 | STRV_FOREACH(p, args) { |
06c28aa0 FB |
2951 | r = dns_name_is_valid(*p); |
2952 | if (r < 0) | |
2953 | return log_error_errno(r, "Failed to validate specified domain %s: %m", *p); | |
d7a0f1f4 FS |
2954 | if (r == 0) |
2955 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2956 | "Domain not valid: %s", | |
2957 | *p); | |
a7a4c60a | 2958 | } |
a7a4c60a | 2959 | |
f75ecb9f | 2960 | r = call_nta(bus, clear ? NULL : args, bus_resolve_mgr, &error); |
65856bf2 YW |
2961 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2962 | sd_bus_error_free(&error); | |
a7a4c60a | 2963 | |
f75ecb9f | 2964 | r = call_nta(bus, clear ? NULL : args, bus_network_mgr, &error); |
65856bf2 | 2965 | } |
a7a4c60a | 2966 | if (r < 0) { |
a7a4c60a YW |
2967 | if (arg_ifindex_permissive && |
2968 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
2969 | return 0; | |
2970 | ||
2971 | return log_error_errno(r, "Failed to set DNSSEC NTA configuration: %s", bus_error_message(&error, r)); | |
2972 | } | |
2973 | ||
2974 | return 0; | |
2975 | } | |
2976 | ||
2977 | static int verb_revert_link(int argc, char **argv, void *userdata) { | |
614a6770 | 2978 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
a7a4c60a | 2979 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
a661dc36 | 2980 | int r; |
a7a4c60a | 2981 | |
614a6770 YW |
2982 | r = acquire_bus(&bus); |
2983 | if (r < 0) | |
2984 | return r; | |
2985 | ||
d1293049 | 2986 | if (argc >= 2) { |
df87a53d | 2987 | r = ifname_mangle(argv[1]); |
d1293049 LP |
2988 | if (r < 0) |
2989 | return r; | |
2990 | } | |
2991 | ||
2992 | if (arg_ifindex <= 0) | |
2993 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Interface argument required."); | |
a7a4c60a | 2994 | |
1d7fa677 YW |
2995 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
2996 | ||
d96f9abc | 2997 | r = bus_call_method(bus, bus_resolve_mgr, "RevertLink", &error, NULL, "i", arg_ifindex); |
65856bf2 YW |
2998 | if (r < 0 && sd_bus_error_has_name(&error, BUS_ERROR_LINK_BUSY)) { |
2999 | sd_bus_error_free(&error); | |
3000 | ||
d96f9abc | 3001 | r = bus_call_method(bus, bus_network_mgr, "RevertLinkDNS", &error, NULL, "i", arg_ifindex); |
65856bf2 | 3002 | } |
a7a4c60a YW |
3003 | if (r < 0) { |
3004 | if (arg_ifindex_permissive && | |
3005 | sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_LINK)) | |
3006 | return 0; | |
3007 | ||
3008 | return log_error_errno(r, "Failed to revert interface configuration: %s", bus_error_message(&error, r)); | |
3009 | } | |
3010 | ||
3011 | return 0; | |
3012 | } | |
3013 | ||
df957849 | 3014 | static int verb_log_level(int argc, char *argv[], void *userdata) { |
614a6770 YW |
3015 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
3016 | int r; | |
3017 | ||
3018 | r = acquire_bus(&bus); | |
3019 | if (r < 0) | |
3020 | return r; | |
df957849 | 3021 | |
b98416e1 | 3022 | assert(IN_SET(argc, 1, 2)); |
df957849 | 3023 | |
a87b151a | 3024 | return verb_log_control_common(bus, "org.freedesktop.resolve1", argv[0], argc == 2 ? argv[1] : NULL); |
df957849 ZJS |
3025 | } |
3026 | ||
309a747f LP |
3027 | static int print_question(char prefix, const char *color, sd_json_variant *question) { |
3028 | sd_json_variant *q = NULL; | |
fffbf1dc LP |
3029 | int r; |
3030 | ||
3031 | assert(color); | |
3032 | ||
3033 | JSON_VARIANT_ARRAY_FOREACH(q, question) { | |
3034 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; | |
3035 | char buf[DNS_RESOURCE_KEY_STRING_MAX]; | |
3036 | ||
ce74fb09 | 3037 | r = dns_resource_key_from_json(q, &key); |
fffbf1dc LP |
3038 | if (r < 0) { |
3039 | log_warning_errno(r, "Received monitor message with invalid question key, ignoring: %m"); | |
3040 | continue; | |
3041 | } | |
3042 | ||
3043 | printf("%s%s %c%s: %s\n", | |
3044 | color, | |
1ae9b0cf | 3045 | glyph(GLYPH_ARROW_RIGHT), |
fffbf1dc LP |
3046 | prefix, |
3047 | ansi_normal(), | |
3048 | dns_resource_key_to_string(key, buf, sizeof(buf))); | |
3049 | } | |
3050 | ||
3051 | return 0; | |
3052 | } | |
3053 | ||
309a747f LP |
3054 | static int print_answer(sd_json_variant *answer) { |
3055 | sd_json_variant *a; | |
fffbf1dc LP |
3056 | int r; |
3057 | ||
3058 | JSON_VARIANT_ARRAY_FOREACH(a, answer) { | |
3059 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
3060 | _cleanup_free_ void *d = NULL; | |
309a747f | 3061 | sd_json_variant *jraw; |
fffbf1dc LP |
3062 | const char *s; |
3063 | size_t l; | |
3064 | ||
309a747f | 3065 | jraw = sd_json_variant_by_key(a, "raw"); |
fffbf1dc LP |
3066 | if (!jraw) { |
3067 | log_warning("Received monitor answer lacking valid raw data, ignoring."); | |
3068 | continue; | |
3069 | } | |
3070 | ||
309a747f | 3071 | r = sd_json_variant_unbase64(jraw, &d, &l); |
fffbf1dc LP |
3072 | if (r < 0) { |
3073 | log_warning_errno(r, "Failed to undo base64 encoding of monitor answer raw data, ignoring."); | |
3074 | continue; | |
3075 | } | |
3076 | ||
3077 | r = dns_resource_record_new_from_raw(&rr, d, l); | |
3078 | if (r < 0) { | |
64ebc0da | 3079 | log_warning_errno(r, "Failed to parse monitor answer RR, ignoring: %m"); |
fffbf1dc LP |
3080 | continue; |
3081 | } | |
3082 | ||
3083 | s = dns_resource_record_to_string(rr); | |
3084 | if (!s) | |
3085 | return log_oom(); | |
3086 | ||
3087 | printf("%s%s A%s: %s\n", | |
3088 | ansi_highlight_yellow(), | |
1ae9b0cf | 3089 | glyph(GLYPH_ARROW_LEFT), |
fffbf1dc LP |
3090 | ansi_normal(), |
3091 | s); | |
3092 | } | |
3093 | ||
3094 | return 0; | |
3095 | } | |
3096 | ||
36df48d8 YW |
3097 | typedef struct MonitorQueryParams { |
3098 | sd_json_variant *question; | |
3099 | sd_json_variant *answer; | |
3100 | sd_json_variant *collected_questions; | |
3101 | int rcode; | |
3102 | int error; | |
3103 | int ede_code; | |
3104 | const char *state; | |
3105 | const char *result; | |
3106 | const char *ede_msg; | |
3107 | } MonitorQueryParams; | |
3108 | ||
3109 | static void monitor_query_params_done(MonitorQueryParams *p) { | |
3110 | assert(p); | |
fffbf1dc | 3111 | |
36df48d8 YW |
3112 | sd_json_variant_unref(p->question); |
3113 | sd_json_variant_unref(p->answer); | |
3114 | sd_json_variant_unref(p->collected_questions); | |
3115 | } | |
fffbf1dc | 3116 | |
36df48d8 YW |
3117 | static void monitor_query_dump(sd_json_variant *v) { |
3118 | static const sd_json_dispatch_field dispatch_table[] = { | |
3119 | { "question", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(MonitorQueryParams, question), SD_JSON_MANDATORY }, | |
3120 | { "answer", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(MonitorQueryParams, answer), 0 }, | |
3121 | { "collectedQuestions", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant, offsetof(MonitorQueryParams, collected_questions), 0 }, | |
3122 | { "state", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MonitorQueryParams, state), SD_JSON_MANDATORY }, | |
3123 | { "result", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MonitorQueryParams, result), 0 }, | |
3124 | { "rcode", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(MonitorQueryParams, rcode), 0 }, | |
3125 | { "errno", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(MonitorQueryParams, error), 0 }, | |
3126 | { "extendedDNSErrorCode", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(MonitorQueryParams, ede_code), 0 }, | |
3127 | { "extendedDNSErrorMessage", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MonitorQueryParams, ede_msg), 0 }, | |
fffbf1dc LP |
3128 | {} |
3129 | }; | |
3130 | ||
36df48d8 YW |
3131 | _cleanup_(monitor_query_params_done) MonitorQueryParams p = { |
3132 | .rcode = -1, | |
3133 | .ede_code = -1, | |
3134 | }; | |
3135 | ||
3136 | assert(v); | |
3137 | ||
3138 | if (sd_json_dispatch(v, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &p) < 0) | |
0c61995d | 3139 | return; |
fffbf1dc LP |
3140 | |
3141 | /* First show the current question */ | |
36df48d8 | 3142 | print_question('Q', ansi_highlight_cyan(), p.question); |
fffbf1dc LP |
3143 | |
3144 | /* And then show the questions that led to this one in case this was a CNAME chain */ | |
36df48d8 | 3145 | print_question('C', ansi_highlight_grey(), p.collected_questions); |
fffbf1dc | 3146 | |
a068e06d | 3147 | printf("%s%s S%s: %s", |
36df48d8 | 3148 | streq_ptr(p.state, "success") ? ansi_highlight_green() : ansi_highlight_red(), |
1ae9b0cf | 3149 | glyph(GLYPH_ARROW_LEFT), |
fffbf1dc | 3150 | ansi_normal(), |
858cb6e4 ZJS |
3151 | streq_ptr(p.state, "errno") ? ERRNO_NAME(p.error) : |
3152 | streq_ptr(p.state, "rcode-failure") ? strna(dns_rcode_to_string(p.rcode)) : | |
3153 | strna(p.state)); | |
fffbf1dc | 3154 | |
36df48d8 YW |
3155 | if (!isempty(p.result)) |
3156 | printf(": %s", p.result); | |
a068e06d | 3157 | |
36df48d8 | 3158 | if (p.ede_code >= 0) |
a068e06d | 3159 | printf(" (%s%s%s)", |
36df48d8 YW |
3160 | FORMAT_DNS_EDE_RCODE(p.ede_code), |
3161 | !isempty(p.ede_msg) ? ": " : "", | |
3162 | strempty(p.ede_msg)); | |
a068e06d YW |
3163 | |
3164 | puts(""); | |
3165 | ||
36df48d8 | 3166 | print_answer(p.answer); |
fffbf1dc LP |
3167 | } |
3168 | ||
3169 | static int monitor_reply( | |
25ff515b | 3170 | sd_varlink *link, |
309a747f | 3171 | sd_json_variant *parameters, |
fffbf1dc | 3172 | const char *error_id, |
25ff515b | 3173 | sd_varlink_reply_flags_t flags, |
fffbf1dc LP |
3174 | void *userdata) { |
3175 | ||
3176 | assert(link); | |
3177 | ||
3178 | if (error_id) { | |
3179 | bool disconnect; | |
3180 | ||
25ff515b | 3181 | disconnect = streq(error_id, SD_VARLINK_ERROR_DISCONNECTED); |
fffbf1dc LP |
3182 | if (disconnect) |
3183 | log_info("Disconnected."); | |
3184 | else | |
3185 | log_error("Varlink error: %s", error_id); | |
3186 | ||
25ff515b | 3187 | (void) sd_event_exit(ASSERT_PTR(sd_varlink_get_event(link)), disconnect ? EXIT_SUCCESS : EXIT_FAILURE); |
fffbf1dc LP |
3188 | return 0; |
3189 | } | |
3190 | ||
309a747f | 3191 | if (sd_json_variant_by_key(parameters, "ready")) { |
fffbf1dc LP |
3192 | /* The first message coming in will just indicate that we are now subscribed. We let our |
3193 | * caller know if they asked for it. Once the caller sees this they should know that we are | |
3194 | * not going to miss any queries anymore. */ | |
3195 | (void) sd_notify(/* unset_environment=false */ false, "READY=1"); | |
3196 | return 0; | |
3197 | } | |
3198 | ||
23441a3d | 3199 | if (!sd_json_format_enabled(arg_json_format_flags)) { |
fffbf1dc LP |
3200 | monitor_query_dump(parameters); |
3201 | printf("\n"); | |
3202 | } else | |
309a747f | 3203 | sd_json_variant_dump(parameters, arg_json_format_flags, NULL, NULL); |
fffbf1dc LP |
3204 | |
3205 | fflush(stdout); | |
3206 | ||
3207 | return 0; | |
3208 | } | |
3209 | ||
3210 | static int verb_monitor(int argc, char *argv[], void *userdata) { | |
3211 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
25ff515b | 3212 | _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
fffbf1dc LP |
3213 | int r, c; |
3214 | ||
cf01bbb7 YW |
3215 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
3216 | ||
fffbf1dc LP |
3217 | r = sd_event_default(&event); |
3218 | if (r < 0) | |
3219 | return log_error_errno(r, "Failed to get event loop: %m"); | |
3220 | ||
3221 | r = sd_event_set_signal_exit(event, true); | |
3222 | if (r < 0) | |
3223 | return log_error_errno(r, "Failed to enable exit on SIGINT/SIGTERM: %m"); | |
3224 | ||
25ff515b | 3225 | r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor"); |
fffbf1dc LP |
3226 | if (r < 0) |
3227 | return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m"); | |
3228 | ||
25ff515b | 3229 | r = sd_varlink_set_relative_timeout(vl, USEC_INFINITY); /* We want the monitor to run basically forever */ |
fffbf1dc | 3230 | if (r < 0) |
2257be13 | 3231 | return log_error_errno(r, "Failed to set varlink timeout: %m"); |
fffbf1dc | 3232 | |
25ff515b | 3233 | r = sd_varlink_attach_event(vl, event, SD_EVENT_PRIORITY_NORMAL); |
fffbf1dc LP |
3234 | if (r < 0) |
3235 | return log_error_errno(r, "Failed to attach varlink connection to event loop: %m"); | |
3236 | ||
25ff515b | 3237 | r = sd_varlink_bind_reply(vl, monitor_reply); |
fffbf1dc LP |
3238 | if (r < 0) |
3239 | return log_error_errno(r, "Failed to bind reply callback to varlink connection: %m"); | |
3240 | ||
cf01bbb7 YW |
3241 | r = sd_varlink_observebo( |
3242 | vl, | |
3243 | "io.systemd.Resolve.Monitor.SubscribeQueryResults", | |
3244 | SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password)); | |
fffbf1dc LP |
3245 | if (r < 0) |
3246 | return log_error_errno(r, "Failed to issue SubscribeQueryResults() varlink call: %m"); | |
3247 | ||
3248 | r = sd_event_loop(event); | |
3249 | if (r < 0) | |
3250 | return log_error_errno(r, "Failed to run event loop: %m"); | |
3251 | ||
3252 | r = sd_event_get_exit_code(event, &c); | |
3253 | if (r < 0) | |
3254 | return log_error_errno(r, "Failed to get exit code: %m"); | |
3255 | ||
3256 | return c; | |
3257 | } | |
3258 | ||
309a747f | 3259 | static int dump_cache_item(sd_json_variant *item) { |
6050e8b5 LP |
3260 | |
3261 | struct item_info { | |
309a747f LP |
3262 | sd_json_variant *key; |
3263 | sd_json_variant *rrs; | |
6050e8b5 LP |
3264 | const char *type; |
3265 | uint64_t until; | |
3266 | } item_info = {}; | |
3267 | ||
309a747f LP |
3268 | static const sd_json_dispatch_field dispatch_table[] = { |
3269 | { "key", SD_JSON_VARIANT_OBJECT, sd_json_dispatch_variant_noref, offsetof(struct item_info, key), SD_JSON_MANDATORY }, | |
3270 | { "rrs", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant_noref, offsetof(struct item_info, rrs), 0 }, | |
3271 | { "type", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct item_info, type), 0 }, | |
3272 | { "until", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct item_info, until), 0 }, | |
6050e8b5 LP |
3273 | {}, |
3274 | }; | |
3275 | ||
3276 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *k = NULL; | |
3277 | int r, c = 0; | |
3278 | ||
309a747f | 3279 | r = sd_json_dispatch(item, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &item_info); |
6050e8b5 LP |
3280 | if (r < 0) |
3281 | return r; | |
3282 | ||
3283 | r = dns_resource_key_from_json(item_info.key, &k); | |
3284 | if (r < 0) | |
3285 | return log_error_errno(r, "Failed to turn JSON data to resource key: %m"); | |
3286 | ||
3287 | if (item_info.type) | |
3288 | printf("%s %s%s%s\n", DNS_RESOURCE_KEY_TO_STRING(k), ansi_highlight_red(), item_info.type, ansi_normal()); | |
3289 | else { | |
309a747f | 3290 | sd_json_variant *i; |
6050e8b5 LP |
3291 | |
3292 | JSON_VARIANT_ARRAY_FOREACH(i, item_info.rrs) { | |
3293 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
3294 | _cleanup_free_ void *data = NULL; | |
309a747f | 3295 | sd_json_variant *raw; |
6050e8b5 LP |
3296 | size_t size; |
3297 | ||
309a747f | 3298 | raw = sd_json_variant_by_key(i, "raw"); |
6050e8b5 LP |
3299 | if (!raw) |
3300 | return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "raw field missing from RR JSON data."); | |
3301 | ||
309a747f | 3302 | r = sd_json_variant_unbase64(raw, &data, &size); |
6050e8b5 LP |
3303 | if (r < 0) |
3304 | return log_error_errno(r, "Unable to decode raw RR JSON data: %m"); | |
3305 | ||
3306 | r = dns_resource_record_new_from_raw(&rr, data, size); | |
3307 | if (r < 0) | |
3308 | return log_error_errno(r, "Failed to parse DNS data: %m"); | |
3309 | ||
3310 | printf("%s\n", dns_resource_record_to_string(rr)); | |
3311 | c++; | |
3312 | } | |
3313 | } | |
3314 | ||
3315 | return c; | |
3316 | } | |
3317 | ||
309a747f | 3318 | static int dump_cache_scope(sd_json_variant *scope) { |
6050e8b5 LP |
3319 | |
3320 | struct scope_info { | |
3321 | const char *protocol; | |
3322 | int family; | |
3323 | int ifindex; | |
3324 | const char *ifname; | |
309a747f | 3325 | sd_json_variant *cache; |
71da4220 NR |
3326 | const char *dnssec_mode; |
3327 | const char *dns_over_tls_mode; | |
6050e8b5 LP |
3328 | } scope_info = { |
3329 | .family = AF_UNSPEC, | |
3330 | }; | |
309a747f | 3331 | sd_json_variant *i; |
6050e8b5 LP |
3332 | int r, c = 0; |
3333 | ||
309a747f | 3334 | static const sd_json_dispatch_field dispatch_table[] = { |
71da4220 NR |
3335 | { "protocol", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct scope_info, protocol), SD_JSON_MANDATORY }, |
3336 | { "family", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, offsetof(struct scope_info, family), 0 }, | |
3337 | { "ifindex", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(struct scope_info, ifindex), SD_JSON_RELAX }, | |
3338 | { "ifname", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct scope_info, ifname), 0 }, | |
3339 | { "cache", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant_noref, offsetof(struct scope_info, cache), SD_JSON_MANDATORY }, | |
3340 | { "dnssec", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct scope_info, dnssec_mode), 0 }, | |
3341 | { "dnsOverTLS", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct scope_info, dns_over_tls_mode), 0 }, | |
6050e8b5 LP |
3342 | {}, |
3343 | }; | |
3344 | ||
309a747f | 3345 | r = sd_json_dispatch(scope, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &scope_info); |
6050e8b5 LP |
3346 | if (r < 0) |
3347 | return r; | |
3348 | ||
3349 | printf("%sScope protocol=%s", ansi_underline(), scope_info.protocol); | |
3350 | ||
3351 | if (scope_info.family != AF_UNSPEC) | |
3352 | printf(" family=%s", af_to_name(scope_info.family)); | |
3353 | ||
3354 | if (scope_info.ifindex > 0) | |
3355 | printf(" ifindex=%i", scope_info.ifindex); | |
3356 | if (scope_info.ifname) | |
3357 | printf(" ifname=%s", scope_info.ifname); | |
3358 | ||
71da4220 NR |
3359 | if (dns_protocol_from_string(scope_info.protocol) == DNS_PROTOCOL_DNS) { |
3360 | if (scope_info.dnssec_mode) | |
3361 | printf(" DNSSEC=%s", scope_info.dnssec_mode); | |
3362 | if (scope_info.dns_over_tls_mode) | |
3363 | printf(" DNSOverTLS=%s", scope_info.dns_over_tls_mode); | |
3364 | } | |
3365 | ||
6050e8b5 LP |
3366 | printf("%s\n", ansi_normal()); |
3367 | ||
3368 | JSON_VARIANT_ARRAY_FOREACH(i, scope_info.cache) { | |
3369 | r = dump_cache_item(i); | |
3370 | if (r < 0) | |
3371 | return r; | |
3372 | ||
3373 | c += r; | |
3374 | } | |
3375 | ||
3376 | if (c == 0) | |
3377 | printf("%sNo entries.%s\n\n", ansi_grey(), ansi_normal()); | |
3378 | else | |
3379 | printf("\n"); | |
3380 | ||
3381 | return 0; | |
3382 | } | |
3383 | ||
3384 | static int verb_show_cache(int argc, char *argv[], void *userdata) { | |
309a747f | 3385 | sd_json_variant *reply = NULL, *d = NULL; |
25ff515b | 3386 | _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
6050e8b5 LP |
3387 | int r; |
3388 | ||
cf01bbb7 YW |
3389 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
3390 | ||
25ff515b | 3391 | r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor"); |
6050e8b5 LP |
3392 | if (r < 0) |
3393 | return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m"); | |
3394 | ||
cf01bbb7 YW |
3395 | r = varlink_callbo_and_log( |
3396 | vl, | |
3397 | "io.systemd.Resolve.Monitor.DumpCache", | |
3398 | &reply, | |
3399 | SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password)); | |
6050e8b5 | 3400 | if (r < 0) |
71d0ecc5 | 3401 | return r; |
6050e8b5 | 3402 | |
309a747f | 3403 | d = sd_json_variant_by_key(reply, "dump"); |
6050e8b5 LP |
3404 | if (!d) |
3405 | return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), | |
3406 | "DumpCache() response is missing 'dump' key."); | |
3407 | ||
309a747f | 3408 | if (!sd_json_variant_is_array(d)) |
6050e8b5 LP |
3409 | return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), |
3410 | "DumpCache() response 'dump' field not an array"); | |
3411 | ||
23441a3d | 3412 | if (!sd_json_format_enabled(arg_json_format_flags)) { |
309a747f | 3413 | sd_json_variant *i; |
6050e8b5 LP |
3414 | |
3415 | JSON_VARIANT_ARRAY_FOREACH(i, d) { | |
3416 | r = dump_cache_scope(i); | |
3417 | if (r < 0) | |
3418 | return r; | |
3419 | } | |
3420 | ||
3421 | return 0; | |
3422 | } | |
3423 | ||
309a747f | 3424 | return sd_json_variant_dump(d, arg_json_format_flags, NULL, NULL); |
6050e8b5 LP |
3425 | } |
3426 | ||
309a747f | 3427 | static int dump_server_state(sd_json_variant *server) { |
bc837621 KV |
3428 | _cleanup_(table_unrefp) Table *table = NULL; |
3429 | TableCell *cell; | |
3430 | ||
3431 | struct server_state { | |
3432 | const char *server_name; | |
3433 | const char *type; | |
3434 | const char *ifname; | |
0319a28e | 3435 | int ifindex; |
bc837621 KV |
3436 | const char *verified_feature_level; |
3437 | const char *possible_feature_level; | |
3438 | const char *dnssec_mode; | |
a67e5c6e | 3439 | bool dnssec_supported; |
bc837621 KV |
3440 | size_t received_udp_fragment_max; |
3441 | uint64_t n_failed_udp; | |
3442 | uint64_t n_failed_tcp; | |
3443 | bool packet_truncated; | |
3444 | bool packet_bad_opt; | |
3445 | bool packet_rrsig_missing; | |
3446 | bool packet_invalid; | |
3447 | bool packet_do_off; | |
0319a28e LP |
3448 | } server_state = { |
3449 | .ifindex = -1, | |
3450 | }; | |
bc837621 KV |
3451 | |
3452 | int r; | |
3453 | ||
309a747f LP |
3454 | static const sd_json_dispatch_field dispatch_table[] = { |
3455 | { "Server", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, server_name), SD_JSON_MANDATORY }, | |
3456 | { "Type", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, type), SD_JSON_MANDATORY }, | |
3457 | { "Interface", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, ifname), 0 }, | |
c4c04e2c | 3458 | { "InterfaceIndex", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(struct server_state, ifindex), SD_JSON_RELAX }, |
309a747f LP |
3459 | { "VerifiedFeatureLevel", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, verified_feature_level), 0 }, |
3460 | { "PossibleFeatureLevel", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, possible_feature_level), 0 }, | |
3461 | { "DNSSECMode", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct server_state, dnssec_mode), SD_JSON_MANDATORY }, | |
3462 | { "DNSSECSupported", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, dnssec_supported), SD_JSON_MANDATORY }, | |
3463 | { "ReceivedUDPFragmentMax", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct server_state, received_udp_fragment_max), SD_JSON_MANDATORY }, | |
3464 | { "FailedUDPAttempts", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct server_state, n_failed_udp), SD_JSON_MANDATORY }, | |
3465 | { "FailedTCPAttempts", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(struct server_state, n_failed_tcp), SD_JSON_MANDATORY }, | |
3466 | { "PacketTruncated", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_truncated), SD_JSON_MANDATORY }, | |
3467 | { "PacketBadOpt", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_bad_opt), SD_JSON_MANDATORY }, | |
3468 | { "PacketRRSIGMissing", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_rrsig_missing), SD_JSON_MANDATORY }, | |
3469 | { "PacketInvalid", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_invalid), SD_JSON_MANDATORY }, | |
3470 | { "PacketDoOff", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct server_state, packet_do_off), SD_JSON_MANDATORY }, | |
bc837621 KV |
3471 | {}, |
3472 | }; | |
3473 | ||
309a747f | 3474 | r = sd_json_dispatch(server, dispatch_table, SD_JSON_LOG|SD_JSON_ALLOW_EXTENSIONS, &server_state); |
bc837621 KV |
3475 | if (r < 0) |
3476 | return r; | |
3477 | ||
3478 | table = table_new_vertical(); | |
3479 | if (!table) | |
3480 | return log_oom(); | |
3481 | ||
3482 | assert_se(cell = table_get_cell(table, 0, 0)); | |
3483 | (void) table_set_ellipsize_percent(table, cell, 100); | |
3484 | (void) table_set_align_percent(table, cell, 0); | |
3485 | ||
3486 | r = table_add_cell_stringf(table, NULL, "Server: %s", server_state.server_name); | |
3487 | if (r < 0) | |
3488 | return table_log_add_error(r); | |
3489 | ||
3490 | r = table_add_many(table, | |
3491 | TABLE_EMPTY, | |
3492 | TABLE_FIELD, "Type", | |
3493 | TABLE_SET_ALIGN_PERCENT, 100, | |
3494 | TABLE_STRING, server_state.type); | |
3495 | if (r < 0) | |
3496 | return table_log_add_error(r); | |
3497 | ||
3498 | if (server_state.ifname) { | |
3499 | r = table_add_many(table, | |
3500 | TABLE_FIELD, "Interface", | |
bc837621 KV |
3501 | TABLE_STRING, server_state.ifname); |
3502 | if (r < 0) | |
3503 | return table_log_add_error(r); | |
3504 | } | |
3505 | ||
0319a28e LP |
3506 | if (server_state.ifindex >= 0) { |
3507 | r = table_add_many(table, | |
3508 | TABLE_FIELD, "Interface Index", | |
3509 | TABLE_INT, server_state.ifindex); | |
3510 | if (r < 0) | |
3511 | return table_log_add_error(r); | |
3512 | } | |
3513 | ||
bc837621 KV |
3514 | if (server_state.verified_feature_level) { |
3515 | r = table_add_many(table, | |
3516 | TABLE_FIELD, "Verified feature level", | |
3517 | TABLE_STRING, server_state.verified_feature_level); | |
3518 | if (r < 0) | |
3519 | return table_log_add_error(r); | |
3520 | } | |
3521 | ||
3522 | if (server_state.possible_feature_level) { | |
3523 | r = table_add_many(table, | |
3524 | TABLE_FIELD, "Possible feature level", | |
3525 | TABLE_STRING, server_state.possible_feature_level); | |
3526 | if (r < 0) | |
3527 | return table_log_add_error(r); | |
3528 | } | |
3529 | ||
3530 | r = table_add_many(table, | |
3531 | TABLE_FIELD, "DNSSEC Mode", | |
3532 | TABLE_STRING, server_state.dnssec_mode, | |
a67e5c6e KV |
3533 | TABLE_FIELD, "DNSSEC Supported", |
3534 | TABLE_STRING, yes_no(server_state.dnssec_supported), | |
bc837621 KV |
3535 | TABLE_FIELD, "Maximum UDP fragment size received", |
3536 | TABLE_UINT64, server_state.received_udp_fragment_max, | |
3537 | TABLE_FIELD, "Failed UDP attempts", | |
3538 | TABLE_UINT64, server_state.n_failed_udp, | |
3539 | TABLE_FIELD, "Failed TCP attempts", | |
3540 | TABLE_UINT64, server_state.n_failed_tcp, | |
3541 | TABLE_FIELD, "Seen truncated packet", | |
3542 | TABLE_STRING, yes_no(server_state.packet_truncated), | |
3543 | TABLE_FIELD, "Seen OPT RR getting lost", | |
3544 | TABLE_STRING, yes_no(server_state.packet_bad_opt), | |
3545 | TABLE_FIELD, "Seen RRSIG RR missing", | |
3546 | TABLE_STRING, yes_no(server_state.packet_rrsig_missing), | |
3547 | TABLE_FIELD, "Seen invalid packet", | |
3548 | TABLE_STRING, yes_no(server_state.packet_invalid), | |
3549 | TABLE_FIELD, "Server dropped DO flag", | |
3550 | TABLE_STRING, yes_no(server_state.packet_do_off), | |
3551 | TABLE_SET_ALIGN_PERCENT, 0, | |
3552 | TABLE_EMPTY, TABLE_EMPTY); | |
3553 | ||
3554 | if (r < 0) | |
3555 | return table_log_add_error(r); | |
3556 | ||
3557 | r = table_print(table, NULL); | |
3558 | if (r < 0) | |
3559 | return table_log_print_error(r); | |
3560 | ||
3561 | return 0; | |
3562 | } | |
3563 | ||
3564 | static int verb_show_server_state(int argc, char *argv[], void *userdata) { | |
309a747f | 3565 | sd_json_variant *reply = NULL, *d = NULL; |
25ff515b | 3566 | _cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
bc837621 KV |
3567 | int r; |
3568 | ||
cf01bbb7 YW |
3569 | (void) polkit_agent_open_if_enabled(BUS_TRANSPORT_LOCAL, arg_ask_password); |
3570 | ||
25ff515b | 3571 | r = sd_varlink_connect_address(&vl, "/run/systemd/resolve/io.systemd.Resolve.Monitor"); |
bc837621 KV |
3572 | if (r < 0) |
3573 | return log_error_errno(r, "Failed to connect to query monitoring service /run/systemd/resolve/io.systemd.Resolve.Monitor: %m"); | |
3574 | ||
cf01bbb7 YW |
3575 | r = varlink_callbo_and_log( |
3576 | vl, | |
3577 | "io.systemd.Resolve.Monitor.DumpServerState", | |
3578 | &reply, | |
3579 | SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", arg_ask_password)); | |
bc837621 | 3580 | if (r < 0) |
71d0ecc5 | 3581 | return r; |
bc837621 | 3582 | |
309a747f | 3583 | d = sd_json_variant_by_key(reply, "dump"); |
bc837621 KV |
3584 | if (!d) |
3585 | return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), | |
3586 | "DumpCache() response is missing 'dump' key."); | |
3587 | ||
309a747f | 3588 | if (!sd_json_variant_is_array(d)) |
bc837621 KV |
3589 | return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), |
3590 | "DumpCache() response 'dump' field not an array"); | |
3591 | ||
23441a3d | 3592 | if (!sd_json_format_enabled(arg_json_format_flags)) { |
309a747f | 3593 | sd_json_variant *i; |
bc837621 KV |
3594 | |
3595 | JSON_VARIANT_ARRAY_FOREACH(i, d) { | |
3596 | r = dump_server_state(i); | |
3597 | if (r < 0) | |
3598 | return r; | |
3599 | } | |
3600 | ||
3601 | return 0; | |
3602 | } | |
3603 | ||
309a747f | 3604 | return sd_json_variant_dump(d, arg_json_format_flags, NULL, NULL); |
bc837621 KV |
3605 | } |
3606 | ||
a7a4c60a YW |
3607 | static void help_protocol_types(void) { |
3608 | if (arg_legend) | |
3609 | puts("Known protocol types:"); | |
5cf4b2e5 LP |
3610 | puts("dns\n" |
3611 | "llmnr\n" | |
3612 | "llmnr-ipv4\n" | |
3613 | "llmnr-ipv6\n" | |
3614 | "mdns\n" | |
3615 | "mdns-ipv4\n" | |
3616 | "mdns-ipv6"); | |
a7a4c60a YW |
3617 | } |
3618 | ||
3619 | static void help_dns_types(void) { | |
a7a4c60a YW |
3620 | if (arg_legend) |
3621 | puts("Known DNS RR types:"); | |
5c828e66 LP |
3622 | |
3623 | DUMP_STRING_TABLE(dns_type, int, _DNS_TYPE_MAX); | |
b93312f5 ZJS |
3624 | } |
3625 | ||
3626 | static void help_dns_classes(void) { | |
b93312f5 | 3627 | if (arg_legend) |
09b1fe14 | 3628 | puts("Known DNS RR classes:"); |
5c828e66 LP |
3629 | |
3630 | DUMP_STRING_TABLE(dns_class, int, _DNS_CLASS_MAX); | |
b93312f5 ZJS |
3631 | } |
3632 | ||
37ec0fdd LP |
3633 | static int compat_help(void) { |
3634 | _cleanup_free_ char *link = NULL; | |
3635 | int r; | |
3636 | ||
3637 | r = terminal_urlify_man("resolvectl", "1", &link); | |
3638 | if (r < 0) | |
3639 | return log_oom(); | |
3640 | ||
cf71be0a ZJS |
3641 | pager_open(arg_pager_flags); |
3642 | ||
1ace2438 ZJS |
3643 | printf("%1$s [OPTIONS...] HOSTNAME|ADDRESS...\n" |
3644 | "%1$s [OPTIONS...] --service [[NAME] TYPE] DOMAIN\n" | |
3645 | "%1$s [OPTIONS...] --openpgp EMAIL@DOMAIN...\n" | |
3646 | "%1$s [OPTIONS...] --statistics\n" | |
3647 | "%1$s [OPTIONS...] --reset-statistics\n" | |
3648 | "\n" | |
353b2baa | 3649 | "%2$sResolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%3$s\n\n" |
1ace2438 ZJS |
3650 | " -h --help Show this help\n" |
3651 | " --version Show package version\n" | |
be371fe0 | 3652 | " --no-pager Do not pipe output into a pager\n" |
1ace2438 ZJS |
3653 | " -4 Resolve IPv4 addresses\n" |
3654 | " -6 Resolve IPv6 addresses\n" | |
3655 | " -i --interface=INTERFACE Look on interface\n" | |
3656 | " -p --protocol=PROTO|help Look via protocol\n" | |
3657 | " -t --type=TYPE|help Query RR with DNS type\n" | |
3658 | " -c --class=CLASS|help Query RR with DNS class\n" | |
3659 | " --service Resolve service (SRV)\n" | |
3660 | " --service-address=BOOL Resolve address for services (default: yes)\n" | |
3661 | " --service-txt=BOOL Resolve TXT records for services (default: yes)\n" | |
3662 | " --openpgp Query OpenPGP public key\n" | |
82d1d240 | 3663 | " --tlsa Query TLS public key\n" |
1ace2438 ZJS |
3664 | " --cname=BOOL Follow CNAME redirects (default: yes)\n" |
3665 | " --search=BOOL Use search domains for single-label names\n" | |
3666 | " (default: yes)\n" | |
dab48ea6 | 3667 | " --raw[=payload|packet] Dump the answer as binary data\n" |
1ace2438 ZJS |
3668 | " --legend=BOOL Print headers and additional info (default: yes)\n" |
3669 | " --statistics Show resolver statistics\n" | |
3670 | " --reset-statistics Reset resolver statistics\n" | |
be371fe0 | 3671 | " --status Show link and server status\n" |
ba35662f | 3672 | " --flush-caches Flush all local DNS caches\n" |
d55b0463 LP |
3673 | " --reset-server-features\n" |
3674 | " Forget learnt DNS server feature levels\n" | |
14965b94 LP |
3675 | " --set-dns=SERVER Set per-interface DNS server address\n" |
3676 | " --set-domain=DOMAIN Set per-interface search domain\n" | |
3677 | " --set-llmnr=MODE Set per-interface LLMNR mode\n" | |
3678 | " --set-mdns=MODE Set per-interface MulticastDNS mode\n" | |
c9299be2 | 3679 | " --set-dnsovertls=MODE Set per-interface DNS-over-TLS mode\n" |
14965b94 LP |
3680 | " --set-dnssec=MODE Set per-interface DNSSEC mode\n" |
3681 | " --set-nta=DOMAIN Set per-interface DNSSEC NTA\n" | |
3682 | " --revert Revert per-interface configuration\n" | |
bc556335 DDM |
3683 | "\nSee the %4$s for details.\n", |
3684 | program_invocation_short_name, | |
3685 | ansi_highlight(), | |
3686 | ansi_normal(), | |
3687 | link); | |
37ec0fdd LP |
3688 | |
3689 | return 0; | |
bdef7319 ZJS |
3690 | } |
3691 | ||
37ec0fdd LP |
3692 | static int native_help(void) { |
3693 | _cleanup_free_ char *link = NULL; | |
3694 | int r; | |
3695 | ||
3696 | r = terminal_urlify_man("resolvectl", "1", &link); | |
3697 | if (r < 0) | |
3698 | return log_oom(); | |
3699 | ||
cf71be0a ZJS |
3700 | pager_open(arg_pager_flags); |
3701 | ||
700f5b18 | 3702 | printf("%1$s [OPTIONS...] COMMAND ...\n" |
a7a4c60a | 3703 | "\n" |
700f5b18 LP |
3704 | "%5$sSend control commands to the network name resolution manager, or%6$s\n" |
3705 | "%5$sresolve domain names, IPv4 and IPv6 addresses, DNS records, and services.%6$s\n" | |
3706 | "\n%3$sCommands:%4$s\n" | |
a7a4c60a YW |
3707 | " query HOSTNAME|ADDRESS... Resolve domain names, IPv4 and IPv6 addresses\n" |
3708 | " service [[NAME] TYPE] DOMAIN Resolve service (SRV)\n" | |
3709 | " openpgp EMAIL@DOMAIN... Query OpenPGP public key\n" | |
3710 | " tlsa DOMAIN[:PORT]... Query TLS public key\n" | |
3711 | " status [LINK...] Show link and server status\n" | |
3712 | " statistics Show resolver statistics\n" | |
3713 | " reset-statistics Reset resolver statistics\n" | |
3714 | " flush-caches Flush all local DNS caches\n" | |
3715 | " reset-server-features Forget learnt DNS server feature levels\n" | |
fffbf1dc | 3716 | " monitor Monitor DNS queries\n" |
6050e8b5 | 3717 | " show-cache Show cache contents\n" |
bc837621 | 3718 | " show-server-state Show servers state\n" |
a7a4c60a YW |
3719 | " dns [LINK [SERVER...]] Get/set per-interface DNS server address\n" |
3720 | " domain [LINK [DOMAIN...]] Get/set per-interface search domain\n" | |
f2fd3cdb | 3721 | " default-route [LINK [BOOL]] Get/set per-interface default route flag\n" |
a7a4c60a YW |
3722 | " llmnr [LINK [MODE]] Get/set per-interface LLMNR mode\n" |
3723 | " mdns [LINK [MODE]] Get/set per-interface MulticastDNS mode\n" | |
c9299be2 | 3724 | " dnsovertls [LINK [MODE]] Get/set per-interface DNS-over-TLS mode\n" |
a7a4c60a YW |
3725 | " dnssec [LINK [MODE]] Get/set per-interface DNSSEC mode\n" |
3726 | " nta [LINK [DOMAIN...]] Get/set per-interface DNSSEC NTA\n" | |
3727 | " revert LINK Revert per-interface configuration\n" | |
bde4bc9b | 3728 | " log-level [LEVEL] Get/set logging threshold for systemd-resolved\n" |
700f5b18 | 3729 | "\n%3$sOptions:%4$s\n" |
e1fac8a6 ZJS |
3730 | " -h --help Show this help\n" |
3731 | " --version Show package version\n" | |
3732 | " --no-pager Do not pipe output into a pager\n" | |
5703301a | 3733 | " --no-ask-password Do not prompt for password\n" |
e1fac8a6 ZJS |
3734 | " -4 Resolve IPv4 addresses\n" |
3735 | " -6 Resolve IPv6 addresses\n" | |
3736 | " -i --interface=INTERFACE Look on interface\n" | |
3737 | " -p --protocol=PROTO|help Look via protocol\n" | |
3738 | " -t --type=TYPE|help Query RR with DNS type\n" | |
3739 | " -c --class=CLASS|help Query RR with DNS class\n" | |
3740 | " --service-address=BOOL Resolve address for services (default: yes)\n" | |
3741 | " --service-txt=BOOL Resolve TXT records for services (default: yes)\n" | |
3742 | " --cname=BOOL Follow CNAME redirects (default: yes)\n" | |
d711322c LP |
3743 | " --validate=BOOL Allow DNSSEC validation (default: yes)\n" |
3744 | " --synthesize=BOOL Allow synthetic response (default: yes)\n" | |
3745 | " --cache=BOOL Allow response from cache (default: yes)\n" | |
5ed91481 | 3746 | " --stale-data=BOOL Allow response from cache with stale data (default: yes)\n" |
36418a47 | 3747 | " --relax-single-label=BOOL Allow single label lookups to go upstream (default: no)\n" |
d711322c LP |
3748 | " --zone=BOOL Allow response from locally registered mDNS/LLMNR\n" |
3749 | " records (default: yes)\n" | |
fffbf1dc LP |
3750 | " --trust-anchor=BOOL Allow response from local trust anchor (default:\n" |
3751 | " yes)\n" | |
d711322c | 3752 | " --network=BOOL Allow response from network (default: yes)\n" |
fffbf1dc LP |
3753 | " --search=BOOL Use search domains for single-label names (default:\n" |
3754 | " yes)\n" | |
e1fac8a6 ZJS |
3755 | " --raw[=payload|packet] Dump the answer as binary data\n" |
3756 | " --legend=BOOL Print headers and additional info (default: yes)\n" | |
fffbf1dc LP |
3757 | " --json=MODE Output as JSON\n" |
3758 | " -j Same as --json=pretty on tty, --json=short\n" | |
3759 | " otherwise\n" | |
700f5b18 | 3760 | "\nSee the %2$s for details.\n", |
bc556335 | 3761 | program_invocation_short_name, |
700f5b18 LP |
3762 | link, |
3763 | ansi_underline(), | |
bc556335 DDM |
3764 | ansi_normal(), |
3765 | ansi_highlight(), | |
700f5b18 | 3766 | ansi_normal()); |
37ec0fdd LP |
3767 | |
3768 | return 0; | |
a7a4c60a YW |
3769 | } |
3770 | ||
3771 | static int verb_help(int argc, char **argv, void *userdata) { | |
37ec0fdd | 3772 | return native_help(); |
a7a4c60a YW |
3773 | } |
3774 | ||
3775 | static int compat_parse_argv(int argc, char *argv[]) { | |
bdef7319 ZJS |
3776 | enum { |
3777 | ARG_VERSION = 0x100, | |
dad29dff | 3778 | ARG_LEGEND, |
45ec7efb LP |
3779 | ARG_SERVICE, |
3780 | ARG_CNAME, | |
3781 | ARG_SERVICE_ADDRESS, | |
3782 | ARG_SERVICE_TXT, | |
4ac2ca1b | 3783 | ARG_OPENPGP, |
82d1d240 | 3784 | ARG_TLSA, |
2e74028a | 3785 | ARG_RAW, |
801ad6a6 | 3786 | ARG_SEARCH, |
a150ff5e LP |
3787 | ARG_STATISTICS, |
3788 | ARG_RESET_STATISTICS, | |
be371fe0 | 3789 | ARG_STATUS, |
ba35662f | 3790 | ARG_FLUSH_CACHES, |
d55b0463 | 3791 | ARG_RESET_SERVER_FEATURES, |
be371fe0 | 3792 | ARG_NO_PAGER, |
14965b94 LP |
3793 | ARG_SET_DNS, |
3794 | ARG_SET_DOMAIN, | |
3795 | ARG_SET_LLMNR, | |
3796 | ARG_SET_MDNS, | |
b3102c05 | 3797 | ARG_SET_DNS_OVER_TLS, |
14965b94 LP |
3798 | ARG_SET_DNSSEC, |
3799 | ARG_SET_NTA, | |
3800 | ARG_REVERT_LINK, | |
bdef7319 ZJS |
3801 | }; |
3802 | ||
3803 | static const struct option options[] = { | |
d55b0463 LP |
3804 | { "help", no_argument, NULL, 'h' }, |
3805 | { "version", no_argument, NULL, ARG_VERSION }, | |
3806 | { "type", required_argument, NULL, 't' }, | |
3807 | { "class", required_argument, NULL, 'c' }, | |
3808 | { "legend", required_argument, NULL, ARG_LEGEND }, | |
3809 | { "interface", required_argument, NULL, 'i' }, | |
3810 | { "protocol", required_argument, NULL, 'p' }, | |
3811 | { "cname", required_argument, NULL, ARG_CNAME }, | |
3812 | { "service", no_argument, NULL, ARG_SERVICE }, | |
3813 | { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS }, | |
3814 | { "service-txt", required_argument, NULL, ARG_SERVICE_TXT }, | |
3815 | { "openpgp", no_argument, NULL, ARG_OPENPGP }, | |
3816 | { "tlsa", optional_argument, NULL, ARG_TLSA }, | |
3817 | { "raw", optional_argument, NULL, ARG_RAW }, | |
3818 | { "search", required_argument, NULL, ARG_SEARCH }, | |
3819 | { "statistics", no_argument, NULL, ARG_STATISTICS, }, | |
3820 | { "reset-statistics", no_argument, NULL, ARG_RESET_STATISTICS }, | |
3821 | { "status", no_argument, NULL, ARG_STATUS }, | |
3822 | { "flush-caches", no_argument, NULL, ARG_FLUSH_CACHES }, | |
3823 | { "reset-server-features", no_argument, NULL, ARG_RESET_SERVER_FEATURES }, | |
3824 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
14965b94 LP |
3825 | { "set-dns", required_argument, NULL, ARG_SET_DNS }, |
3826 | { "set-domain", required_argument, NULL, ARG_SET_DOMAIN }, | |
3827 | { "set-llmnr", required_argument, NULL, ARG_SET_LLMNR }, | |
3828 | { "set-mdns", required_argument, NULL, ARG_SET_MDNS }, | |
b3102c05 | 3829 | { "set-dnsovertls", required_argument, NULL, ARG_SET_DNS_OVER_TLS }, |
14965b94 LP |
3830 | { "set-dnssec", required_argument, NULL, ARG_SET_DNSSEC }, |
3831 | { "set-nta", required_argument, NULL, ARG_SET_NTA }, | |
3832 | { "revert", no_argument, NULL, ARG_REVERT_LINK }, | |
bdef7319 ZJS |
3833 | {} |
3834 | }; | |
3835 | ||
2d4c5cbc | 3836 | int c, r; |
bdef7319 ZJS |
3837 | |
3838 | assert(argc >= 0); | |
3839 | assert(argv); | |
3840 | ||
51323288 | 3841 | while ((c = getopt_long(argc, argv, "h46i:t:c:p:", options, NULL)) >= 0) |
79893116 | 3842 | switch (c) { |
bdef7319 ZJS |
3843 | |
3844 | case 'h': | |
37ec0fdd | 3845 | return compat_help(); |
bdef7319 ZJS |
3846 | |
3847 | case ARG_VERSION: | |
3f6fd1ba | 3848 | return version(); |
bdef7319 ZJS |
3849 | |
3850 | case '4': | |
3851 | arg_family = AF_INET; | |
3852 | break; | |
3853 | ||
3854 | case '6': | |
3855 | arg_family = AF_INET6; | |
3856 | break; | |
3857 | ||
27d8af3e | 3858 | case 'i': |
df87a53d | 3859 | r = ifname_mangle(optarg); |
a7a4c60a YW |
3860 | if (r < 0) |
3861 | return r; | |
2d4c5cbc LP |
3862 | break; |
3863 | ||
3864 | case 't': | |
b93312f5 ZJS |
3865 | if (streq(optarg, "help")) { |
3866 | help_dns_types(); | |
3867 | return 0; | |
3868 | } | |
3869 | ||
4b548ef3 | 3870 | r = dns_type_from_string(optarg); |
7211c853 ZJS |
3871 | if (r < 0) |
3872 | return log_error_errno(r, "Failed to parse RR record type %s: %m", optarg); | |
3873 | ||
4b548ef3 LP |
3874 | arg_type = (uint16_t) r; |
3875 | assert((int) arg_type == r); | |
b93312f5 | 3876 | |
a150ff5e | 3877 | arg_mode = MODE_RESOLVE_RECORD; |
2d4c5cbc LP |
3878 | break; |
3879 | ||
3880 | case 'c': | |
b93312f5 ZJS |
3881 | if (streq(optarg, "help")) { |
3882 | help_dns_classes(); | |
3883 | return 0; | |
3884 | } | |
3885 | ||
4b548ef3 | 3886 | r = dns_class_from_string(optarg); |
7211c853 ZJS |
3887 | if (r < 0) |
3888 | return log_error_errno(r, "Failed to parse RR record class %s: %m", optarg); | |
3889 | ||
4b548ef3 LP |
3890 | arg_class = (uint16_t) r; |
3891 | assert((int) arg_class == r); | |
b93312f5 ZJS |
3892 | |
3893 | break; | |
3894 | ||
dad29dff | 3895 | case ARG_LEGEND: |
599c7c54 | 3896 | r = parse_boolean_argument("--legend=", optarg, &arg_legend); |
45ec7efb | 3897 | if (r < 0) |
599c7c54 | 3898 | return r; |
bdef7319 ZJS |
3899 | break; |
3900 | ||
51323288 | 3901 | case 'p': |
ba82da3b ZJS |
3902 | if (streq(optarg, "help")) { |
3903 | help_protocol_types(); | |
3904 | return 0; | |
3905 | } else if (streq(optarg, "dns")) | |
51323288 LP |
3906 | arg_flags |= SD_RESOLVED_DNS; |
3907 | else if (streq(optarg, "llmnr")) | |
3908 | arg_flags |= SD_RESOLVED_LLMNR; | |
3909 | else if (streq(optarg, "llmnr-ipv4")) | |
3910 | arg_flags |= SD_RESOLVED_LLMNR_IPV4; | |
3911 | else if (streq(optarg, "llmnr-ipv6")) | |
3912 | arg_flags |= SD_RESOLVED_LLMNR_IPV6; | |
062aabb9 DR |
3913 | else if (streq(optarg, "mdns")) |
3914 | arg_flags |= SD_RESOLVED_MDNS; | |
3915 | else if (streq(optarg, "mdns-ipv4")) | |
3916 | arg_flags |= SD_RESOLVED_MDNS_IPV4; | |
3917 | else if (streq(optarg, "mdns-ipv6")) | |
3918 | arg_flags |= SD_RESOLVED_MDNS_IPV6; | |
baaa35ad ZJS |
3919 | else |
3920 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3921 | "Unknown protocol specifier: %s", optarg); | |
51323288 LP |
3922 | |
3923 | break; | |
3924 | ||
45ec7efb | 3925 | case ARG_SERVICE: |
a150ff5e | 3926 | arg_mode = MODE_RESOLVE_SERVICE; |
45ec7efb LP |
3927 | break; |
3928 | ||
4ac2ca1b ZJS |
3929 | case ARG_OPENPGP: |
3930 | arg_mode = MODE_RESOLVE_OPENPGP; | |
3931 | break; | |
3932 | ||
82d1d240 ZJS |
3933 | case ARG_TLSA: |
3934 | arg_mode = MODE_RESOLVE_TLSA; | |
ebbc70e5 YW |
3935 | if (!optarg || service_family_is_valid(optarg)) |
3936 | arg_service_family = optarg; | |
baaa35ad ZJS |
3937 | else |
3938 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3939 | "Unknown service family \"%s\".", optarg); | |
82d1d240 ZJS |
3940 | break; |
3941 | ||
2e74028a | 3942 | case ARG_RAW: |
baaa35ad ZJS |
3943 | if (on_tty()) |
3944 | return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), | |
3945 | "Refusing to write binary data to tty."); | |
2e74028a | 3946 | |
dab48ea6 ZJS |
3947 | if (optarg == NULL || streq(optarg, "payload")) |
3948 | arg_raw = RAW_PAYLOAD; | |
3949 | else if (streq(optarg, "packet")) | |
3950 | arg_raw = RAW_PACKET; | |
baaa35ad ZJS |
3951 | else |
3952 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
3953 | "Unknown --raw specifier \"%s\".", | |
3954 | optarg); | |
dab48ea6 | 3955 | |
2e74028a ZJS |
3956 | arg_legend = false; |
3957 | break; | |
3958 | ||
45ec7efb | 3959 | case ARG_CNAME: |
c3470872 | 3960 | r = parse_boolean_argument("--cname=", optarg, NULL); |
45ec7efb | 3961 | if (r < 0) |
c3470872 | 3962 | return r; |
5883ff60 | 3963 | SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0); |
45ec7efb LP |
3964 | break; |
3965 | ||
3966 | case ARG_SERVICE_ADDRESS: | |
c3470872 | 3967 | r = parse_boolean_argument("--service-address=", optarg, NULL); |
45ec7efb | 3968 | if (r < 0) |
c3470872 | 3969 | return r; |
5883ff60 | 3970 | SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0); |
45ec7efb LP |
3971 | break; |
3972 | ||
3973 | case ARG_SERVICE_TXT: | |
c3470872 | 3974 | r = parse_boolean_argument("--service-txt=", optarg, NULL); |
45ec7efb | 3975 | if (r < 0) |
c3470872 | 3976 | return r; |
5883ff60 | 3977 | SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0); |
45ec7efb LP |
3978 | break; |
3979 | ||
801ad6a6 | 3980 | case ARG_SEARCH: |
c3470872 | 3981 | r = parse_boolean_argument("--search=", optarg, NULL); |
801ad6a6 | 3982 | if (r < 0) |
c3470872 | 3983 | return r; |
5883ff60 | 3984 | SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0); |
801ad6a6 LP |
3985 | break; |
3986 | ||
a150ff5e LP |
3987 | case ARG_STATISTICS: |
3988 | arg_mode = MODE_STATISTICS; | |
3989 | break; | |
3990 | ||
3991 | case ARG_RESET_STATISTICS: | |
3992 | arg_mode = MODE_RESET_STATISTICS; | |
3993 | break; | |
3994 | ||
ba35662f LP |
3995 | case ARG_FLUSH_CACHES: |
3996 | arg_mode = MODE_FLUSH_CACHES; | |
3997 | break; | |
3998 | ||
d55b0463 LP |
3999 | case ARG_RESET_SERVER_FEATURES: |
4000 | arg_mode = MODE_RESET_SERVER_FEATURES; | |
4001 | break; | |
4002 | ||
be371fe0 LP |
4003 | case ARG_STATUS: |
4004 | arg_mode = MODE_STATUS; | |
4005 | break; | |
4006 | ||
4007 | case ARG_NO_PAGER: | |
0221d68a | 4008 | arg_pager_flags |= PAGER_DISABLE; |
be371fe0 LP |
4009 | break; |
4010 | ||
a7a4c60a YW |
4011 | case ARG_SET_DNS: |
4012 | r = strv_extend(&arg_set_dns, optarg); | |
14965b94 | 4013 | if (r < 0) |
14965b94 | 4014 | return log_oom(); |
14965b94 | 4015 | |
14965b94 LP |
4016 | arg_mode = MODE_SET_LINK; |
4017 | break; | |
14965b94 | 4018 | |
a7a4c60a | 4019 | case ARG_SET_DOMAIN: |
14965b94 LP |
4020 | r = strv_extend(&arg_set_domain, optarg); |
4021 | if (r < 0) | |
4022 | return log_oom(); | |
4023 | ||
4024 | arg_mode = MODE_SET_LINK; | |
4025 | break; | |
14965b94 LP |
4026 | |
4027 | case ARG_SET_LLMNR: | |
a7a4c60a | 4028 | arg_set_llmnr = optarg; |
14965b94 LP |
4029 | arg_mode = MODE_SET_LINK; |
4030 | break; | |
4031 | ||
4032 | case ARG_SET_MDNS: | |
a7a4c60a | 4033 | arg_set_mdns = optarg; |
14965b94 LP |
4034 | arg_mode = MODE_SET_LINK; |
4035 | break; | |
4036 | ||
b3102c05 | 4037 | case ARG_SET_DNS_OVER_TLS: |
c9299be2 | 4038 | arg_set_dns_over_tls = optarg; |
d050561a IT |
4039 | arg_mode = MODE_SET_LINK; |
4040 | break; | |
4041 | ||
14965b94 | 4042 | case ARG_SET_DNSSEC: |
a7a4c60a | 4043 | arg_set_dnssec = optarg; |
14965b94 LP |
4044 | arg_mode = MODE_SET_LINK; |
4045 | break; | |
4046 | ||
4047 | case ARG_SET_NTA: | |
14965b94 LP |
4048 | r = strv_extend(&arg_set_nta, optarg); |
4049 | if (r < 0) | |
4050 | return log_oom(); | |
4051 | ||
4052 | arg_mode = MODE_SET_LINK; | |
4053 | break; | |
4054 | ||
4055 | case ARG_REVERT_LINK: | |
4056 | arg_mode = MODE_REVERT_LINK; | |
4057 | break; | |
4058 | ||
bdef7319 ZJS |
4059 | case '?': |
4060 | return -EINVAL; | |
4061 | ||
4062 | default: | |
04499a70 | 4063 | assert_not_reached(); |
bdef7319 ZJS |
4064 | } |
4065 | ||
baaa35ad ZJS |
4066 | if (arg_type == 0 && arg_class != 0) |
4067 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4068 | "--class= may only be used in conjunction with --type=."); | |
45ec7efb | 4069 | |
baaa35ad ZJS |
4070 | if (arg_type != 0 && arg_mode == MODE_RESOLVE_SERVICE) |
4071 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4072 | "--service and --type= may not be combined."); | |
2d4c5cbc LP |
4073 | |
4074 | if (arg_type != 0 && arg_class == 0) | |
4075 | arg_class = DNS_CLASS_IN; | |
4076 | ||
c1dafe4f LP |
4077 | if (arg_class != 0 && arg_type == 0) |
4078 | arg_type = DNS_TYPE_A; | |
4079 | ||
14965b94 LP |
4080 | if (IN_SET(arg_mode, MODE_SET_LINK, MODE_REVERT_LINK)) { |
4081 | ||
baaa35ad ZJS |
4082 | if (arg_ifindex <= 0) |
4083 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4084 | "--set-dns=, --set-domain=, --set-llmnr=, --set-mdns=, --set-dnsovertls=, --set-dnssec=, --set-nta= and --revert require --interface=."); | |
14965b94 LP |
4085 | } |
4086 | ||
bdef7319 ZJS |
4087 | return 1 /* work to do */; |
4088 | } | |
4089 | ||
a7a4c60a YW |
4090 | static int native_parse_argv(int argc, char *argv[]) { |
4091 | enum { | |
4092 | ARG_VERSION = 0x100, | |
4093 | ARG_LEGEND, | |
4094 | ARG_CNAME, | |
d711322c LP |
4095 | ARG_VALIDATE, |
4096 | ARG_SYNTHESIZE, | |
4097 | ARG_CACHE, | |
4098 | ARG_ZONE, | |
4099 | ARG_TRUST_ANCHOR, | |
4100 | ARG_NETWORK, | |
a7a4c60a YW |
4101 | ARG_SERVICE_ADDRESS, |
4102 | ARG_SERVICE_TXT, | |
4103 | ARG_RAW, | |
4104 | ARG_SEARCH, | |
4105 | ARG_NO_PAGER, | |
5703301a | 4106 | ARG_NO_ASK_PASSWORD, |
fffbf1dc | 4107 | ARG_JSON, |
36418a47 LP |
4108 | ARG_STALE_DATA, |
4109 | ARG_RELAX_SINGLE_LABEL, | |
a7a4c60a | 4110 | }; |
bdef7319 | 4111 | |
a7a4c60a YW |
4112 | static const struct option options[] = { |
4113 | { "help", no_argument, NULL, 'h' }, | |
4114 | { "version", no_argument, NULL, ARG_VERSION }, | |
4115 | { "type", required_argument, NULL, 't' }, | |
4116 | { "class", required_argument, NULL, 'c' }, | |
4117 | { "legend", required_argument, NULL, ARG_LEGEND }, | |
4118 | { "interface", required_argument, NULL, 'i' }, | |
4119 | { "protocol", required_argument, NULL, 'p' }, | |
4120 | { "cname", required_argument, NULL, ARG_CNAME }, | |
d711322c LP |
4121 | { "validate", required_argument, NULL, ARG_VALIDATE }, |
4122 | { "synthesize", required_argument, NULL, ARG_SYNTHESIZE }, | |
4123 | { "cache", required_argument, NULL, ARG_CACHE }, | |
4124 | { "zone", required_argument, NULL, ARG_ZONE }, | |
4125 | { "trust-anchor", required_argument, NULL, ARG_TRUST_ANCHOR }, | |
4126 | { "network", required_argument, NULL, ARG_NETWORK }, | |
a7a4c60a YW |
4127 | { "service-address", required_argument, NULL, ARG_SERVICE_ADDRESS }, |
4128 | { "service-txt", required_argument, NULL, ARG_SERVICE_TXT }, | |
4129 | { "raw", optional_argument, NULL, ARG_RAW }, | |
4130 | { "search", required_argument, NULL, ARG_SEARCH }, | |
4131 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
5703301a | 4132 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, |
fffbf1dc | 4133 | { "json", required_argument, NULL, ARG_JSON }, |
5ed91481 | 4134 | { "stale-data", required_argument, NULL, ARG_STALE_DATA }, |
36418a47 | 4135 | { "relax-single-label", required_argument, NULL, ARG_RELAX_SINGLE_LABEL }, |
a7a4c60a YW |
4136 | {} |
4137 | }; | |
bdef7319 | 4138 | |
a7a4c60a | 4139 | int c, r; |
79266746 | 4140 | |
a7a4c60a YW |
4141 | assert(argc >= 0); |
4142 | assert(argv); | |
bdef7319 | 4143 | |
fffbf1dc | 4144 | while ((c = getopt_long(argc, argv, "h46i:t:c:p:j", options, NULL)) >= 0) |
79893116 | 4145 | switch (c) { |
45ec7efb | 4146 | |
a7a4c60a | 4147 | case 'h': |
37ec0fdd | 4148 | return native_help(); |
a7a4c60a YW |
4149 | |
4150 | case ARG_VERSION: | |
4151 | return version(); | |
a150ff5e | 4152 | |
a7a4c60a YW |
4153 | case '4': |
4154 | arg_family = AF_INET; | |
4155 | break; | |
a150ff5e | 4156 | |
a7a4c60a YW |
4157 | case '6': |
4158 | arg_family = AF_INET6; | |
4159 | break; | |
4160 | ||
4161 | case 'i': | |
df87a53d | 4162 | r = ifname_mangle(optarg); |
a7a4c60a YW |
4163 | if (r < 0) |
4164 | return r; | |
a7a4c60a YW |
4165 | break; |
4166 | ||
4167 | case 't': | |
4168 | if (streq(optarg, "help")) { | |
4169 | help_dns_types(); | |
4170 | return 0; | |
4171 | } | |
4172 | ||
4173 | r = dns_type_from_string(optarg); | |
7211c853 ZJS |
4174 | if (r < 0) |
4175 | return log_error_errno(r, "Failed to parse RR record type %s: %m", optarg); | |
4176 | ||
a7a4c60a YW |
4177 | arg_type = (uint16_t) r; |
4178 | assert((int) arg_type == r); | |
4179 | ||
4180 | break; | |
4181 | ||
4182 | case 'c': | |
4183 | if (streq(optarg, "help")) { | |
4184 | help_dns_classes(); | |
4185 | return 0; | |
4186 | } | |
4187 | ||
4188 | r = dns_class_from_string(optarg); | |
7211c853 ZJS |
4189 | if (r < 0) |
4190 | return log_error_errno(r, "Failed to parse RR record class %s: %m", optarg); | |
4191 | ||
a7a4c60a YW |
4192 | arg_class = (uint16_t) r; |
4193 | assert((int) arg_class == r); | |
4194 | ||
4195 | break; | |
4196 | ||
4197 | case ARG_LEGEND: | |
599c7c54 | 4198 | r = parse_boolean_argument("--legend=", optarg, &arg_legend); |
a7a4c60a | 4199 | if (r < 0) |
599c7c54 | 4200 | return r; |
a7a4c60a YW |
4201 | break; |
4202 | ||
4203 | case 'p': | |
4204 | if (streq(optarg, "help")) { | |
4205 | help_protocol_types(); | |
4206 | return 0; | |
4207 | } else if (streq(optarg, "dns")) | |
4208 | arg_flags |= SD_RESOLVED_DNS; | |
4209 | else if (streq(optarg, "llmnr")) | |
4210 | arg_flags |= SD_RESOLVED_LLMNR; | |
4211 | else if (streq(optarg, "llmnr-ipv4")) | |
4212 | arg_flags |= SD_RESOLVED_LLMNR_IPV4; | |
4213 | else if (streq(optarg, "llmnr-ipv6")) | |
4214 | arg_flags |= SD_RESOLVED_LLMNR_IPV6; | |
4215 | else if (streq(optarg, "mdns")) | |
4216 | arg_flags |= SD_RESOLVED_MDNS; | |
4217 | else if (streq(optarg, "mdns-ipv4")) | |
4218 | arg_flags |= SD_RESOLVED_MDNS_IPV4; | |
4219 | else if (streq(optarg, "mdns-ipv6")) | |
4220 | arg_flags |= SD_RESOLVED_MDNS_IPV6; | |
baaa35ad ZJS |
4221 | else |
4222 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4223 | "Unknown protocol specifier: %s", | |
4224 | optarg); | |
a150ff5e | 4225 | |
a7a4c60a | 4226 | break; |
a150ff5e | 4227 | |
a7a4c60a | 4228 | case ARG_RAW: |
baaa35ad ZJS |
4229 | if (on_tty()) |
4230 | return log_error_errno(SYNTHETIC_ERRNO(ENOTTY), | |
4231 | "Refusing to write binary data to tty."); | |
a150ff5e | 4232 | |
a7a4c60a YW |
4233 | if (optarg == NULL || streq(optarg, "payload")) |
4234 | arg_raw = RAW_PAYLOAD; | |
4235 | else if (streq(optarg, "packet")) | |
4236 | arg_raw = RAW_PACKET; | |
baaa35ad ZJS |
4237 | else |
4238 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4239 | "Unknown --raw specifier \"%s\".", | |
4240 | optarg); | |
a150ff5e | 4241 | |
a7a4c60a YW |
4242 | arg_legend = false; |
4243 | break; | |
a150ff5e | 4244 | |
a7a4c60a | 4245 | case ARG_CNAME: |
c3470872 | 4246 | r = parse_boolean_argument("--cname=", optarg, NULL); |
a7a4c60a | 4247 | if (r < 0) |
c3470872 | 4248 | return r; |
a7a4c60a YW |
4249 | SET_FLAG(arg_flags, SD_RESOLVED_NO_CNAME, r == 0); |
4250 | break; | |
a150ff5e | 4251 | |
d711322c | 4252 | case ARG_VALIDATE: |
c3470872 | 4253 | r = parse_boolean_argument("--validate=", optarg, NULL); |
d711322c | 4254 | if (r < 0) |
c3470872 | 4255 | return r; |
d711322c LP |
4256 | SET_FLAG(arg_flags, SD_RESOLVED_NO_VALIDATE, r == 0); |
4257 | break; | |
4258 | ||
4259 | case ARG_SYNTHESIZE: | |
c3470872 | 4260 | r = parse_boolean_argument("--synthesize=", optarg, NULL); |
d711322c | 4261 | if (r < 0) |
c3470872 | 4262 | return r; |
d711322c LP |
4263 | SET_FLAG(arg_flags, SD_RESOLVED_NO_SYNTHESIZE, r == 0); |
4264 | break; | |
4265 | ||
4266 | case ARG_CACHE: | |
c3470872 | 4267 | r = parse_boolean_argument("--cache=", optarg, NULL); |
d711322c | 4268 | if (r < 0) |
c3470872 | 4269 | return r; |
d711322c LP |
4270 | SET_FLAG(arg_flags, SD_RESOLVED_NO_CACHE, r == 0); |
4271 | break; | |
4272 | ||
5ed91481 KV |
4273 | case ARG_STALE_DATA: |
4274 | r = parse_boolean_argument("--stale-data=", optarg, NULL); | |
4275 | if (r < 0) | |
4276 | return r; | |
4277 | SET_FLAG(arg_flags, SD_RESOLVED_NO_STALE, r == 0); | |
4278 | break; | |
4279 | ||
d711322c | 4280 | case ARG_ZONE: |
c3470872 | 4281 | r = parse_boolean_argument("--zone=", optarg, NULL); |
d711322c | 4282 | if (r < 0) |
c3470872 | 4283 | return r; |
d711322c LP |
4284 | SET_FLAG(arg_flags, SD_RESOLVED_NO_ZONE, r == 0); |
4285 | break; | |
4286 | ||
4287 | case ARG_TRUST_ANCHOR: | |
c3470872 | 4288 | r = parse_boolean_argument("--trust-anchor=", optarg, NULL); |
d711322c | 4289 | if (r < 0) |
c3470872 | 4290 | return r; |
d711322c LP |
4291 | SET_FLAG(arg_flags, SD_RESOLVED_NO_TRUST_ANCHOR, r == 0); |
4292 | break; | |
4293 | ||
4294 | case ARG_NETWORK: | |
c3470872 | 4295 | r = parse_boolean_argument("--network=", optarg, NULL); |
d711322c | 4296 | if (r < 0) |
c3470872 | 4297 | return r; |
d711322c LP |
4298 | SET_FLAG(arg_flags, SD_RESOLVED_NO_NETWORK, r == 0); |
4299 | break; | |
4300 | ||
a7a4c60a | 4301 | case ARG_SERVICE_ADDRESS: |
c3470872 | 4302 | r = parse_boolean_argument("--service-address=", optarg, NULL); |
a7a4c60a | 4303 | if (r < 0) |
c3470872 | 4304 | return r; |
a7a4c60a YW |
4305 | SET_FLAG(arg_flags, SD_RESOLVED_NO_ADDRESS, r == 0); |
4306 | break; | |
a150ff5e | 4307 | |
a7a4c60a | 4308 | case ARG_SERVICE_TXT: |
c3470872 | 4309 | r = parse_boolean_argument("--service-txt=", optarg, NULL); |
a7a4c60a | 4310 | if (r < 0) |
c3470872 | 4311 | return r; |
a7a4c60a YW |
4312 | SET_FLAG(arg_flags, SD_RESOLVED_NO_TXT, r == 0); |
4313 | break; | |
45ec7efb | 4314 | |
a7a4c60a | 4315 | case ARG_SEARCH: |
c3470872 | 4316 | r = parse_boolean_argument("--search=", optarg, NULL); |
a7a4c60a | 4317 | if (r < 0) |
c3470872 | 4318 | return r; |
a7a4c60a YW |
4319 | SET_FLAG(arg_flags, SD_RESOLVED_NO_SEARCH, r == 0); |
4320 | break; | |
36418a47 LP |
4321 | |
4322 | case ARG_RELAX_SINGLE_LABEL: | |
4323 | r = parse_boolean_argument("--relax-single-label=", optarg, NULL); | |
4324 | if (r < 0) | |
4325 | return r; | |
4326 | SET_FLAG(arg_flags, SD_RESOLVED_RELAX_SINGLE_LABEL, r > 0); | |
4327 | break; | |
79266746 | 4328 | |
a7a4c60a | 4329 | case ARG_NO_PAGER: |
0221d68a | 4330 | arg_pager_flags |= PAGER_DISABLE; |
a7a4c60a YW |
4331 | break; |
4332 | ||
5703301a YW |
4333 | case ARG_NO_ASK_PASSWORD: |
4334 | arg_ask_password = false; | |
4335 | break; | |
4336 | ||
fffbf1dc LP |
4337 | case ARG_JSON: |
4338 | r = parse_json_argument(optarg, &arg_json_format_flags); | |
4339 | if (r <= 0) | |
4340 | return r; | |
4341 | ||
4342 | break; | |
4343 | ||
4344 | case 'j': | |
309a747f | 4345 | arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO; |
fffbf1dc LP |
4346 | break; |
4347 | ||
a7a4c60a YW |
4348 | case '?': |
4349 | return -EINVAL; | |
4ac2ca1b | 4350 | |
a7a4c60a | 4351 | default: |
04499a70 | 4352 | assert_not_reached(); |
4ac2ca1b ZJS |
4353 | } |
4354 | ||
baaa35ad ZJS |
4355 | if (arg_type == 0 && arg_class != 0) |
4356 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
4357 | "--class= may only be used in conjunction with --type=."); | |
4ac2ca1b | 4358 | |
a7a4c60a YW |
4359 | if (arg_type != 0 && arg_class == 0) |
4360 | arg_class = DNS_CLASS_IN; | |
4ac2ca1b | 4361 | |
a7a4c60a YW |
4362 | if (arg_class != 0 && arg_type == 0) |
4363 | arg_type = DNS_TYPE_A; | |
82d1d240 | 4364 | |
a7a4c60a YW |
4365 | return 1 /* work to do */; |
4366 | } | |
82d1d240 | 4367 | |
614a6770 | 4368 | static int native_main(int argc, char *argv[]) { |
a7a4c60a YW |
4369 | |
4370 | static const Verb verbs[] = { | |
4371 | { "help", VERB_ANY, VERB_ANY, 0, verb_help }, | |
4372 | { "status", VERB_ANY, VERB_ANY, VERB_DEFAULT, verb_status }, | |
4373 | { "query", 2, VERB_ANY, 0, verb_query }, | |
4374 | { "service", 2, 4, 0, verb_service }, | |
4375 | { "openpgp", 2, VERB_ANY, 0, verb_openpgp }, | |
4376 | { "tlsa", 2, VERB_ANY, 0, verb_tlsa }, | |
4377 | { "statistics", VERB_ANY, 1, 0, show_statistics }, | |
4378 | { "reset-statistics", VERB_ANY, 1, 0, reset_statistics }, | |
4379 | { "flush-caches", VERB_ANY, 1, 0, flush_caches }, | |
4380 | { "reset-server-features", VERB_ANY, 1, 0, reset_server_features }, | |
4381 | { "dns", VERB_ANY, VERB_ANY, 0, verb_dns }, | |
4382 | { "domain", VERB_ANY, VERB_ANY, 0, verb_domain }, | |
f2fd3cdb | 4383 | { "default-route", VERB_ANY, 3, 0, verb_default_route }, |
a7a4c60a YW |
4384 | { "llmnr", VERB_ANY, 3, 0, verb_llmnr }, |
4385 | { "mdns", VERB_ANY, 3, 0, verb_mdns }, | |
efe55c81 | 4386 | { "dnsovertls", VERB_ANY, 3, 0, verb_dns_over_tls }, |
a7a4c60a YW |
4387 | { "dnssec", VERB_ANY, 3, 0, verb_dnssec }, |
4388 | { "nta", VERB_ANY, VERB_ANY, 0, verb_nta }, | |
d1293049 | 4389 | { "revert", VERB_ANY, 2, 0, verb_revert_link }, |
df957849 | 4390 | { "log-level", VERB_ANY, 2, 0, verb_log_level }, |
fffbf1dc | 4391 | { "monitor", VERB_ANY, 1, 0, verb_monitor }, |
6050e8b5 | 4392 | { "show-cache", VERB_ANY, 1, 0, verb_show_cache }, |
bc837621 | 4393 | { "show-server-state", VERB_ANY, 1, 0, verb_show_server_state}, |
a7a4c60a YW |
4394 | {} |
4395 | }; | |
82d1d240 | 4396 | |
614a6770 | 4397 | return dispatch_verb(argc, argv, verbs, /* userdata = */ NULL); |
a7a4c60a | 4398 | } |
82d1d240 | 4399 | |
614a6770 | 4400 | static int translate(const char *verb, const char *single_arg, size_t num_args, char **args) { |
a7a4c60a | 4401 | char **fake, **p; |
c9d243cd | 4402 | size_t num; |
bdef7319 | 4403 | |
a7a4c60a YW |
4404 | assert(verb); |
4405 | assert(num_args == 0 || args); | |
a150ff5e | 4406 | |
a7a4c60a | 4407 | num = !!single_arg + num_args + 1; |
79266746 | 4408 | |
a7a4c60a YW |
4409 | p = fake = newa0(char *, num + 1); |
4410 | *p++ = (char *) verb; | |
4411 | if (single_arg) | |
4412 | *p++ = (char *) single_arg; | |
f75ecb9f YW |
4413 | FOREACH_ARRAY(arg, args, num_args) |
4414 | *p++ = *arg; | |
ba35662f | 4415 | |
a7a4c60a | 4416 | optind = 0; |
614a6770 | 4417 | return native_main((int) num, fake); |
a7a4c60a | 4418 | } |
ba35662f | 4419 | |
614a6770 | 4420 | static int compat_main(int argc, char *argv[]) { |
a7a4c60a | 4421 | int r = 0; |
be371fe0 | 4422 | |
a7a4c60a YW |
4423 | switch (arg_mode) { |
4424 | case MODE_RESOLVE_HOST: | |
4425 | case MODE_RESOLVE_RECORD: | |
614a6770 | 4426 | return translate("query", NULL, argc - optind, argv + optind); |
d55b0463 | 4427 | |
a7a4c60a | 4428 | case MODE_RESOLVE_SERVICE: |
614a6770 | 4429 | return translate("service", NULL, argc - optind, argv + optind); |
d55b0463 | 4430 | |
a7a4c60a | 4431 | case MODE_RESOLVE_OPENPGP: |
614a6770 | 4432 | return translate("openpgp", NULL, argc - optind, argv + optind); |
be371fe0 | 4433 | |
a7a4c60a | 4434 | case MODE_RESOLVE_TLSA: |
614a6770 | 4435 | return translate("tlsa", arg_service_family, argc - optind, argv + optind); |
be371fe0 | 4436 | |
a7a4c60a | 4437 | case MODE_STATISTICS: |
614a6770 | 4438 | return translate("statistics", NULL, 0, NULL); |
a7a4c60a YW |
4439 | |
4440 | case MODE_RESET_STATISTICS: | |
614a6770 | 4441 | return translate("reset-statistics", NULL, 0, NULL); |
a7a4c60a YW |
4442 | |
4443 | case MODE_FLUSH_CACHES: | |
614a6770 | 4444 | return translate("flush-caches", NULL, 0, NULL); |
a7a4c60a YW |
4445 | |
4446 | case MODE_RESET_SERVER_FEATURES: | |
614a6770 | 4447 | return translate("reset-server-features", NULL, 0, NULL); |
be371fe0 | 4448 | |
a7a4c60a | 4449 | case MODE_STATUS: |
614a6770 | 4450 | return translate("status", NULL, argc - optind, argv + optind); |
14965b94 | 4451 | |
14965b94 | 4452 | case MODE_SET_LINK: |
a661dc36 YW |
4453 | assert(arg_ifname); |
4454 | ||
374825ec YW |
4455 | if (arg_disable_default_route) { |
4456 | r = translate("default-route", arg_ifname, 1, STRV_MAKE("no")); | |
4457 | if (r < 0) | |
4458 | return r; | |
4459 | } | |
4460 | ||
a7a4c60a | 4461 | if (arg_set_dns) { |
614a6770 | 4462 | r = translate("dns", arg_ifname, strv_length(arg_set_dns), arg_set_dns); |
a7a4c60a YW |
4463 | if (r < 0) |
4464 | return r; | |
4465 | } | |
4466 | ||
4467 | if (arg_set_domain) { | |
614a6770 | 4468 | r = translate("domain", arg_ifname, strv_length(arg_set_domain), arg_set_domain); |
a7a4c60a YW |
4469 | if (r < 0) |
4470 | return r; | |
14965b94 LP |
4471 | } |
4472 | ||
a7a4c60a | 4473 | if (arg_set_nta) { |
614a6770 | 4474 | r = translate("nta", arg_ifname, strv_length(arg_set_nta), arg_set_nta); |
a7a4c60a YW |
4475 | if (r < 0) |
4476 | return r; | |
4477 | } | |
14965b94 | 4478 | |
a7a4c60a | 4479 | if (arg_set_llmnr) { |
614a6770 | 4480 | r = translate("llmnr", arg_ifname, 1, (char **) &arg_set_llmnr); |
a7a4c60a YW |
4481 | if (r < 0) |
4482 | return r; | |
4483 | } | |
4484 | ||
4485 | if (arg_set_mdns) { | |
614a6770 | 4486 | r = translate("mdns", arg_ifname, 1, (char **) &arg_set_mdns); |
a7a4c60a YW |
4487 | if (r < 0) |
4488 | return r; | |
4489 | } | |
4490 | ||
c9299be2 | 4491 | if (arg_set_dns_over_tls) { |
614a6770 | 4492 | r = translate("dnsovertls", arg_ifname, 1, (char **) &arg_set_dns_over_tls); |
d050561a IT |
4493 | if (r < 0) |
4494 | return r; | |
4495 | } | |
4496 | ||
a7a4c60a | 4497 | if (arg_set_dnssec) { |
614a6770 | 4498 | r = translate("dnssec", arg_ifname, 1, (char **) &arg_set_dnssec); |
a7a4c60a YW |
4499 | if (r < 0) |
4500 | return r; | |
14965b94 LP |
4501 | } |
4502 | ||
a7a4c60a YW |
4503 | return r; |
4504 | ||
4505 | case MODE_REVERT_LINK: | |
a661dc36 YW |
4506 | assert(arg_ifname); |
4507 | ||
614a6770 | 4508 | return translate("revert", arg_ifname, 0, NULL); |
088c1363 LP |
4509 | |
4510 | case _MODE_INVALID: | |
04499a70 | 4511 | assert_not_reached(); |
bdef7319 ZJS |
4512 | } |
4513 | ||
a7a4c60a YW |
4514 | return 0; |
4515 | } | |
4516 | ||
f6aa6190 | 4517 | static int run(int argc, char **argv) { |
427eeb44 | 4518 | bool compat = false; |
a7a4c60a YW |
4519 | int r; |
4520 | ||
4521 | setlocale(LC_ALL, ""); | |
d2acb93d | 4522 | log_setup(); |
a7a4c60a | 4523 | |
427eeb44 YW |
4524 | if (invoked_as(argv, "resolvconf")) { |
4525 | compat = true; | |
a7a4c60a | 4526 | r = resolvconf_parse_argv(argc, argv); |
427eeb44 YW |
4527 | } else if (invoked_as(argv, "systemd-resolve")) { |
4528 | compat = true; | |
a7a4c60a | 4529 | r = compat_parse_argv(argc, argv); |
427eeb44 | 4530 | } else |
a7a4c60a YW |
4531 | r = native_parse_argv(argc, argv); |
4532 | if (r <= 0) | |
f6aa6190 | 4533 | return r; |
a7a4c60a | 4534 | |
427eeb44 | 4535 | if (compat) |
614a6770 | 4536 | return compat_main(argc, argv); |
be371fe0 | 4537 | |
614a6770 | 4538 | return native_main(argc, argv); |
bdef7319 | 4539 | } |
f6aa6190 YW |
4540 | |
4541 | DEFINE_MAIN_FUNCTION(run); |