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 "specifier.h"
12 #include "string-table.h"
13 #include "string-util.h"
16 DEFINE_CONFIG_PARSE_ENUM(config_parse_dns_stub_listener_mode
, dns_stub_listener_mode
, DnsStubListenerMode
, "Failed to parse DNS stub listener mode setting");
18 static const char* const dns_stub_listener_mode_table
[_DNS_STUB_LISTENER_MODE_MAX
] = {
19 [DNS_STUB_LISTENER_NO
] = "no",
20 [DNS_STUB_LISTENER_UDP
] = "udp",
21 [DNS_STUB_LISTENER_TCP
] = "tcp",
22 [DNS_STUB_LISTENER_YES
] = "yes",
24 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dns_stub_listener_mode
, DnsStubListenerMode
, DNS_STUB_LISTENER_YES
);
26 int manager_add_dns_server_by_string(Manager
*m
, DnsServerType type
, const char *word
) {
27 union in_addr_union address
;
28 int family
, r
, ifindex
= 0;
34 r
= in_addr_ifindex_from_string_auto(word
, &family
, &address
, &ifindex
);
38 /* Silently filter out 0.0.0.0 and 127.0.0.53 (our own stub DNS listener) */
39 if (!dns_server_address_valid(family
, &address
))
42 /* Filter out duplicates */
43 s
= dns_server_find(manager_get_first_dns_server(m
, type
), family
, &address
, ifindex
);
46 * Drop the marker. This is used to find the servers
47 * that ceased to exist, see
48 * manager_mark_dns_servers() and
49 * manager_flush_marked_dns_servers().
51 dns_server_move_back_and_unmark(s
);
55 return dns_server_new(m
, NULL
, type
, NULL
, family
, &address
, ifindex
);
58 int manager_parse_dns_server_string_and_warn(Manager
*m
, DnsServerType type
, const char *string
) {
65 _cleanup_free_
char *word
= NULL
;
67 r
= extract_first_word(&string
, &word
, NULL
, 0);
73 r
= manager_add_dns_server_by_string(m
, type
, word
);
75 log_warning_errno(r
, "Failed to add DNS server address '%s', ignoring: %m", word
);
81 int manager_add_search_domain_by_string(Manager
*m
, const char *domain
) {
89 route_only
= *domain
== '~';
93 if (dns_name_is_root(domain
) || streq(domain
, "*")) {
98 r
= dns_search_domain_find(m
->search_domains
, domain
, &d
);
102 dns_search_domain_move_back_and_unmark(d
);
104 r
= dns_search_domain_new(m
, &d
, DNS_SEARCH_DOMAIN_SYSTEM
, NULL
, domain
);
109 d
->route_only
= route_only
;
113 int manager_parse_search_domains_and_warn(Manager
*m
, const char *string
) {
120 _cleanup_free_
char *word
= NULL
;
122 r
= extract_first_word(&string
, &word
, NULL
, EXTRACT_QUOTES
);
128 r
= manager_add_search_domain_by_string(m
, word
);
130 log_warning_errno(r
, "Failed to add search domain '%s', ignoring: %m", word
);
136 int config_parse_dns_servers(
138 const char *filename
,
141 unsigned section_line
,
148 Manager
*m
= userdata
;
157 /* Empty assignment means clear the list */
158 dns_server_unlink_all(manager_get_first_dns_server(m
, ltype
));
160 /* Otherwise, add to the list */
161 r
= manager_parse_dns_server_string_and_warn(m
, ltype
, rvalue
);
163 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse DNS server string '%s'. Ignoring.", rvalue
);
168 /* If we have a manual setting, then we stop reading
169 * /etc/resolv.conf */
170 if (ltype
== DNS_SERVER_SYSTEM
)
171 m
->read_resolv_conf
= false;
172 if (ltype
== DNS_SERVER_FALLBACK
)
173 m
->need_builtin_fallbacks
= false;
178 int config_parse_search_domains(
180 const char *filename
,
183 unsigned section_line
,
190 Manager
*m
= userdata
;
199 /* Empty assignment means clear the list */
200 dns_search_domain_unlink_all(m
->search_domains
);
202 /* Otherwise, add to the list */
203 r
= manager_parse_search_domains_and_warn(m
, rvalue
);
205 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse search domains string '%s'. Ignoring.", rvalue
);
210 /* If we have a manual setting, then we stop reading
211 * /etc/resolv.conf */
212 m
->read_resolv_conf
= false;
217 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
) {
218 static const Specifier specifier_table
[] = {
219 { 'b', specifier_boot_id
, NULL
},
220 { 'H', specifier_host_name
, NULL
},
221 { 'm', specifier_machine_id
, NULL
},
222 { 'v', specifier_kernel_release
, NULL
},
225 DnssdService
*s
= userdata
;
226 _cleanup_free_
char *name
= NULL
;
234 if (isempty(rvalue
)) {
235 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service instance name can't be empty. Ignoring.");
239 r
= free_and_strdup(&s
->name_template
, rvalue
);
243 r
= specifier_printf(s
->name_template
, specifier_table
, NULL
, &name
);
245 return log_debug_errno(r
, "Failed to replace specifiers: %m");
247 if (!dns_service_name_is_valid(name
)) {
248 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service instance name template renders to invalid name '%s'. Ignoring.", name
);
255 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
) {
256 DnssdService
*s
= userdata
;
264 if (isempty(rvalue
)) {
265 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service type can't be empty. Ignoring.");
269 if (!dnssd_srv_type_is_valid(rvalue
)) {
270 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Service type is invalid. Ignoring.");
274 r
= free_and_strdup(&s
->type
, rvalue
);
281 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
) {
282 _cleanup_(dnssd_txtdata_freep
) DnssdTxtData
*txt_data
= NULL
;
283 DnssdService
*s
= userdata
;
284 DnsTxtItem
*last
= NULL
;
291 if (isempty(rvalue
)) {
292 /* Flush out collected items */
293 s
->txt_data_items
= dnssd_txtdata_free_all(s
->txt_data_items
);
297 txt_data
= new0(DnssdTxtData
, 1);
302 _cleanup_free_
char *word
= NULL
;
303 _cleanup_free_
char *key
= NULL
;
304 _cleanup_free_
char *value
= NULL
;
305 _cleanup_free_
void *decoded
= NULL
;
310 r
= extract_first_word(&rvalue
, &word
, NULL
,
311 EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
|EXTRACT_CUNESCAPE_RELAX
);
317 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
319 r
= split_pair(word
, "=", &key
, &value
);
323 key
= TAKE_PTR(word
);
325 if (!ascii_is_valid(key
)) {
326 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Invalid syntax, ignoring: %s", key
);
332 case DNS_TXT_ITEM_DATA
:
334 r
= unbase64mem(value
, strlen(value
), &decoded
, &length
);
338 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
339 "Invalid base64 encoding, ignoring: %s", value
);
342 r
= dnssd_txt_item_new_from_data(key
, decoded
, length
, &i
);
347 case DNS_TXT_ITEM_TEXT
:
348 r
= dnssd_txt_item_new_from_string(key
, value
, &i
);
354 assert_not_reached("Unknown type of Txt config");
357 LIST_INSERT_AFTER(items
, txt_data
->txt
, last
, i
);
361 if (!LIST_IS_EMPTY(txt_data
->txt
)) {
362 LIST_PREPEND(items
, s
->txt_data_items
, txt_data
);
369 int manager_parse_config_file(Manager
*m
) {
374 r
= config_parse_many_nulstr(PKGSYSCONFDIR
"/resolved.conf",
375 CONF_PATHS_NULSTR("systemd/resolved.conf.d"),
377 config_item_perf_lookup
, resolved_gperf_lookup
,
378 CONFIG_PARSE_WARN
, m
);
382 if (m
->need_builtin_fallbacks
) {
383 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_FALLBACK
, DNS_SERVERS
);
389 if (m
->dnssec_mode
!= DNSSEC_NO
) {
390 log_warning("DNSSEC option cannot be enabled or set to allow-downgrade when systemd-resolved is built without gcrypt support. Turning off DNSSEC support.");
391 m
->dnssec_mode
= DNSSEC_NO
;
395 #if ! ENABLE_DNS_OVER_TLS
396 if (m
->dns_over_tls_mode
!= DNS_OVER_TLS_NO
) {
397 log_warning("DNS-over-TLS option cannot be set to opportunistic when systemd-resolved is built without DNS-over-TLS support. Turning off DNS-over-TLS support.");
398 m
->dns_over_tls_mode
= DNS_OVER_TLS_NO
;