forked from adamlaska/systemd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolvectl.c
More file actions
3411 lines (2691 loc) · 125 KB
/
resolvectl.c
File metadata and controls
3411 lines (2691 loc) · 125 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <getopt.h>
#include <locale.h>
#include <net/if.h>
#include "sd-bus.h"
#include "sd-netlink.h"
#include "af-list.h"
#include "alloc-util.h"
#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-map-properties.h"
#include "bus-message-util.h"
#include "dns-domain.h"
#include "escape.h"
#include "format-table.h"
#include "format-util.h"
#include "gcrypt-util.h"
#include "hostname-util.h"
#include "main-func.h"
#include "missing_network.h"
#include "netlink-util.h"
#include "openssl-util.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "pretty-print.h"
#include "process-util.h"
#include "resolvconf-compat.h"
#include "resolvectl.h"
#include "resolved-def.h"
#include "resolved-dns-packet.h"
#include "resolved-util.h"
#include "socket-netlink.h"
#include "sort-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "strv.h"
#include "terminal-util.h"
#include "utf8.h"
#include "verb-log-control.h"
#include "verbs.h"
static int arg_family = AF_UNSPEC;
static int arg_ifindex = 0;
static char *arg_ifname = NULL;
static uint16_t arg_type = 0;
static uint16_t arg_class = 0;
static bool arg_legend = true;
static uint64_t arg_flags = 0;
static PagerFlags arg_pager_flags = 0;
bool arg_ifindex_permissive = false; /* If true, don't generate an error if the specified interface index doesn't exist */
static const char *arg_service_family = NULL;
typedef enum RawType {
RAW_NONE,
RAW_PAYLOAD,
RAW_PACKET,
} RawType;
static RawType arg_raw = RAW_NONE;
ExecutionMode arg_mode = MODE_RESOLVE_HOST;
char **arg_set_dns = NULL;
char **arg_set_domain = NULL;
static const char *arg_set_llmnr = NULL;
static const char *arg_set_mdns = NULL;
static const char *arg_set_dns_over_tls = NULL;
static const char *arg_set_dnssec = NULL;
static char **arg_set_nta = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_ifname, freep);
STATIC_DESTRUCTOR_REGISTER(arg_set_dns, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_set_domain, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_set_nta, strv_freep);
typedef enum StatusMode {
STATUS_ALL,
STATUS_DNS,
STATUS_DOMAIN,
STATUS_DEFAULT_ROUTE,
STATUS_LLMNR,
STATUS_MDNS,
STATUS_PRIVATE,
STATUS_DNSSEC,
STATUS_NTA,
} StatusMode;
typedef struct InterfaceInfo {
int index;
const char *name;
} InterfaceInfo;
static int interface_info_compare(const InterfaceInfo *a, const InterfaceInfo *b) {
int r;
r = CMP(a->index, b->index);
if (r != 0)
return r;
return strcmp_ptr(a->name, b->name);
}
int ifname_mangle(const char *s) {
_cleanup_free_ char *iface = NULL;
int ifi;
assert(s);
iface = strdup(s);
if (!iface)
return log_oom();
ifi = rtnl_resolve_interface(NULL, iface);
if (ifi < 0) {
if (ifi == -ENODEV && arg_ifindex_permissive) {
log_debug("Interface '%s' not found, but -f specified, ignoring.", iface);
return 0; /* done */
}
return log_error_errno(ifi, "Failed to resolve interface \"%s\": %m", iface);
}
if (arg_ifindex > 0 && arg_ifindex != ifi)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified multiple different interfaces. Refusing.");
arg_ifindex = ifi;
free_and_replace(arg_ifname, iface);
return 1;
}
int ifname_resolvconf_mangle(const char *s) {
const char *dot;
assert(s);
dot = strchr(s, '.');
if (dot) {
_cleanup_free_ char *iface = NULL;
log_debug("Ignoring protocol specifier '%s'.", dot + 1);
iface = strndup(s, dot - s);
if (!iface)
return log_oom();
return ifname_mangle(iface);
} else
return ifname_mangle(s);
}
static void print_source(uint64_t flags, usec_t rtt) {
if (!arg_legend)
return;
if (flags == 0)
return;
printf("\n%s-- Information acquired via", ansi_grey());
printf(" protocol%s%s%s%s%s",
flags & SD_RESOLVED_DNS ? " DNS" :"",
flags & SD_RESOLVED_LLMNR_IPV4 ? " LLMNR/IPv4" : "",
flags & SD_RESOLVED_LLMNR_IPV6 ? " LLMNR/IPv6" : "",
flags & SD_RESOLVED_MDNS_IPV4 ? " mDNS/IPv4" : "",
flags & SD_RESOLVED_MDNS_IPV6 ? " mDNS/IPv6" : "");
printf(" in %s.%s\n"
"%s-- Data is authenticated: %s; Data was acquired via local or encrypted transport: %s%s\n",
FORMAT_TIMESPAN(rtt, 100),
ansi_normal(),
ansi_grey(),
yes_no(flags & SD_RESOLVED_AUTHENTICATED),
yes_no(flags & SD_RESOLVED_CONFIDENTIAL),
ansi_normal());
if ((flags & (SD_RESOLVED_FROM_MASK|SD_RESOLVED_SYNTHETIC)) != 0)
printf("%s-- Data from:%s%s%s%s%s%s\n",
ansi_grey(),
FLAGS_SET(flags, SD_RESOLVED_SYNTHETIC) ? " synthetic" : "",
FLAGS_SET(flags, SD_RESOLVED_FROM_CACHE) ? " cache" : "",
FLAGS_SET(flags, SD_RESOLVED_FROM_ZONE) ? " zone" : "",
FLAGS_SET(flags, SD_RESOLVED_FROM_TRUST_ANCHOR) ? " trust-anchor" : "",
FLAGS_SET(flags, SD_RESOLVED_FROM_NETWORK) ? " network" : "",
ansi_normal());
}
static void print_ifindex_comment(int printed_so_far, int ifindex) {
char ifname[IF_NAMESIZE];
int r;
if (ifindex <= 0)
return;
r = format_ifname(ifindex, ifname);
if (r < 0)
return (void) log_warning_errno(r, "Failed to resolve interface name for index %i, ignoring: %m", ifindex);
printf("%*s%s-- link: %s%s",
60 > printed_so_far ? 60 - printed_so_far : 0, " ", /* Align comment to the 60th column */
ansi_grey(), ifname, ansi_normal());
}
static int resolve_host(sd_bus *bus, const char *name) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
const char *canonical = NULL;
unsigned c = 0;
uint64_t flags;
usec_t ts;
int r;
assert(name);
log_debug("Resolving %s (family %s, interface %s).", name, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname);
r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveHostname");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(req, "isit", arg_ifindex, name, arg_family, arg_flags);
if (r < 0)
return bus_log_create_error(r);
ts = now(CLOCK_MONOTONIC);
r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
if (r < 0)
return log_error_errno(r, "%s: resolve call failed: %s", name, bus_error_message(&error, r));
ts = now(CLOCK_MONOTONIC) - ts;
r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
if (r < 0)
return bus_log_parse_error(r);
while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
_cleanup_free_ char *pretty = NULL;
int ifindex, family, k;
union in_addr_union a;
assert_cc(sizeof(int) == sizeof(int32_t));
r = sd_bus_message_read(reply, "i", &ifindex);
if (r < 0)
return bus_log_parse_error(r);
sd_bus_error_free(&error);
r = bus_message_read_in_addr_auto(reply, &error, &family, &a);
if (r < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS))
return log_error_errno(r, "%s: systemd-resolved returned invalid result: %s", name, bus_error_message(&error, r));
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) {
log_debug_errno(r, "%s: systemd-resolved returned invalid result, ignoring: %s", name, bus_error_message(&error, r));
continue;
}
r = in_addr_ifindex_to_string(family, &a, ifindex, &pretty);
if (r < 0)
return log_error_errno(r, "Failed to print address for %s: %m", name);
k = printf("%*s%s %s%s%s",
(int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
ansi_highlight(), pretty, ansi_normal());
print_ifindex_comment(k, ifindex);
fputc('\n', stdout);
c++;
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read(reply, "st", &canonical, &flags);
if (r < 0)
return bus_log_parse_error(r);
if (!streq(name, canonical))
printf("%*s%s (%s)\n",
(int) strlen(name), c == 0 ? name : "", c == 0 ? ":" : " ",
canonical);
if (c == 0)
return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
"%s: no addresses found", name);
print_source(flags, ts);
return 0;
}
static int resolve_address(sd_bus *bus, int family, const union in_addr_union *address, int ifindex) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_free_ char *pretty = NULL;
uint64_t flags;
unsigned c = 0;
usec_t ts;
int r;
assert(bus);
assert(IN_SET(family, AF_INET, AF_INET6));
assert(address);
if (ifindex <= 0)
ifindex = arg_ifindex;
r = in_addr_ifindex_to_string(family, address, ifindex, &pretty);
if (r < 0)
return log_oom();
log_debug("Resolving %s.", pretty);
r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveAddress");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(req, "ii", ifindex, family);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append_array(req, 'y', address, FAMILY_ADDRESS_SIZE(family));
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(req, "t", arg_flags);
if (r < 0)
return bus_log_create_error(r);
ts = now(CLOCK_MONOTONIC);
r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
if (r < 0)
return log_error_errno(r, "%s: resolve call failed: %s", pretty, bus_error_message(&error, r));
ts = now(CLOCK_MONOTONIC) - ts;
r = sd_bus_message_enter_container(reply, 'a', "(is)");
if (r < 0)
return bus_log_create_error(r);
while ((r = sd_bus_message_enter_container(reply, 'r', "is")) > 0) {
const char *n;
int k;
assert_cc(sizeof(int) == sizeof(int32_t));
r = sd_bus_message_read(reply, "is", &ifindex, &n);
if (r < 0)
return r;
r = sd_bus_message_exit_container(reply);
if (r < 0)
return r;
k = printf("%*s%s %s%s%s",
(int) strlen(pretty), c == 0 ? pretty : "",
c == 0 ? ":" : " ",
ansi_highlight(), n, ansi_normal());
print_ifindex_comment(k, ifindex);
fputc('\n', stdout);
c++;
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read(reply, "t", &flags);
if (r < 0)
return bus_log_parse_error(r);
if (c == 0)
return log_error_errno(SYNTHETIC_ERRNO(ESRCH),
"%s: no names found", pretty);
print_source(flags, ts);
return 0;
}
static int output_rr_packet(const void *d, size_t l, int ifindex) {
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
int r;
r = dns_packet_new(&p, DNS_PROTOCOL_DNS, 0, DNS_PACKET_SIZE_MAX);
if (r < 0)
return log_oom();
p->refuse_compression = true;
r = dns_packet_append_blob(p, d, l, NULL);
if (r < 0)
return log_oom();
r = dns_packet_read_rr(p, &rr, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse RR: %m");
if (arg_raw == RAW_PAYLOAD) {
void *data;
ssize_t k;
k = dns_resource_record_payload(rr, &data);
if (k < 0)
return log_error_errno(k, "Cannot dump RR: %m");
fwrite(data, 1, k, stdout);
} else {
const char *s;
int k;
s = dns_resource_record_to_string(rr);
if (!s)
return log_oom();
k = printf("%s", s);
print_ifindex_comment(k, ifindex);
fputc('\n', stdout);
}
return 0;
}
static int idna_candidate(const char *name, char **ret) {
_cleanup_free_ char *idnafied = NULL;
int r;
assert(name);
assert(ret);
r = dns_name_apply_idna(name, &idnafied);
if (r < 0)
return log_error_errno(r, "Failed to apply IDNA to name '%s': %m", name);
if (r > 0 && !streq(name, idnafied)) {
*ret = TAKE_PTR(idnafied);
return true;
}
*ret = NULL;
return false;
}
static bool single_label_nonsynthetic(const char *name) {
_cleanup_free_ char *first_label = NULL;
int r;
if (!dns_name_is_single_label(name))
return false;
if (is_localhost(name) || is_gateway_hostname(name))
return false;
r = resolve_system_hostname(NULL, &first_label);
if (r < 0) {
log_warning_errno(r, "Failed to determine the hostname: %m");
return false;
}
return !streq(name, first_label);
}
static int resolve_record(sd_bus *bus, const char *name, uint16_t class, uint16_t type, bool warn_missing) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_free_ char *idnafied = NULL;
bool needs_authentication = false;
unsigned n = 0;
uint64_t flags;
usec_t ts;
int r;
assert(name);
log_debug("Resolving %s %s %s (interface %s).", name, dns_class_to_string(class), dns_type_to_string(type), isempty(arg_ifname) ? "*" : arg_ifname);
if (dns_name_dot_suffixed(name) == 0 && single_label_nonsynthetic(name))
log_notice("(Note that search domains are not appended when --type= is specified. "
"Please specify fully qualified domain names, or remove --type= switch from invocation in order to request regular hostname resolution.)");
r = idna_candidate(name, &idnafied);
if (r < 0)
return r;
if (r > 0)
log_notice("(Note that IDNA translation is not applied when --type= is specified. "
"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.",
idnafied);
r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveRecord");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(req, "isqqt", arg_ifindex, name, class, type, arg_flags);
if (r < 0)
return bus_log_create_error(r);
ts = now(CLOCK_MONOTONIC);
r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
if (r < 0) {
if (warn_missing || r != -ENXIO)
log_error("%s: resolve call failed: %s", name, bus_error_message(&error, r));
return r;
}
ts = now(CLOCK_MONOTONIC) - ts;
r = sd_bus_message_enter_container(reply, 'a', "(iqqay)");
if (r < 0)
return bus_log_parse_error(r);
while ((r = sd_bus_message_enter_container(reply, 'r', "iqqay")) > 0) {
uint16_t c, t;
int ifindex;
const void *d;
size_t l;
assert_cc(sizeof(int) == sizeof(int32_t));
r = sd_bus_message_read(reply, "iqq", &ifindex, &c, &t);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read_array(reply, 'y', &d, &l);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
if (arg_raw == RAW_PACKET) {
uint64_t u64 = htole64(l);
fwrite(&u64, sizeof(u64), 1, stdout);
fwrite(d, 1, l, stdout);
} else {
r = output_rr_packet(d, l, ifindex);
if (r < 0)
return r;
}
if (dns_type_needs_authentication(t))
needs_authentication = true;
n++;
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read(reply, "t", &flags);
if (r < 0)
return bus_log_parse_error(r);
if (n == 0) {
if (warn_missing)
log_error("%s: no records found", name);
return -ESRCH;
}
print_source(flags, ts);
if ((flags & SD_RESOLVED_AUTHENTICATED) == 0 && needs_authentication) {
fflush(stdout);
fprintf(stderr, "\n%s"
"WARNING: The resources shown contain cryptographic key data which could not be\n"
" authenticated. It is not suitable to authenticate any communication.\n"
" This is usually indication that DNSSEC authentication was not enabled\n"
" or is not available for the selected protocol or DNS servers.%s\n",
ansi_highlight_red(),
ansi_normal());
}
return 0;
}
static int resolve_rfc4501(sd_bus *bus, const char *name) {
uint16_t type = 0, class = 0;
const char *p, *q, *n;
int r;
assert(bus);
assert(name);
assert(startswith(name, "dns:"));
/* Parse RFC 4501 dns: URIs */
p = name + 4;
if (p[0] == '/') {
const char *e;
if (p[1] != '/')
goto invalid;
e = strchr(p + 2, '/');
if (!e)
goto invalid;
if (e != p + 2)
log_warning("DNS authority specification not supported; ignoring specified authority.");
p = e + 1;
}
q = strchr(p, '?');
if (q) {
n = strndupa_safe(p, q - p);
q++;
for (;;) {
const char *f;
f = startswith_no_case(q, "class=");
if (f) {
_cleanup_free_ char *t = NULL;
const char *e;
if (class != 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"DNS class specified twice.");
e = strchrnul(f, ';');
t = strndup(f, e - f);
if (!t)
return log_oom();
r = dns_class_from_string(t);
if (r < 0)
return log_error_errno(r, "Unknown DNS class %s.", t);
class = r;
if (*e == ';') {
q = e + 1;
continue;
}
break;
}
f = startswith_no_case(q, "type=");
if (f) {
_cleanup_free_ char *t = NULL;
const char *e;
if (type != 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"DNS type specified twice.");
e = strchrnul(f, ';');
t = strndup(f, e - f);
if (!t)
return log_oom();
r = dns_type_from_string(t);
if (r < 0)
return log_error_errno(r, "Unknown DNS type %s: %m", t);
type = r;
if (*e == ';') {
q = e + 1;
continue;
}
break;
}
goto invalid;
}
} else
n = p;
if (class == 0)
class = arg_class ?: DNS_CLASS_IN;
if (type == 0)
type = arg_type ?: DNS_TYPE_A;
return resolve_record(bus, n, class, type, true);
invalid:
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid DNS URI: %s", name);
}
static int verb_query(int argc, char **argv, void *userdata) {
sd_bus *bus = userdata;
int q, r = 0;
if (arg_type != 0)
STRV_FOREACH(p, argv + 1) {
q = resolve_record(bus, *p, arg_class, arg_type, true);
if (q < 0)
r = q;
}
else
STRV_FOREACH(p, argv + 1) {
if (startswith(*p, "dns:"))
q = resolve_rfc4501(bus, *p);
else {
int family, ifindex;
union in_addr_union a;
q = in_addr_ifindex_from_string_auto(*p, &family, &a, &ifindex);
if (q >= 0)
q = resolve_address(bus, family, &a, ifindex);
else
q = resolve_host(bus, *p);
}
if (q < 0)
r = q;
}
return r;
}
static int resolve_service(sd_bus *bus, const char *name, const char *type, const char *domain) {
const char *canonical_name, *canonical_type, *canonical_domain;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL, *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
size_t indent, sz;
uint64_t flags;
const char *p;
unsigned c;
usec_t ts;
int r;
assert(bus);
assert(domain);
name = empty_to_null(name);
type = empty_to_null(type);
if (name)
log_debug("Resolving service \"%s\" of type %s in %s (family %s, interface %s).", name, type, domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname);
else if (type)
log_debug("Resolving service type %s of %s (family %s, interface %s).", type, domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname);
else
log_debug("Resolving service type %s (family %s, interface %s).", domain, af_to_name(arg_family) ?: "*", isempty(arg_ifname) ? "*" : arg_ifname);
r = bus_message_new_method_call(bus, &req, bus_resolve_mgr, "ResolveService");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(req, "isssit", arg_ifindex, name, type, domain, arg_family, arg_flags);
if (r < 0)
return bus_log_create_error(r);
ts = now(CLOCK_MONOTONIC);
r = sd_bus_call(bus, req, SD_RESOLVED_QUERY_TIMEOUT_USEC, &error, &reply);
if (r < 0)
return log_error_errno(r, "Resolve call failed: %s", bus_error_message(&error, r));
ts = now(CLOCK_MONOTONIC) - ts;
r = sd_bus_message_enter_container(reply, 'a', "(qqqsa(iiay)s)");
if (r < 0)
return bus_log_parse_error(r);
indent =
(name ? strlen(name) + 1 : 0) +
(type ? strlen(type) + 1 : 0) +
strlen(domain) + 2;
c = 0;
while ((r = sd_bus_message_enter_container(reply, 'r', "qqqsa(iiay)s")) > 0) {
uint16_t priority, weight, port;
const char *hostname, *canonical;
r = sd_bus_message_read(reply, "qqqs", &priority, &weight, &port, &hostname);
if (r < 0)
return bus_log_parse_error(r);
if (name)
printf("%*s%s", (int) strlen(name), c == 0 ? name : "", c == 0 ? "/" : " ");
if (type)
printf("%*s%s", (int) strlen(type), c == 0 ? type : "", c == 0 ? "/" : " ");
printf("%*s%s %s:%u [priority=%u, weight=%u]\n",
(int) strlen(domain), c == 0 ? domain : "",
c == 0 ? ":" : " ",
hostname, port,
priority, weight);
r = sd_bus_message_enter_container(reply, 'a', "(iiay)");
if (r < 0)
return bus_log_parse_error(r);
while ((r = sd_bus_message_enter_container(reply, 'r', "iiay")) > 0) {
_cleanup_free_ char *pretty = NULL;
int ifindex, family, k;
union in_addr_union a;;
assert_cc(sizeof(int) == sizeof(int32_t));
r = sd_bus_message_read(reply, "i", &ifindex);
if (r < 0)
return bus_log_parse_error(r);
sd_bus_error_free(&error);
r = bus_message_read_in_addr_auto(reply, &error, &family, &a);
if (r < 0 && !sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS))
return log_error_errno(r, "%s: systemd-resolved returned invalid result: %s", name, bus_error_message(&error, r));
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
if (sd_bus_error_has_name(&error, SD_BUS_ERROR_INVALID_ARGS)) {
log_debug_errno(r, "%s: systemd-resolved returned invalid result, ignoring: %s", name, bus_error_message(&error, r));
continue;
}
r = in_addr_ifindex_to_string(family, &a, ifindex, &pretty);
if (r < 0)
return log_error_errno(r, "Failed to print address for %s: %m", name);
k = printf("%*s%s", (int) indent, "", pretty);
print_ifindex_comment(k, ifindex);
fputc('\n', stdout);
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read(reply, "s", &canonical);
if (r < 0)
return bus_log_parse_error(r);
if (!streq(hostname, canonical))
printf("%*s(%s)\n", (int) indent, "", canonical);
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
c++;
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_enter_container(reply, 'a', "ay");
if (r < 0)
return bus_log_parse_error(r);
while ((r = sd_bus_message_read_array(reply, 'y', (const void**) &p, &sz)) > 0) {
_cleanup_free_ char *escaped = NULL;
escaped = cescape_length(p, sz);
if (!escaped)
return log_oom();
printf("%*s%s\n", (int) indent, "", escaped);
}
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_read(reply, "ssst", &canonical_name, &canonical_type, &canonical_domain, &flags);
if (r < 0)
return bus_log_parse_error(r);
canonical_name = empty_to_null(canonical_name);
canonical_type = empty_to_null(canonical_type);
if (!streq_ptr(name, canonical_name) ||
!streq_ptr(type, canonical_type) ||
!streq_ptr(domain, canonical_domain)) {
printf("%*s(", (int) indent, "");
if (canonical_name)
printf("%s/", canonical_name);
if (canonical_type)
printf("%s/", canonical_type);
printf("%s)\n", canonical_domain);
}
print_source(flags, ts);
return 0;
}
static int verb_service(int argc, char **argv, void *userdata) {
sd_bus *bus = userdata;
if (argc == 2)
return resolve_service(bus, NULL, NULL, argv[1]);
else if (argc == 3)
return resolve_service(bus, NULL, argv[1], argv[2]);
else
return resolve_service(bus, argv[1], argv[2], argv[3]);
}
static int resolve_openpgp(sd_bus *bus, const char *address) {
const char *domain, *full;
int r;
_cleanup_free_ char *hashed = NULL;
assert(bus);
assert(address);
domain = strrchr(address, '@');
if (!domain)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Address does not contain '@': \"%s\"", address);
if (domain == address || domain[1] == '\0')
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Address starts or ends with '@': \"%s\"", address);
domain++;
r = string_hashsum_sha256(address, domain - 1 - address, &hashed);
if (r < 0)
return log_error_errno(r, "Hashing failed: %m");
strshorten(hashed, 56);
full = strjoina(hashed, "._openpgpkey.", domain);
log_debug("Looking up \"%s\".", full);
r = resolve_record(bus, full,
arg_class ?: DNS_CLASS_IN,
arg_type ?: DNS_TYPE_OPENPGPKEY, false);
if (IN_SET(r, -ENXIO, -ESRCH)) { /* NXDOMAIN or NODATA? */
hashed = mfree(hashed);
r = string_hashsum_sha224(address, domain - 1 - address, &hashed);
if (r < 0)
return log_error_errno(r, "Hashing failed: %m");
full = strjoina(hashed, "._openpgpkey.", domain);
log_debug("Looking up \"%s\".", full);
return resolve_record(bus, full,
arg_class ?: DNS_CLASS_IN,
arg_type ?: DNS_TYPE_OPENPGPKEY, true);
}
return r;
}
static int verb_openpgp(int argc, char **argv, void *userdata) {
sd_bus *bus = userdata;
int q, r = 0;
STRV_FOREACH(p, argv + 1) {
q = resolve_openpgp(bus, *p);
if (q < 0)
r = q;
}
return r;
}
static int resolve_tlsa(sd_bus *bus, const char *family, const char *address) {
const char *port;
uint16_t port_num = 443;
_cleanup_free_ char *full = NULL;
int r;
assert(bus);
assert(address);
port = strrchr(address, ':');
if (port) {
r = parse_ip_port(port + 1, &port_num);
if (r < 0)