1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Tom Gundersen <teg@jklm.no>
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "alloc-util.h"
22 #include "conf-parser.h"
24 #include "extract-word.h"
25 #include "parse-util.h"
26 #include "resolved-conf.h"
27 #include "string-table.h"
28 #include "string-util.h"
30 DEFINE_CONFIG_PARSE_ENUM(config_parse_dns_stub_listener_mode
, dns_stub_listener_mode
, DnsStubListenerMode
, "Failed to parse DNS stub listener mode setting");
32 static const char* const dns_stub_listener_mode_table
[_DNS_STUB_LISTENER_MODE_MAX
] = {
33 [DNS_STUB_LISTENER_NO
] = "no",
34 [DNS_STUB_LISTENER_UDP
] = "udp",
35 [DNS_STUB_LISTENER_TCP
] = "tcp",
36 [DNS_STUB_LISTENER_YES
] = "yes",
38 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dns_stub_listener_mode
, DnsStubListenerMode
, DNS_STUB_LISTENER_YES
);
40 int manager_add_dns_server_by_string(Manager
*m
, DnsServerType type
, const char *word
) {
41 union in_addr_union address
;
42 int family
, r
, ifindex
= 0;
48 r
= in_addr_ifindex_from_string_auto(word
, &family
, &address
, &ifindex
);
52 /* Silently filter out 0.0.0.0 and 127.0.0.53 (our own stub DNS listener) */
53 if (!dns_server_address_valid(family
, &address
))
56 /* Filter out duplicates */
57 s
= dns_server_find(manager_get_first_dns_server(m
, type
), family
, &address
, ifindex
);
60 * Drop the marker. This is used to find the servers
61 * that ceased to exist, see
62 * manager_mark_dns_servers() and
63 * manager_flush_marked_dns_servers().
65 dns_server_move_back_and_unmark(s
);
69 return dns_server_new(m
, NULL
, type
, NULL
, family
, &address
, ifindex
);
72 int manager_parse_dns_server_string_and_warn(Manager
*m
, DnsServerType type
, const char *string
) {
79 _cleanup_free_
char *word
= NULL
;
81 r
= extract_first_word(&string
, &word
, NULL
, 0);
87 r
= manager_add_dns_server_by_string(m
, type
, word
);
89 log_warning_errno(r
, "Failed to add DNS server address '%s', ignoring: %m", word
);
95 int manager_add_search_domain_by_string(Manager
*m
, const char *domain
) {
103 route_only
= *domain
== '~';
107 if (dns_name_is_root(domain
) || streq(domain
, "*")) {
112 r
= dns_search_domain_find(m
->search_domains
, domain
, &d
);
116 dns_search_domain_move_back_and_unmark(d
);
118 r
= dns_search_domain_new(m
, &d
, DNS_SEARCH_DOMAIN_SYSTEM
, NULL
, domain
);
123 d
->route_only
= route_only
;
127 int manager_parse_search_domains_and_warn(Manager
*m
, const char *string
) {
134 _cleanup_free_
char *word
= NULL
;
136 r
= extract_first_word(&string
, &word
, NULL
, EXTRACT_QUOTES
);
142 r
= manager_add_search_domain_by_string(m
, word
);
144 log_warning_errno(r
, "Failed to add search domain '%s', ignoring: %m", word
);
150 int config_parse_dns_servers(
152 const char *filename
,
155 unsigned section_line
,
162 Manager
*m
= userdata
;
171 /* Empty assignment means clear the list */
172 dns_server_unlink_all(manager_get_first_dns_server(m
, ltype
));
174 /* Otherwise, add to the list */
175 r
= manager_parse_dns_server_string_and_warn(m
, ltype
, rvalue
);
177 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse DNS server string '%s'. Ignoring.", rvalue
);
182 /* If we have a manual setting, then we stop reading
183 * /etc/resolv.conf */
184 if (ltype
== DNS_SERVER_SYSTEM
)
185 m
->read_resolv_conf
= false;
186 if (ltype
== DNS_SERVER_FALLBACK
)
187 m
->need_builtin_fallbacks
= false;
192 int config_parse_search_domains(
194 const char *filename
,
197 unsigned section_line
,
204 Manager
*m
= userdata
;
213 /* Empty assignment means clear the list */
214 dns_search_domain_unlink_all(m
->search_domains
);
216 /* Otherwise, add to the list */
217 r
= manager_parse_search_domains_and_warn(m
, rvalue
);
219 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse search domains string '%s'. Ignoring.", rvalue
);
224 /* If we have a manual setting, then we stop reading
225 * /etc/resolv.conf */
226 m
->read_resolv_conf
= false;
231 int manager_parse_config_file(Manager
*m
) {
236 r
= config_parse_many_nulstr(PKGSYSCONFDIR
"/resolved.conf",
237 CONF_PATHS_NULSTR("systemd/resolved.conf.d"),
239 config_item_perf_lookup
, resolved_gperf_lookup
,
240 CONFIG_PARSE_WARN
, m
);
244 if (m
->need_builtin_fallbacks
) {
245 r
= manager_parse_dns_server_string_and_warn(m
, DNS_SERVER_FALLBACK
, DNS_SERVERS
);
251 if (m
->dnssec_mode
!= DNSSEC_NO
) {
252 log_warning("DNSSEC option cannot be enabled or set to allow-downgrade when systemd-resolved is built without gcrypt support. Turning off DNSSEC support.");
253 m
->dnssec_mode
= DNSSEC_NO
;