]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-conf.c
resolved: add a generic DnsSearchDomain concept
[thirdparty/systemd.git] / src / resolve / resolved-conf.c
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
22 #include "alloc-util.h"
23 #include "conf-parser.h"
24 #include "def.h"
25 #include "extract-word.h"
26 #include "parse-util.h"
27 #include "resolved-conf.h"
28 #include "string-util.h"
29
30 int manager_add_dns_server_by_string(Manager *m, DnsServerType type, const char *word) {
31 union in_addr_union address;
32 int family, r;
33 DnsServer *s;
34
35 assert(m);
36 assert(word);
37
38 r = in_addr_from_string_auto(word, &family, &address);
39 if (r < 0)
40 return r;
41
42 /* Filter out duplicates */
43 s = manager_find_dns_server(m, type, family, &address);
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 */
51 dns_server_move_back_and_unmark(s);
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
64 for(;;) {
65 _cleanup_free_ char *word = NULL;
66
67 r = extract_first_word(&string, &word, NULL, 0);
68 if (r < 0)
69 return r;
70 if (r == 0)
71 break;
72
73 r = manager_add_dns_server_by_string(m, type, word);
74 if (r < 0)
75 log_warning_errno(r, "Failed to add DNS server address '%s', ignoring.", word);
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);
117 }
118
119 return 0;
120 }
121
122 int config_parse_dns_servers(
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;
135 int r;
136
137 assert(filename);
138 assert(lvalue);
139 assert(rvalue);
140 assert(m);
141
142 if (isempty(rvalue))
143 /* Empty assignment means clear the list */
144 manager_flush_dns_servers(m, ltype);
145 else {
146 /* Otherwise, add to the list */
147 r = manager_parse_dns_server_string_and_warn(m, ltype, rvalue);
148 if (r < 0) {
149 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse DNS server string '%s'. Ignoring.", rvalue);
150 return 0;
151 }
152 }
153
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;
158 if (ltype == DNS_SERVER_FALLBACK)
159 m->need_builtin_fallbacks = false;
160
161 return 0;
162 }
163
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
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
215 Support support, *v = data;
216 int r;
217
218 assert(filename);
219 assert(lvalue);
220 assert(rvalue);
221
222 support = support_from_string(rvalue);
223 if (support < 0) {
224 r = parse_boolean(rvalue);
225 if (r < 0) {
226 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse support level '%s'. Ignoring.", rvalue);
227 return 0;
228 }
229
230 support = r ? SUPPORT_YES : SUPPORT_NO;
231 }
232
233 *v = support;
234 return 0;
235 }
236
237 int manager_parse_config_file(Manager *m) {
238 int r;
239
240 assert(m);
241
242 r = config_parse_many(PKGSYSCONFDIR "/resolved.conf",
243 CONF_PATHS_NULSTR("systemd/resolved.conf.d"),
244 "Resolve\0",
245 config_item_perf_lookup, resolved_gperf_lookup,
246 false, m);
247 if (r < 0)
248 return r;
249
250 if (m->need_builtin_fallbacks) {
251 r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_FALLBACK, DNS_SERVERS);
252 if (r < 0)
253 return r;
254 }
255
256 return 0;
257
258 }