]>
Commit | Line | Data |
---|---|---|
4e945a6f LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2014 Tom Gundersen <teg@jklm.no> | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
b5efdb8a | 22 | #include "alloc-util.h" |
4e945a6f | 23 | #include "conf-parser.h" |
a0f29c76 | 24 | #include "def.h" |
b5efdb8a | 25 | #include "extract-word.h" |
6bedfcbb | 26 | #include "parse-util.h" |
4e945a6f | 27 | #include "resolved-conf.h" |
6bedfcbb | 28 | #include "string-util.h" |
4e945a6f | 29 | |
636e813d LP |
30 | int manager_add_dns_server_by_string(Manager *m, DnsServerType type, const char *word) { |
31 | union in_addr_union address; | |
636e813d | 32 | int family, r; |
0eac4623 | 33 | DnsServer *s; |
4e945a6f LP |
34 | |
35 | assert(m); | |
636e813d LP |
36 | assert(word); |
37 | ||
38 | r = in_addr_from_string_auto(word, &family, &address); | |
39 | if (r < 0) | |
40 | return r; | |
4e945a6f | 41 | |
636e813d | 42 | /* Filter out duplicates */ |
4b95f179 | 43 | s = dns_server_find(manager_get_first_dns_server(m, type), family, &address); |
636e813d LP |
44 | if (s) { |
45 | /* | |
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(). | |
50 | */ | |
0b58db65 | 51 | dns_server_move_back_and_unmark(s); |
636e813d LP |
52 | return 0; |
53 | } | |
54 | ||
55 | return dns_server_new(m, NULL, type, NULL, family, &address); | |
56 | } | |
57 | ||
58 | int manager_parse_dns_server_string_and_warn(Manager *m, DnsServerType type, const char *string) { | |
59 | int r; | |
60 | ||
61 | assert(m); | |
62 | assert(string); | |
63 | ||
880603a1 | 64 | for(;;) { |
b5efdb8a | 65 | _cleanup_free_ char *word = NULL; |
880603a1 SS |
66 | |
67 | r = extract_first_word(&string, &word, NULL, 0); | |
68 | if (r < 0) | |
636e813d | 69 | return r; |
880603a1 SS |
70 | if (r == 0) |
71 | break; | |
4e945a6f | 72 | |
636e813d | 73 | r = manager_add_dns_server_by_string(m, type, word); |
a51c1048 | 74 | if (r < 0) |
636e813d | 75 | log_warning_errno(r, "Failed to add DNS server address '%s', ignoring.", word); |
a51c1048 LP |
76 | } |
77 | ||
78 | return 0; | |
79 | } | |
80 | ||
81 | int manager_add_search_domain_by_string(Manager *m, const char *domain) { | |
82 | DnsSearchDomain *d; | |
83 | int r; | |
84 | ||
85 | assert(m); | |
86 | assert(domain); | |
87 | ||
88 | r = dns_search_domain_find(m->search_domains, domain, &d); | |
89 | if (r < 0) | |
90 | return r; | |
91 | if (r > 0) { | |
92 | dns_search_domain_move_back_and_unmark(d); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | return dns_search_domain_new(m, NULL, DNS_SEARCH_DOMAIN_SYSTEM, NULL, domain); | |
97 | } | |
98 | ||
99 | int manager_parse_search_domains_and_warn(Manager *m, const char *string) { | |
100 | int r; | |
101 | ||
102 | assert(m); | |
103 | assert(string); | |
104 | ||
105 | for(;;) { | |
106 | _cleanup_free_ char *word = NULL; | |
107 | ||
108 | r = extract_first_word(&string, &word, NULL, EXTRACT_QUOTES); | |
109 | if (r < 0) | |
110 | return r; | |
111 | if (r == 0) | |
112 | break; | |
113 | ||
114 | r = manager_add_search_domain_by_string(m, word); | |
115 | if (r < 0) | |
116 | log_warning_errno(r, "Failed to add search domain '%s', ignoring.", word); | |
4e945a6f LP |
117 | } |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
636e813d | 122 | int config_parse_dns_servers( |
4e945a6f LP |
123 | const char *unit, |
124 | const char *filename, | |
125 | unsigned line, | |
126 | const char *section, | |
127 | unsigned section_line, | |
128 | const char *lvalue, | |
129 | int ltype, | |
130 | const char *rvalue, | |
131 | void *data, | |
132 | void *userdata) { | |
133 | ||
134 | Manager *m = userdata; | |
4e945a6f LP |
135 | int r; |
136 | ||
137 | assert(filename); | |
138 | assert(lvalue); | |
139 | assert(rvalue); | |
140 | assert(m); | |
141 | ||
3e684349 | 142 | if (isempty(rvalue)) |
5cb36f41 | 143 | /* Empty assignment means clear the list */ |
4b95f179 | 144 | dns_server_unlink_all(manager_get_first_dns_server(m, ltype)); |
3e684349 | 145 | else { |
b938cb90 | 146 | /* Otherwise, add to the list */ |
636e813d | 147 | r = manager_parse_dns_server_string_and_warn(m, ltype, rvalue); |
5cb36f41 | 148 | if (r < 0) { |
12ca818f | 149 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse DNS server string '%s'. Ignoring.", rvalue); |
5cb36f41 LP |
150 | return 0; |
151 | } | |
4e945a6f LP |
152 | } |
153 | ||
5cb36f41 LP |
154 | /* If we have a manual setting, then we stop reading |
155 | * /etc/resolv.conf */ | |
156 | if (ltype == DNS_SERVER_SYSTEM) | |
157 | m->read_resolv_conf = false; | |
00fa60ae LP |
158 | if (ltype == DNS_SERVER_FALLBACK) |
159 | m->need_builtin_fallbacks = false; | |
5cb36f41 | 160 | |
4e945a6f LP |
161 | return 0; |
162 | } | |
163 | ||
a51c1048 LP |
164 | int config_parse_search_domains( |
165 | const char *unit, | |
166 | const char *filename, | |
167 | unsigned line, | |
168 | const char *section, | |
169 | unsigned section_line, | |
170 | const char *lvalue, | |
171 | int ltype, | |
172 | const char *rvalue, | |
173 | void *data, | |
174 | void *userdata) { | |
175 | ||
176 | Manager *m = userdata; | |
177 | int r; | |
178 | ||
179 | assert(filename); | |
180 | assert(lvalue); | |
181 | assert(rvalue); | |
182 | assert(m); | |
183 | ||
184 | if (isempty(rvalue)) | |
185 | /* Empty assignment means clear the list */ | |
186 | dns_search_domain_unlink_all(m->search_domains); | |
187 | else { | |
188 | /* Otherwise, add to the list */ | |
189 | r = manager_parse_search_domains_and_warn(m, rvalue); | |
190 | if (r < 0) { | |
191 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse search domains string '%s'. Ignoring.", rvalue); | |
192 | return 0; | |
193 | } | |
194 | } | |
195 | ||
196 | /* If we have a manual setting, then we stop reading | |
197 | * /etc/resolv.conf */ | |
198 | m->read_resolv_conf = false; | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
4e945a6f LP |
203 | int config_parse_support( |
204 | const char *unit, | |
205 | const char *filename, | |
206 | unsigned line, | |
207 | const char *section, | |
208 | unsigned section_line, | |
209 | const char *lvalue, | |
210 | int ltype, | |
211 | const char *rvalue, | |
212 | void *data, | |
213 | void *userdata) { | |
214 | ||
4e945a6f LP |
215 | Support support, *v = data; |
216 | int r; | |
217 | ||
218 | assert(filename); | |
219 | assert(lvalue); | |
220 | assert(rvalue); | |
4e945a6f LP |
221 | |
222 | support = support_from_string(rvalue); | |
223 | if (support < 0) { | |
224 | r = parse_boolean(rvalue); | |
225 | if (r < 0) { | |
12ca818f | 226 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse support level '%s'. Ignoring.", rvalue); |
4e945a6f LP |
227 | return 0; |
228 | } | |
229 | ||
230 | support = r ? SUPPORT_YES : SUPPORT_NO; | |
231 | } | |
232 | ||
233 | *v = support; | |
234 | return 0; | |
235 | } | |
236 | ||
24710c48 LP |
237 | int config_parse_dnssec( |
238 | const char *unit, | |
239 | const char *filename, | |
240 | unsigned line, | |
241 | const char *section, | |
242 | unsigned section_line, | |
243 | const char *lvalue, | |
244 | int ltype, | |
245 | const char *rvalue, | |
246 | void *data, | |
247 | void *userdata) { | |
248 | ||
249 | Manager *m = data; | |
250 | DnssecMode mode; | |
251 | int r; | |
252 | ||
253 | assert(filename); | |
254 | assert(lvalue); | |
255 | assert(rvalue); | |
256 | ||
257 | mode = dnssec_mode_from_string(rvalue); | |
258 | if (mode < 0) { | |
259 | r = parse_boolean(rvalue); | |
260 | if (r < 0) { | |
261 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse DNSSEC mode '%s'. Ignoring.", rvalue); | |
262 | return 0; | |
263 | } | |
264 | ||
265 | mode = r ? DNSSEC_YES : DNSSEC_NO; | |
266 | } | |
267 | ||
268 | m->unicast_scope->dnssec_mode = mode; | |
269 | return 0; | |
270 | } | |
271 | ||
4e945a6f | 272 | int manager_parse_config_file(Manager *m) { |
00fa60ae LP |
273 | int r; |
274 | ||
4e945a6f LP |
275 | assert(m); |
276 | ||
00fa60ae LP |
277 | r = config_parse_many(PKGSYSCONFDIR "/resolved.conf", |
278 | CONF_PATHS_NULSTR("systemd/resolved.conf.d"), | |
279 | "Resolve\0", | |
280 | config_item_perf_lookup, resolved_gperf_lookup, | |
281 | false, m); | |
282 | if (r < 0) | |
283 | return r; | |
284 | ||
285 | if (m->need_builtin_fallbacks) { | |
286 | r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_FALLBACK, DNS_SERVERS); | |
287 | if (r < 0) | |
288 | return r; | |
289 | } | |
290 | ||
291 | return 0; | |
292 | ||
4e945a6f | 293 | } |