1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "conf-parser.h"
6 #include "extract-word.h"
8 #include "parse-util.h"
9 #include "resolved-conf.h"
10 #include "resolved-dnssd.h"
11 #include "resolved-manager.h"
12 #include "resolved-dns-search-domain.h"
13 #include "resolved-dns-stub.h"
14 #include "dns-domain.h"
15 #include "socket-netlink.h"
16 #include "specifier.h"
17 #include "string-table.h"
18 #include "string-util.h"
22 DEFINE_CONFIG_PARSE_ENUM(config_parse_dns_stub_listener_mode
, dns_stub_listener_mode
, DnsStubListenerMode
, "Failed to parse DNS stub listener mode setting");
24 static const char* const dns_stub_listener_mode_table
[_DNS_STUB_LISTENER_MODE_MAX
] = {
25 [DNS_STUB_LISTENER_NO
] = "no",
26 [DNS_STUB_LISTENER_UDP
] = "udp",
27 [DNS_STUB_LISTENER_TCP
] = "tcp",
28 [DNS_STUB_LISTENER_YES
] = "yes",
30 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dns_stub_listener_mode
, DnsStubListenerMode
, DNS_STUB_LISTENER_YES
);
32 static void dns_stub_listener_extra_hash_func(const DNSStubListenerExtra
*a
, struct siphash
*state
) {
35 siphash24_compress(&a
->mode
, sizeof(a
->mode
), state
);
36 siphash24_compress(&a
->family
, sizeof(a
->family
), state
);
37 siphash24_compress(&a
->address
, FAMILY_ADDRESS_SIZE(a
->family
), state
);
38 siphash24_compress(&a
->port
, sizeof(a
->port
), state
);
41 static int dns_stub_listener_extra_compare_func(const DNSStubListenerExtra
*a
, const DNSStubListenerExtra
*b
) {
47 r
= CMP(a
->mode
, b
->mode
);
51 r
= CMP(a
->family
, b
->family
);
55 r
= memcmp(&a
->address
, &b
->address
, FAMILY_ADDRESS_SIZE(a
->family
));
59 return CMP(a
->port
, b
->port
);
62 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
63 dns_stub_listener_extra_hash_ops
,
65 dns_stub_listener_extra_hash_func
,
66 dns_stub_listener_extra_compare_func
,
67 dns_stub_listener_extra_free
);
69 static int manager_add_dns_server_by_string(Manager
*m
, DnsServerType type
, const char *word
) {
70 _cleanup_free_
char *server_name
= NULL
;
71 union in_addr_union address
;
72 int family
, r
, ifindex
= 0;
79 r
= in_addr_port_ifindex_name_from_string_auto(word
, &family
, &address
, &port
, &ifindex
, &server_name
);
83 if (IN_SET(port
, 53, 853))
86 /* Silently filter out 0.0.0.0 and 127.0.0.53 (our own stub DNS listener) */
87 if (!dns_server_address_valid(family
, &address
))
90 /* By default, the port number is determined with the transaction feature level.
91 * See dns_transaction_port() and dns_server_port(). */
92 if (IN_SET(port
, 53, 853))
95 /* Filter out duplicates */
96 s
= dns_server_find(manager_get_first_dns_server(m
, type
), family
, &address
, port
, ifindex
, server_name
);
99 * Drop the marker. This is used to find the servers
100 * that ceased to exist, see
101 * manager_mark_dns_servers() and
102 * manager_flush_marked_dns_servers().
104 dns_server_move_back_and_unmark(s
);
108 return dns_server_new(m
, NULL
, type
, NULL
, family
, &address
, port
, ifindex
, server_name
);
111 int manager_parse_dns_server_string_and_warn(Manager
*m
, DnsServerType type
, const char *string
) {
118 _cleanup_free_
char *word
= NULL
;
120 r
= extract_first_word(&string
, &word
, NULL
, 0);
126 r
= manager_add_dns_server_by_string(m
, type
, word
);
128 log_warning_errno(r
, "Failed to add DNS server address '%s', ignoring: %m", word
);
134 static int manager_add_search_domain_by_string(Manager
*m
, const char *domain
) {
142 route_only
= *domain
== '~';
146 if (dns_name_is_root(domain
) || streq(domain
, "*")) {
151 r
= dns_search_domain_find(m
->search_domains
, domain
, &d
);
155 dns_search_domain_move_back_and_unmark(d
);
157 r
= dns_search_domain_new(m
, &d
, DNS_SEARCH_DOMAIN_SYSTEM
, NULL
, domain
);
162 d
->route_only
= route_only
;
166 int manager_parse_search_domains_and_warn(Manager
*m
, const char *string
) {
173 _cleanup_free_
char *word
= NULL
;
175 r
= extract_first_word(&string
, &word
, NULL
, EXTRACT_UNQUOTE
);
181 r
= manager_add_search_domain_by_string(m
, word
);
183 log_warning_errno(r
, "Failed to add search domain '%s', ignoring: %m", word
);
189 int config_parse_dns_servers(
191 const char *filename
,
194 unsigned section_line
,
201 Manager
*m
= userdata
;
210 /* Empty assignment means clear the list */
211 dns_server_unlink_all(manager_get_first_dns_server(m
, ltype
));
213 /* Otherwise, add to the list */
214 r
= manager_parse_dns_server_string_and_warn(m
, ltype
, rvalue
);
216 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse DNS server string '%s'. Ignoring.", rvalue
);
221 /* If we have a manual setting, then we stop reading
222 * /etc/resolv.conf */
223 if (ltype
== DNS_SERVER_SYSTEM
)
224 m
->read_resolv_conf
= false;
225 if (ltype
== DNS_SERVER_FALLBACK
)
226 m
->need_builtin_fallbacks
= false;
231 int config_parse_search_domains(
233 const char *filename
,
236 unsigned section_line
,
243 Manager
*m
= userdata
;
252 /* Empty assignment means clear the list */
253 dns_search_domain_unlink_all(m
->search_domains
);
255 /* Otherwise, add to the list */
256 r
= manager_parse_search_domains_and_warn(m
, rvalue
);
258 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse search domains string '%s'. Ignoring.", rvalue
);
263 /* If we have a manual setting, then we stop reading
264 * /etc/resolv.conf */
265 m
->read_resolv_conf
= false;
270 int config_parse_dnssd_service_name(const char *unit
, const char *filename
, unsigned line
, const char *section
, unsigned section_line
, const char *lvalue
, int ltype
, const char *rvalue
, void *data
, void *userdata
) {
271 static const Specifier specifier_table
[] = {
272 { 'm', specifier_machine_id
, NULL
},
273 { 'b', specifier_boot_id
, NULL
},
274 { 'H', specifier_host_name
, NULL
},
275 { 'v', specifier_kernel_release
, NULL
},
276 { 'a', specifier_architecture
, NULL
},
277 { 'o', specifier_os_id
, NULL
},
278 { 'w', specifier_os_version_id
, NULL
},
279 { 'B', specifier_os_build_id
, NULL
},
280 { 'W', specifier_os_variant_id
, NULL
},
283 DnssdService
*s
= userdata
;
284 _cleanup_free_
char *name
= NULL
;
292 if (isempty(rvalue
)) {
293 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service instance name can't be empty. Ignoring.");
297 r
= free_and_strdup(&s
->name_template
, rvalue
);
301 r
= specifier_printf(s
->name_template
, specifier_table
, NULL
, &name
);
303 return log_debug_errno(r
, "Failed to replace specifiers: %m");
305 if (!dns_service_name_is_valid(name
)) {
306 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service instance name template renders to invalid name '%s'. Ignoring.", name
);
313 int config_parse_dnssd_service_type(const char *unit
, const char *filename
, unsigned line
, const char *section
, unsigned section_line
, const char *lvalue
, int ltype
, const char *rvalue
, void *data
, void *userdata
) {
314 DnssdService
*s
= userdata
;
322 if (isempty(rvalue
)) {
323 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service type can't be empty. Ignoring.");
327 if (!dnssd_srv_type_is_valid(rvalue
)) {
328 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service type is invalid. Ignoring.");
332 r
= free_and_strdup(&s
->type
, rvalue
);
339 int config_parse_dnssd_txt(const char *unit
, const char *filename
, unsigned line
, const char *section
, unsigned section_line
, const char *lvalue
, int ltype
, const char *rvalue
, void *data
, void *userdata
) {
340 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
341 DnssdService
*s
= userdata
;
342 DnsTxtItem
*last
= NULL
;
349 if (isempty(rvalue
)) {
350 /* Flush out collected items */
351 s
->txt_data_items
= dnssd_txtdata_free_all(s
->txt_data_items
);
355 txt_data
= new0(DnssdTxtData
, 1);
360 _cleanup_free_
char *word
= NULL
;
361 _cleanup_free_
char *key
= NULL
;
362 _cleanup_free_
char *value
= NULL
;
363 _cleanup_free_
void *decoded
= NULL
;
368 r
= extract_first_word(&rvalue
, &word
, NULL
,
369 EXTRACT_UNQUOTE
|EXTRACT_CUNESCAPE
|EXTRACT_CUNESCAPE_RELAX
);
375 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
377 r
= split_pair(word
, "=", &key
, &value
);
381 key
= TAKE_PTR(word
);
383 if (!ascii_is_valid(key
)) {
384 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid syntax, ignoring: %s", key
);
390 case DNS_TXT_ITEM_DATA
:
392 r
= unbase64mem(value
, strlen(value
), &decoded
, &length
);
396 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
397 "Invalid base64 encoding, ignoring: %s", value
);
400 r
= dnssd_txt_item_new_from_data(key
, decoded
, length
, &i
);
405 case DNS_TXT_ITEM_TEXT
:
406 r
= dnssd_txt_item_new_from_string(key
, value
, &i
);
412 assert_not_reached("Unknown type of Txt config");
415 LIST_INSERT_AFTER(items
, txt_data
->txt
, last
, i
);
419 if (!LIST_IS_EMPTY(txt_data
->txt
)) {
420 LIST_PREPEND(items
, s
->txt_data_items
, txt_data
);
427 int config_parse_dns_stub_listener_extra(
429 const char *filename
,
432 unsigned section_line
,
439 _cleanup_free_ DNSStubListenerExtra
*stub
= NULL
;
440 Manager
*m
= userdata
;
449 if (isempty(rvalue
)) {
450 m
->dns_extra_stub_listeners
= ordered_set_free(m
->dns_extra_stub_listeners
);
454 r
= dns_stub_listener_extra_new(&stub
);
458 p
= startswith(rvalue
, "udp:");
460 stub
->mode
= DNS_STUB_LISTENER_UDP
;
462 p
= startswith(rvalue
, "tcp:");
464 stub
->mode
= DNS_STUB_LISTENER_TCP
;
466 stub
->mode
= DNS_STUB_LISTENER_YES
;
471 r
= in_addr_port_from_string_auto(p
, &stub
->family
, &stub
->address
, &stub
->port
);
473 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
474 "Failed to parse address in %s=%s, ignoring assignment: %m",
479 r
= ordered_set_ensure_put(&m
->dns_extra_stub_listeners
, &dns_stub_listener_extra_hash_ops
, stub
);
483 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
484 "Failed to store %s=%s, ignoring assignment: %m", lvalue
, rvalue
);
493 int manager_parse_config_file(Manager
*m
) {
498 r
= config_parse_many_nulstr(
499 PKGSYSCONFDIR
"/resolved.conf",
500 CONF_PATHS_NULSTR("systemd/resolved.conf.d"),
502 config_item_perf_lookup
, resolved_gperf_lookup
,
509 if (m
->need_builtin_fallbacks
) {
510 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_FALLBACK
, DNS_SERVERS
);
516 if (m
->dnssec_mode
!= DNSSEC_NO
) {
517 log_warning("DNSSEC option cannot be enabled or set to allow-downgrade when systemd-resolved is built without gcrypt support. Turning off DNSSEC support.");
518 m
->dnssec_mode
= DNSSEC_NO
;
522 #if ! ENABLE_DNS_OVER_TLS
523 if (m
->dns_over_tls_mode
!= DNS_OVER_TLS_NO
) {
524 log_warning("DNS-over-TLS option cannot be enabled or set to opportunistic when systemd-resolved is built without DNS-over-TLS support. Turning off DNS-over-TLS support.");
525 m
->dns_over_tls_mode
= DNS_OVER_TLS_NO
;