]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-conf.c
Merge pull request #1975 from ssahani/vxlan2
[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 = dns_server_find(manager_get_first_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 bool route_only;
84 int r;
85
86 assert(m);
87 assert(domain);
88
89 route_only = *domain == '~';
90 if (route_only)
91 domain++;
92
93 if (dns_name_is_root(domain) || streq(domain, "*")) {
94 route_only = true;
95 domain = ".";
96 }
97
98 r = dns_search_domain_find(m->search_domains, domain, &d);
99 if (r < 0)
100 return r;
101 if (r > 0)
102 dns_search_domain_move_back_and_unmark(d);
103 else {
104 r = dns_search_domain_new(m, &d, DNS_SEARCH_DOMAIN_SYSTEM, NULL, domain);
105 if (r < 0)
106 return r;
107 }
108
109 d->route_only = route_only;
110 return 0;
111 }
112
113 int manager_parse_search_domains_and_warn(Manager *m, const char *string) {
114 int r;
115
116 assert(m);
117 assert(string);
118
119 for(;;) {
120 _cleanup_free_ char *word = NULL;
121
122 r = extract_first_word(&string, &word, NULL, EXTRACT_QUOTES);
123 if (r < 0)
124 return r;
125 if (r == 0)
126 break;
127
128 r = manager_add_search_domain_by_string(m, word);
129 if (r < 0)
130 log_warning_errno(r, "Failed to add search domain '%s', ignoring.", word);
131 }
132
133 return 0;
134 }
135
136 int config_parse_dns_servers(
137 const char *unit,
138 const char *filename,
139 unsigned line,
140 const char *section,
141 unsigned section_line,
142 const char *lvalue,
143 int ltype,
144 const char *rvalue,
145 void *data,
146 void *userdata) {
147
148 Manager *m = userdata;
149 int r;
150
151 assert(filename);
152 assert(lvalue);
153 assert(rvalue);
154 assert(m);
155
156 if (isempty(rvalue))
157 /* Empty assignment means clear the list */
158 dns_server_unlink_all(manager_get_first_dns_server(m, ltype));
159 else {
160 /* Otherwise, add to the list */
161 r = manager_parse_dns_server_string_and_warn(m, ltype, rvalue);
162 if (r < 0) {
163 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse DNS server string '%s'. Ignoring.", rvalue);
164 return 0;
165 }
166 }
167
168 /* If we have a manual setting, then we stop reading
169 * /etc/resolv.conf */
170 if (ltype == DNS_SERVER_SYSTEM)
171 m->read_resolv_conf = false;
172 if (ltype == DNS_SERVER_FALLBACK)
173 m->need_builtin_fallbacks = false;
174
175 return 0;
176 }
177
178 int config_parse_search_domains(
179 const char *unit,
180 const char *filename,
181 unsigned line,
182 const char *section,
183 unsigned section_line,
184 const char *lvalue,
185 int ltype,
186 const char *rvalue,
187 void *data,
188 void *userdata) {
189
190 Manager *m = userdata;
191 int r;
192
193 assert(filename);
194 assert(lvalue);
195 assert(rvalue);
196 assert(m);
197
198 if (isempty(rvalue))
199 /* Empty assignment means clear the list */
200 dns_search_domain_unlink_all(m->search_domains);
201 else {
202 /* Otherwise, add to the list */
203 r = manager_parse_search_domains_and_warn(m, rvalue);
204 if (r < 0) {
205 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse search domains string '%s'. Ignoring.", rvalue);
206 return 0;
207 }
208 }
209
210 /* If we have a manual setting, then we stop reading
211 * /etc/resolv.conf */
212 m->read_resolv_conf = false;
213
214 return 0;
215 }
216
217 int manager_parse_config_file(Manager *m) {
218 int r;
219
220 assert(m);
221
222 r = config_parse_many(PKGSYSCONFDIR "/resolved.conf",
223 CONF_PATHS_NULSTR("systemd/resolved.conf.d"),
224 "Resolve\0",
225 config_item_perf_lookup, resolved_gperf_lookup,
226 false, m);
227 if (r < 0)
228 return r;
229
230 if (m->need_builtin_fallbacks) {
231 r = manager_parse_dns_server_string_and_warn(m, DNS_SERVER_FALLBACK, DNS_SERVERS);
232 if (r < 0)
233 return r;
234 }
235
236 return 0;
237
238 }