]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-conf.c
Merge pull request #3456 from poettering/ipv6-ra-rename
[thirdparty/systemd.git] / src / resolve / resolved-conf.c
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
20 #include "alloc-util.h"
21 #include "conf-parser.h"
22 #include "def.h"
23 #include "extract-word.h"
24 #include "parse-util.h"
25 #include "resolved-conf.h"
26 #include "string-util.h"
27
28 int manager_add_dns_server_by_string(Manager *m, DnsServerType type, const char *word) {
29 union in_addr_union address;
30 int family, r, ifindex = 0;
31 DnsServer *s;
32
33 assert(m);
34 assert(word);
35
36 r = in_addr_ifindex_from_string_auto(word, &family, &address, &ifindex);
37 if (r < 0)
38 return r;
39
40 /* Filter out duplicates */
41 s = dns_server_find(manager_get_first_dns_server(m, type), family, &address, ifindex);
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 */
49 dns_server_move_back_and_unmark(s);
50 return 0;
51 }
52
53 return dns_server_new(m, NULL, type, NULL, family, &address, ifindex);
54 }
55
56 int 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
62 for (;;) {
63 _cleanup_free_ char *word = NULL;
64
65 r = extract_first_word(&string, &word, NULL, 0);
66 if (r < 0)
67 return r;
68 if (r == 0)
69 break;
70
71 r = manager_add_dns_server_by_string(m, type, word);
72 if (r < 0)
73 log_warning_errno(r, "Failed to add DNS server address '%s', ignoring: %m", word);
74 }
75
76 return 0;
77 }
78
79 int manager_add_search_domain_by_string(Manager *m, const char *domain) {
80 DnsSearchDomain *d;
81 bool route_only;
82 int r;
83
84 assert(m);
85 assert(domain);
86
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
96 r = dns_search_domain_find(m->search_domains, domain, &d);
97 if (r < 0)
98 return r;
99 if (r > 0)
100 dns_search_domain_move_back_and_unmark(d);
101 else {
102 r = dns_search_domain_new(m, &d, DNS_SEARCH_DOMAIN_SYSTEM, NULL, domain);
103 if (r < 0)
104 return r;
105 }
106
107 d->route_only = route_only;
108 return 0;
109 }
110
111 int manager_parse_search_domains_and_warn(Manager *m, const char *string) {
112 int r;
113
114 assert(m);
115 assert(string);
116
117 for (;;) {
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)
128 log_warning_errno(r, "Failed to add search domain '%s', ignoring: %m", word);
129 }
130
131 return 0;
132 }
133
134 int config_parse_dns_servers(
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;
147 int r;
148
149 assert(filename);
150 assert(lvalue);
151 assert(rvalue);
152 assert(m);
153
154 if (isempty(rvalue))
155 /* Empty assignment means clear the list */
156 dns_server_unlink_all(manager_get_first_dns_server(m, ltype));
157 else {
158 /* Otherwise, add to the list */
159 r = manager_parse_dns_server_string_and_warn(m, ltype, rvalue);
160 if (r < 0) {
161 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse DNS server string '%s'. Ignoring.", rvalue);
162 return 0;
163 }
164 }
165
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;
170 if (ltype == DNS_SERVER_FALLBACK)
171 m->need_builtin_fallbacks = false;
172
173 return 0;
174 }
175
176 int 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
215 int manager_parse_config_file(Manager *m) {
216 int r;
217
218 assert(m);
219
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
236 }