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 int manager_add_dns_server_by_string(Manager
*m
, DnsServerType type
, const char *word
) {
25 _cleanup_free_
char *server_name
= NULL
;
26 union in_addr_union address
;
27 int family
, r
, ifindex
= 0;
34 r
= in_addr_port_ifindex_name_from_string_auto(word
, &family
, &address
, &port
, &ifindex
, &server_name
);
38 if (IN_SET(port
, 53, 853))
41 /* Silently filter out 0.0.0.0 and 127.0.0.53 (our own stub DNS listener) */
42 if (!dns_server_address_valid(family
, &address
))
45 /* By default, the port number is determined with the transaction feature level.
46 * See dns_transaction_port() and dns_server_port(). */
47 if (IN_SET(port
, 53, 853))
50 /* Filter out duplicates */
51 s
= dns_server_find(manager_get_first_dns_server(m
, type
), family
, &address
, port
, ifindex
, server_name
);
54 * Drop the marker. This is used to find the servers
55 * that ceased to exist, see
56 * manager_mark_dns_servers() and
57 * manager_flush_marked_dns_servers().
59 dns_server_move_back_and_unmark(s
);
63 return dns_server_new(m
, NULL
, type
, NULL
, family
, &address
, port
, ifindex
, server_name
);
66 int manager_parse_dns_server_string_and_warn(Manager
*m
, DnsServerType type
, const char *string
) {
73 _cleanup_free_
char *word
= NULL
;
75 r
= extract_first_word(&string
, &word
, NULL
, 0);
81 r
= manager_add_dns_server_by_string(m
, type
, word
);
83 log_warning_errno(r
, "Failed to add DNS server address '%s', ignoring: %m", word
);
89 static int manager_add_search_domain_by_string(Manager
*m
, const char *domain
) {
97 route_only
= *domain
== '~';
101 if (dns_name_is_root(domain
) || streq(domain
, "*")) {
106 r
= dns_search_domain_find(m
->search_domains
, domain
, &d
);
110 dns_search_domain_move_back_and_unmark(d
);
112 r
= dns_search_domain_new(m
, &d
, DNS_SEARCH_DOMAIN_SYSTEM
, NULL
, domain
);
117 d
->route_only
= route_only
;
121 int manager_parse_search_domains_and_warn(Manager
*m
, const char *string
) {
128 _cleanup_free_
char *word
= NULL
;
130 r
= extract_first_word(&string
, &word
, NULL
, EXTRACT_UNQUOTE
);
136 r
= manager_add_search_domain_by_string(m
, word
);
138 log_warning_errno(r
, "Failed to add search domain '%s', ignoring: %m", word
);
144 int config_parse_dns_servers(
146 const char *filename
,
149 unsigned section_line
,
156 Manager
*m
= userdata
;
165 /* Empty assignment means clear the list */
166 dns_server_unlink_all(manager_get_first_dns_server(m
, ltype
));
168 /* Otherwise, add to the list */
169 r
= manager_parse_dns_server_string_and_warn(m
, ltype
, rvalue
);
171 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse DNS server string '%s'. Ignoring.", rvalue
);
176 /* If we have a manual setting, then we stop reading
177 * /etc/resolv.conf */
178 if (ltype
== DNS_SERVER_SYSTEM
)
179 m
->read_resolv_conf
= false;
180 if (ltype
== DNS_SERVER_FALLBACK
)
181 m
->need_builtin_fallbacks
= false;
186 int config_parse_search_domains(
188 const char *filename
,
191 unsigned section_line
,
198 Manager
*m
= userdata
;
207 /* Empty assignment means clear the list */
208 dns_search_domain_unlink_all(m
->search_domains
);
210 /* Otherwise, add to the list */
211 r
= manager_parse_search_domains_and_warn(m
, rvalue
);
213 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse search domains string '%s'. Ignoring.", rvalue
);
218 /* If we have a manual setting, then we stop reading
219 * /etc/resolv.conf */
220 m
->read_resolv_conf
= false;
225 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
) {
226 static const Specifier specifier_table
[] = {
227 { 'm', specifier_machine_id
, NULL
},
228 { 'b', specifier_boot_id
, NULL
},
229 { 'H', specifier_host_name
, NULL
},
230 { 'v', specifier_kernel_release
, NULL
},
231 { 'a', specifier_architecture
, NULL
},
232 { 'o', specifier_os_id
, NULL
},
233 { 'w', specifier_os_version_id
, NULL
},
234 { 'B', specifier_os_build_id
, NULL
},
235 { 'W', specifier_os_variant_id
, NULL
},
238 DnssdService
*s
= userdata
;
239 _cleanup_free_
char *name
= NULL
;
247 if (isempty(rvalue
)) {
248 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service instance name can't be empty. Ignoring.");
252 r
= free_and_strdup(&s
->name_template
, rvalue
);
256 r
= specifier_printf(s
->name_template
, specifier_table
, NULL
, &name
);
258 return log_debug_errno(r
, "Failed to replace specifiers: %m");
260 if (!dns_service_name_is_valid(name
)) {
261 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service instance name template renders to invalid name '%s'. Ignoring.", name
);
268 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
) {
269 DnssdService
*s
= userdata
;
277 if (isempty(rvalue
)) {
278 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service type can't be empty. Ignoring.");
282 if (!dnssd_srv_type_is_valid(rvalue
)) {
283 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service type is invalid. Ignoring.");
287 r
= free_and_strdup(&s
->type
, rvalue
);
294 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
) {
295 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
296 DnssdService
*s
= userdata
;
297 DnsTxtItem
*last
= NULL
;
304 if (isempty(rvalue
)) {
305 /* Flush out collected items */
306 s
->txt_data_items
= dnssd_txtdata_free_all(s
->txt_data_items
);
310 txt_data
= new0(DnssdTxtData
, 1);
315 _cleanup_free_
char *word
= NULL
;
316 _cleanup_free_
char *key
= NULL
;
317 _cleanup_free_
char *value
= NULL
;
318 _cleanup_free_
void *decoded
= NULL
;
323 r
= extract_first_word(&rvalue
, &word
, NULL
,
324 EXTRACT_UNQUOTE
|EXTRACT_CUNESCAPE
|EXTRACT_CUNESCAPE_RELAX
);
330 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
332 r
= split_pair(word
, "=", &key
, &value
);
336 key
= TAKE_PTR(word
);
338 if (!ascii_is_valid(key
)) {
339 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid syntax, ignoring: %s", key
);
345 case DNS_TXT_ITEM_DATA
:
347 r
= unbase64mem(value
, strlen(value
), &decoded
, &length
);
351 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
352 "Invalid base64 encoding, ignoring: %s", value
);
355 r
= dnssd_txt_item_new_from_data(key
, decoded
, length
, &i
);
360 case DNS_TXT_ITEM_TEXT
:
361 r
= dnssd_txt_item_new_from_string(key
, value
, &i
);
367 assert_not_reached("Unknown type of Txt config");
370 LIST_INSERT_AFTER(items
, txt_data
->txt
, last
, i
);
374 if (!LIST_IS_EMPTY(txt_data
->txt
)) {
375 LIST_PREPEND(items
, s
->txt_data_items
, txt_data
);
382 int config_parse_dns_stub_listener_extra(
384 const char *filename
,
387 unsigned section_line
,
394 _cleanup_free_ DnsStubListenerExtra
*stub
= NULL
;
395 Manager
*m
= userdata
;
404 if (isempty(rvalue
)) {
405 m
->dns_extra_stub_listeners
= ordered_set_free(m
->dns_extra_stub_listeners
);
409 r
= dns_stub_listener_extra_new(m
, &stub
);
413 p
= startswith(rvalue
, "udp:");
415 stub
->mode
= DNS_STUB_LISTENER_UDP
;
417 p
= startswith(rvalue
, "tcp:");
419 stub
->mode
= DNS_STUB_LISTENER_TCP
;
421 stub
->mode
= DNS_STUB_LISTENER_YES
;
426 r
= in_addr_port_from_string_auto(p
, &stub
->family
, &stub
->address
, &stub
->port
);
428 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
429 "Failed to parse address in %s=%s, ignoring assignment: %m",
434 r
= ordered_set_ensure_put(&m
->dns_extra_stub_listeners
, &dns_stub_listener_extra_hash_ops
, stub
);
438 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
439 "Failed to store %s=%s, ignoring assignment: %m", lvalue
, rvalue
);
448 int manager_parse_config_file(Manager
*m
) {
453 r
= config_parse_many_nulstr(
454 PKGSYSCONFDIR
"/resolved.conf",
455 CONF_PATHS_NULSTR("systemd/resolved.conf.d"),
457 config_item_perf_lookup
, resolved_gperf_lookup
,
464 if (m
->need_builtin_fallbacks
) {
465 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_FALLBACK
, DNS_SERVERS
);
471 if (m
->dnssec_mode
!= DNSSEC_NO
) {
472 log_warning("DNSSEC option cannot be enabled or set to allow-downgrade when systemd-resolved is built without gcrypt support. Turning off DNSSEC support.");
473 m
->dnssec_mode
= DNSSEC_NO
;
477 #if ! ENABLE_DNS_OVER_TLS
478 if (m
->dns_over_tls_mode
!= DNS_OVER_TLS_NO
) {
479 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.");
480 m
->dns_over_tls_mode
= DNS_OVER_TLS_NO
;