1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Tom Gundersen <teg@jklm.no>
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <arpa/inet.h>
#include <resolv.h>
#include <linux/if.h>
#include "resolved.h"
#include "event-util.h"
#include "network-util.h"
#include "sd-dhcp-lease.h"
#include "dhcp-lease-internal.h"
#include "network-internal.h"
#include "conf-parser.h"
static int set_fallback_dns(Manager *m, const char *string) {
char *word, *state;
size_t length;
int r;
assert(m);
assert(string);
FOREACH_WORD_QUOTED(word, length, string, state) {
_cleanup_free_ Address *address = NULL;
Address *tail;
_cleanup_free_ char *addrstr = NULL;
address = new0(Address, 1);
if (!address)
return -ENOMEM;
addrstr = strndup(word, length);
if (!addrstr)
return -ENOMEM;
r = net_parse_inaddr(addrstr, &address->family, &address->in_addr);
if (r < 0) {
log_debug("Ignoring invalid DNS address '%s'", addrstr);
continue;
}
LIST_FIND_TAIL(addresses, m->fallback_dns, tail);
LIST_INSERT_AFTER(addresses, m->fallback_dns, tail, address);
address = NULL;
}
return 0;
}
int config_parse_dnsv(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
Manager *m = userdata;
Address *address;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(m);
while ((address = m->fallback_dns)) {
LIST_REMOVE(addresses, m->fallback_dns, address);
free(address);
}
set_fallback_dns(m, rvalue);
return 0;
}
static int manager_parse_config_file(Manager *m) {
int r;
assert(m);
r = config_parse(NULL, "/etc/systemd/resolved.conf", NULL,
"Resolve\0", config_item_perf_lookup, (void*) resolved_gperf_lookup,
false, false, m);
if (r < 0)
log_warning("Failed to parse configuration file: %s", strerror(-r));
return r;
}
int manager_new(Manager **ret) {
_cleanup_manager_free_ Manager *m = NULL;
int r;
m = new0(Manager, 1);
if (!m)
return -ENOMEM;
r = set_fallback_dns(m, DNS_SERVERS);
if (r < 0)
return r;
r = manager_parse_config_file(m);
if (r < 0)
return r;
r = sd_event_default(&m->event);
if (r < 0)
return r;
sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
sd_event_set_watchdog(m->event, true);
*ret = m;
m = NULL;
return 0;
}
void manager_free(Manager *m) {
Address *address;
if (!m)
return;
sd_event_unref(m->event);
while ((address = m->fallback_dns)) {
LIST_REMOVE(addresses, m->fallback_dns, address);
free(address);
}
free(m);
}
static void append_dns(FILE *f, void *dns, unsigned char family, unsigned *count) {
char buf[INET6_ADDRSTRLEN];
const char *address;
assert(f);
assert(dns);
assert(count);
address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN);
if (!address) {
log_warning("Invalid DNS address. Ignoring.");
return;
}
if (*count == MAXNS)
fputs("# Too many DNS servers configured, the following entries "
"may be ignored\n", f);
fprintf(f, "nameserver %s\n", address);
(*count) ++;
}
int manager_update_resolv_conf(Manager *m) {
const char *path = "/run/systemd/resolve/resolv.conf";
_cleanup_free_ char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ unsigned *indices = NULL;
Address *address;
unsigned count = 0;
int n, r, i;
assert(m);
r = fopen_temporary(path, &f, &temp_path);
if (r < 0)
return r;
fchmod(fileno(f), 0644);
fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
"# Third party programs must not access this file directly, but\n"
"# only through the symlink at /etc/resolv.conf. To manage\n"
"# resolv.conf(5) in a different way, replace the symlink by a\n"
"# static file or a different symlink.\n\n", f);
n = sd_network_get_ifindices(&indices);
if (n < 0)
n = 0;
for (i = 0; i < n; i++) {
_cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
struct in_addr *nameservers;
struct in6_addr *nameservers6;
size_t nameservers_size;
r = sd_network_dhcp_use_dns(indices[i]);
if (r > 0) {
r = sd_network_get_dhcp_lease(indices[i], &lease);
if (r >= 0) {
r = sd_dhcp_lease_get_dns(lease, &nameservers, &nameservers_size);
if (r >= 0) {
unsigned j;
for (j = 0; j < nameservers_size; j++)
append_dns(f, &nameservers[j], AF_INET, &count);
}
}
}
r = sd_network_get_dns(indices[i], &nameservers, &nameservers_size);
if (r >= 0) {
unsigned j;
for (j = 0; j < nameservers_size; j++)
append_dns(f, &nameservers[j], AF_INET, &count);
free(nameservers);
}
r = sd_network_get_dns6(indices[i], &nameservers6, &nameservers_size);
if (r >= 0) {
unsigned j;
for (j = 0; j < nameservers_size; j++)
append_dns(f, &nameservers6[j], AF_INET6, &count);
free(nameservers6);
}
}
LIST_FOREACH(addresses, address, m->fallback_dns)
append_dns(f, &address->in_addr, address->family, &count);
fflush(f);
if (ferror(f) || rename(temp_path, path) < 0) {
r = -errno;
unlink(path);
unlink(temp_path);
return r;
}
return 0;
}
static int manager_network_event_handler(sd_event_source *s, int fd, uint32_t revents,
void *userdata) {
Manager *m = userdata;
int r;
assert(m);
r = manager_update_resolv_conf(m);
if (r < 0)
log_warning("Could not update resolv.conf: %s", strerror(-r));
sd_network_monitor_flush(m->network_monitor);
return 0;
}
int manager_network_monitor_listen(Manager *m) {
_cleanup_event_source_unref_ sd_event_source *event_source = NULL;
_cleanup_network_monitor_unref_ sd_network_monitor *monitor = NULL;
int r, fd, events;
r = sd_network_monitor_new(NULL, &monitor);
if (r < 0)
return r;
fd = sd_network_monitor_get_fd(monitor);
if (fd < 0)
return fd;
events = sd_network_monitor_get_events(monitor);
if (events < 0)
return events;
r = sd_event_add_io(m->event, &event_source, fd, events,
&manager_network_event_handler, m);
if (r < 0)
return r;
m->network_monitor = monitor;
m->network_event_source = event_source;
monitor = NULL;
event_source = NULL;
return 0;
}
|