]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-conf.c
resolved: explicitly refuse zone transfers using the bus API
[thirdparty/systemd.git] / src / resolve / resolved-conf.c
CommitLineData
4e945a6f
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2014 Tom Gundersen <teg@jklm.no>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
b5efdb8a 20#include "alloc-util.h"
4e945a6f 21#include "conf-parser.h"
a0f29c76 22#include "def.h"
b5efdb8a 23#include "extract-word.h"
6bedfcbb 24#include "parse-util.h"
4e945a6f 25#include "resolved-conf.h"
6bedfcbb 26#include "string-util.h"
4e945a6f 27
636e813d
LP
28int manager_add_dns_server_by_string(Manager *m, DnsServerType type, const char *word) {
29 union in_addr_union address;
2817157b 30 int family, r, ifindex = 0;
0eac4623 31 DnsServer *s;
4e945a6f
LP
32
33 assert(m);
636e813d
LP
34 assert(word);
35
2817157b 36 r = in_addr_ifindex_from_string_auto(word, &family, &address, &ifindex);
636e813d
LP
37 if (r < 0)
38 return r;
4e945a6f 39
636e813d 40 /* Filter out duplicates */
2817157b 41 s = dns_server_find(manager_get_first_dns_server(m, type), family, &address, ifindex);
636e813d
LP
42 if (s) {
43 /*
44 * Drop the marker. This is used to find the servers
45 * that ceased to exist, see
46 * manager_mark_dns_servers() and
47 * manager_flush_marked_dns_servers().
48 */
0b58db65 49 dns_server_move_back_and_unmark(s);
636e813d
LP
50 return 0;
51 }
52
2817157b 53 return dns_server_new(m, NULL, type, NULL, family, &address, ifindex);
636e813d
LP
54}
55
56int manager_parse_dns_server_string_and_warn(Manager *m, DnsServerType type, const char *string) {
57 int r;
58
59 assert(m);
60 assert(string);
61
9ed794a3 62 for (;;) {
b5efdb8a 63 _cleanup_free_ char *word = NULL;
880603a1
SS
64
65 r = extract_first_word(&string, &word, NULL, 0);
66 if (r < 0)
636e813d 67 return r;
880603a1
SS
68 if (r == 0)
69 break;
4e945a6f 70
636e813d 71 r = manager_add_dns_server_by_string(m, type, word);
a51c1048 72 if (r < 0)
2817157b 73 log_warning_errno(r, "Failed to add DNS server address '%s', ignoring: %m", word);
a51c1048
LP
74 }
75
76 return 0;
77}
78
79int manager_add_search_domain_by_string(Manager *m, const char *domain) {
80 DnsSearchDomain *d;
adc800a6 81 bool route_only;
a51c1048
LP
82 int r;
83
84 assert(m);
85 assert(domain);
86
adc800a6
LP
87 route_only = *domain == '~';
88 if (route_only)
89 domain++;
90
91 if (dns_name_is_root(domain) || streq(domain, "*")) {
92 route_only = true;
93 domain = ".";
94 }
95
a51c1048
LP
96 r = dns_search_domain_find(m->search_domains, domain, &d);
97 if (r < 0)
98 return r;
adc800a6 99 if (r > 0)
a51c1048 100 dns_search_domain_move_back_and_unmark(d);
adc800a6
LP
101 else {
102 r = dns_search_domain_new(m, &d, DNS_SEARCH_DOMAIN_SYSTEM, NULL, domain);
103 if (r < 0)
104 return r;
a51c1048
LP
105 }
106
adc800a6
LP
107 d->route_only = route_only;
108 return 0;
a51c1048
LP
109}
110
111int manager_parse_search_domains_and_warn(Manager *m, const char *string) {
112 int r;
113
114 assert(m);
115 assert(string);
116
9ed794a3 117 for (;;) {
a51c1048
LP
118 _cleanup_free_ char *word = NULL;
119
120 r = extract_first_word(&string, &word, NULL, EXTRACT_QUOTES);
121 if (r < 0)
122 return r;
123 if (r == 0)
124 break;
125
126 r = manager_add_search_domain_by_string(m, word);
127 if (r < 0)
2817157b 128 log_warning_errno(r, "Failed to add search domain '%s', ignoring: %m", word);
4e945a6f
LP
129 }
130
131 return 0;
132}
133
636e813d 134int config_parse_dns_servers(
4e945a6f
LP
135 const char *unit,
136 const char *filename,
137 unsigned line,
138 const char *section,
139 unsigned section_line,
140 const char *lvalue,
141 int ltype,
142 const char *rvalue,
143 void *data,
144 void *userdata) {
145
146 Manager *m = userdata;
4e945a6f
LP
147 int r;
148
149 assert(filename);
150 assert(lvalue);
151 assert(rvalue);
152 assert(m);
153
3e684349 154 if (isempty(rvalue))
5cb36f41 155 /* Empty assignment means clear the list */
4b95f179 156 dns_server_unlink_all(manager_get_first_dns_server(m, ltype));
3e684349 157 else {
b938cb90 158 /* Otherwise, add to the list */
636e813d 159 r = manager_parse_dns_server_string_and_warn(m, ltype, rvalue);
5cb36f41 160 if (r < 0) {
12ca818f 161 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse DNS server string '%s'. Ignoring.", rvalue);
5cb36f41
LP
162 return 0;
163 }
4e945a6f
LP
164 }
165
5cb36f41
LP
166 /* If we have a manual setting, then we stop reading
167 * /etc/resolv.conf */
168 if (ltype == DNS_SERVER_SYSTEM)
169 m->read_resolv_conf = false;
00fa60ae
LP
170 if (ltype == DNS_SERVER_FALLBACK)
171 m->need_builtin_fallbacks = false;
5cb36f41 172
4e945a6f
LP
173 return 0;
174}
175
a51c1048
LP
176int config_parse_search_domains(
177 const char *unit,
178 const char *filename,
179 unsigned line,
180 const char *section,
181 unsigned section_line,
182 const char *lvalue,
183 int ltype,
184 const char *rvalue,
185 void *data,
186 void *userdata) {
187
188 Manager *m = userdata;
189 int r;
190
191 assert(filename);
192 assert(lvalue);
193 assert(rvalue);
194 assert(m);
195
196 if (isempty(rvalue))
197 /* Empty assignment means clear the list */
198 dns_search_domain_unlink_all(m->search_domains);
199 else {
200 /* Otherwise, add to the list */
201 r = manager_parse_search_domains_and_warn(m, rvalue);
202 if (r < 0) {
203 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse search domains string '%s'. Ignoring.", rvalue);
204 return 0;
205 }
206 }
207
208 /* If we have a manual setting, then we stop reading
209 * /etc/resolv.conf */
210 m->read_resolv_conf = false;
211
212 return 0;
213}
214
4e945a6f 215int manager_parse_config_file(Manager *m) {
00fa60ae
LP
216 int r;
217
4e945a6f
LP
218 assert(m);
219
00fa60ae
LP
220 r = config_parse_many(PKGSYSCONFDIR "/resolved.conf",
221 CONF_PATHS_NULSTR("systemd/resolved.conf.d"),
222 "Resolve\0",
223 config_item_perf_lookup, resolved_gperf_lookup,
224 false, m);
225 if (r < 0)
226 return r;
227
228 if (m->need_builtin_fallbacks) {
229 r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_FALLBACK, DNS_SERVERS);
230 if (r < 0)
231 return r;
232 }
233
234 return 0;
235
4e945a6f 236}