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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* Copyright (C) 2010 Dan Williams <dcbw@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <config.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <glib.h>
#include <glib/gi18n.h>
#include "nm-dns-dnsmasq.h"
#include "nm-utils.h"
#include "nm-logging.h"
#include "nm-ip4-config.h"
#include "nm-ip6-config.h"
#include "nm-dns-utils.h"
G_DEFINE_TYPE (NMDnsDnsmasq, nm_dns_dnsmasq, NM_TYPE_DNS_PLUGIN)
#define NM_DNS_DNSMASQ_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DNS_DNSMASQ, NMDnsDnsmasqPrivate))
#define PIDFILE NMRUNDIR "/dnsmasq.pid"
#define CONFFILE NMRUNDIR "/dnsmasq.conf"
#define CONFDIR NMCONFDIR "/dnsmasq.d"
typedef struct {
guint32 foo;
} NMDnsDnsmasqPrivate;
/*******************************************/
static inline const char *
find_dnsmasq (void)
{
static const char *paths[] = {
DNSMASQ_PATH,
"/usr/local/sbin/dnsmasq",
"/usr/sbin/dnsmasq",
"/sbin/dnsmasq",
NULL
};
const char **binary = paths;
while (*binary != NULL) {
if (**binary && g_file_test (*binary, G_FILE_TEST_EXISTS))
return *binary;
binary++;
}
return NULL;
}
static gboolean
add_ip4_config (GString *str, NMIP4Config *ip4, gboolean split)
{
char buf[INET_ADDRSTRLEN];
in_addr_t addr;
int nnameservers, i_nameserver, n, i;
gboolean added = FALSE;
nnameservers = nm_ip4_config_get_num_nameservers (ip4);
if (split) {
char **domains, **iter;
if (nnameservers == 0)
return FALSE;
for (i_nameserver = 0; i_nameserver < nnameservers; i_nameserver++) {
addr = nm_ip4_config_get_nameserver (ip4, i_nameserver);
nm_utils_inet4_ntop (addr, buf);
/* searches are preferred over domains */
n = nm_ip4_config_get_num_searches (ip4);
for (i = 0; i < n; i++) {
g_string_append_printf (str, "server=/%s/%s\n",
nm_ip4_config_get_search (ip4, i),
buf);
added = TRUE;
}
if (n == 0) {
/* If not searches, use any domains */
n = nm_ip4_config_get_num_domains (ip4);
for (i = 0; i < n; i++) {
g_string_append_printf (str, "server=/%s/%s\n",
nm_ip4_config_get_domain (ip4, i),
buf);
added = TRUE;
}
}
/* Ensure reverse-DNS works by directing queries for in-addr.arpa
* domains to the split domain's nameserver.
*/
domains = nm_dns_utils_get_ip4_rdns_domains (ip4);
if (domains) {
for (iter = domains; iter && *iter; iter++)
g_string_append_printf (str, "server=/%s/%s\n", *iter, buf);
g_strfreev (domains);
added = TRUE;
}
}
}
/* If no searches or domains, just add the namservers */
if (!added) {
for (i = 0; i < nnameservers; i++) {
addr = nm_ip4_config_get_nameserver (ip4, i);
g_string_append_printf (str, "server=%s\n", nm_utils_inet4_ntop (addr, NULL));
}
}
return TRUE;
}
static char *
ip6_addr_to_string (const struct in6_addr *addr, const char *iface)
{
char *buf;
if (IN6_IS_ADDR_V4MAPPED (addr)) {
/* inet_ntop is probably supposed to do this for us, but it doesn't */
buf = g_malloc (INET_ADDRSTRLEN);
nm_utils_inet4_ntop (addr->s6_addr32[3], buf);
} else if (!iface || !iface[0] || !IN6_IS_ADDR_LINKLOCAL (addr)) {
buf = g_malloc (INET6_ADDRSTRLEN);
nm_utils_inet6_ntop (addr, buf);
} else {
/* If we got a scope identifier, we need use '%' instead of
* '@', since dnsmasq supports '%' in server= addresses
* only since version 2.58 and up
*/
buf = g_strconcat (nm_utils_inet6_ntop (addr, NULL), "@", iface, NULL);
}
return buf;
}
static gboolean
add_ip6_config (GString *str, NMIP6Config *ip6, gboolean split)
{
const struct in6_addr *addr;
char *buf = NULL;
int nnameservers, i_nameserver, n, i;
gboolean added = FALSE;
const char *iface;
nnameservers = nm_ip6_config_get_num_nameservers (ip6);
iface = g_object_get_data (G_OBJECT (ip6), IP_CONFIG_IFACE_TAG);
g_assert (iface);
if (split) {
if (nnameservers == 0)
return FALSE;
for (i_nameserver = 0; i_nameserver < nnameservers; i_nameserver++) {
addr = nm_ip6_config_get_nameserver (ip6, i_nameserver);
buf = ip6_addr_to_string (addr, iface);
/* searches are preferred over domains */
n = nm_ip6_config_get_num_searches (ip6);
for (i = 0; i < n; i++) {
g_string_append_printf (str, "server=/%s/%s\n",
nm_ip6_config_get_search (ip6, i),
buf);
added = TRUE;
}
if (n == 0) {
/* If not searches, use any domains */
n = nm_ip6_config_get_num_domains (ip6);
for (i = 0; i < n; i++) {
g_string_append_printf (str, "server=/%s/%s\n",
nm_ip6_config_get_domain (ip6, i),
buf);
added = TRUE;
}
}
g_free (buf);
}
}
/* If no searches or domains, just add the namservers */
if (!added) {
for (i = 0; i < nnameservers; i++) {
addr = nm_ip6_config_get_nameserver (ip6, i);
buf = ip6_addr_to_string (addr, iface);
if (buf) {
g_string_append_printf (str, "server=%s\n", buf);
g_free (buf);
}
}
}
return TRUE;
}
static gboolean
update (NMDnsPlugin *plugin,
const GSList *vpn_configs,
const GSList *dev_configs,
const GSList *other_configs,
const char *hostname)
{
NMDnsDnsmasq *self = NM_DNS_DNSMASQ (plugin);
GString *conf;
GSList *iter;
const char *argv[15];
GError *error = NULL;
int ignored;
GPid pid = 0;
guint idx = 0;
/* Kill the old dnsmasq; there doesn't appear to be a way to get dnsmasq
* to reread the config file using SIGHUP or similar. This is a small race
* here when restarting dnsmasq when DNS requests could go to the upstream
* servers instead of to dnsmasq.
*/
nm_dns_plugin_child_kill (plugin);
/* Build up the new dnsmasq config file */
conf = g_string_sized_new (150);
/* Use split DNS for VPN configs */
for (iter = (GSList *) vpn_configs; iter; iter = g_slist_next (iter)) {
if (NM_IS_IP4_CONFIG (iter->data))
add_ip4_config (conf, NM_IP4_CONFIG (iter->data), TRUE);
else if (NM_IS_IP6_CONFIG (iter->data))
add_ip6_config (conf, NM_IP6_CONFIG (iter->data), TRUE);
}
/* Now add interface configs without split DNS */
for (iter = (GSList *) dev_configs; iter; iter = g_slist_next (iter)) {
if (NM_IS_IP4_CONFIG (iter->data))
add_ip4_config (conf, NM_IP4_CONFIG (iter->data), FALSE);
else if (NM_IS_IP6_CONFIG (iter->data))
add_ip6_config (conf, NM_IP6_CONFIG (iter->data), FALSE);
}
/* And any other random configs */
for (iter = (GSList *) other_configs; iter; iter = g_slist_next (iter)) {
if (NM_IS_IP4_CONFIG (iter->data))
add_ip4_config (conf, NM_IP4_CONFIG (iter->data), FALSE);
else if (NM_IS_IP6_CONFIG (iter->data))
add_ip6_config (conf, NM_IP6_CONFIG (iter->data), FALSE);
}
/* Write out the config file */
if (!g_file_set_contents (CONFFILE, conf->str, -1, &error)) {
nm_log_warn (LOGD_DNS, "Failed to write dnsmasq config file %s: (%d) %s",
CONFFILE,
error ? error->code : -1,
error && error->message ? error->message : "(unknown)");
g_clear_error (&error);
goto out;
}
ignored = chmod (CONFFILE, 0644);
nm_log_dbg (LOGD_DNS, "dnsmasq local caching DNS configuration:");
nm_log_dbg (LOGD_DNS, "%s", conf->str);
argv[idx++] = find_dnsmasq ();
argv[idx++] = "--no-resolv"; /* Use only commandline */
argv[idx++] = "--keep-in-foreground";
argv[idx++] = "--no-hosts"; /* don't use /etc/hosts to resolve */
argv[idx++] = "--bind-interfaces";
argv[idx++] = "--pid-file=" PIDFILE;
argv[idx++] = "--listen-address=127.0.0.1"; /* Should work for both 4 and 6 */
argv[idx++] = "--conf-file=" CONFFILE;
argv[idx++] = "--cache-size=400";
argv[idx++] = "--proxy-dnssec"; /* Allow DNSSEC to pass through */
/* dnsmasq exits if the conf dir is not present */
if (g_file_test (CONFDIR, G_FILE_TEST_IS_DIR))
argv[idx++] = "--conf-dir=" CONFDIR;
argv[idx++] = NULL;
g_warn_if_fail (idx <= G_N_ELEMENTS (argv));
/* And finally spawn dnsmasq */
pid = nm_dns_plugin_child_spawn (NM_DNS_PLUGIN (self), argv, PIDFILE, "bin/dnsmasq");
out:
g_string_free (conf, TRUE);
return pid ? TRUE : FALSE;
}
/****************************************************************/
static const char *
dm_exit_code_to_msg (int status)
{
if (status == 1)
return "Configuration problem";
else if (status == 2)
return "Network access problem (address in use; permissions; etc)";
else if (status == 3)
return "Filesystem problem (missing file/directory; permissions; etc)";
else if (status == 4)
return "Memory allocation failure";
else if (status == 5)
return "Other problem";
else if (status >= 11)
return "Lease-script 'init' process failure";
return "Unknown error";
}
static void
child_quit (NMDnsPlugin *plugin, gint status)
{
NMDnsDnsmasq *self = NM_DNS_DNSMASQ (plugin);
gboolean failed = TRUE;
int err;
if (WIFEXITED (status)) {
err = WEXITSTATUS (status);
if (err) {
nm_log_warn (LOGD_DNS, "dnsmasq exited with error: %s (%d)",
dm_exit_code_to_msg (err),
err);
} else
failed = FALSE;
} else if (WIFSTOPPED (status)) {
nm_log_warn (LOGD_DNS, "dnsmasq stopped unexpectedly with signal %d", WSTOPSIG (status));
} else if (WIFSIGNALED (status)) {
nm_log_warn (LOGD_DNS, "dnsmasq died with signal %d", WTERMSIG (status));
} else {
nm_log_warn (LOGD_DNS, "dnsmasq died from an unknown cause");
}
unlink (CONFFILE);
if (failed)
g_signal_emit_by_name (self, NM_DNS_PLUGIN_FAILED);
}
/****************************************************************/
static gboolean
is_caching (NMDnsPlugin *plugin)
{
return TRUE;
}
static const char *
get_name (NMDnsPlugin *plugin)
{
return "dnsmasq";
}
/****************************************************************/
NMDnsPlugin *
nm_dns_dnsmasq_new (void)
{
return g_object_new (NM_TYPE_DNS_DNSMASQ, NULL);
}
static void
nm_dns_dnsmasq_init (NMDnsDnsmasq *self)
{
}
static void
dispose (GObject *object)
{
unlink (CONFFILE);
G_OBJECT_CLASS (nm_dns_dnsmasq_parent_class)->dispose (object);
}
static void
nm_dns_dnsmasq_class_init (NMDnsDnsmasqClass *dns_class)
{
NMDnsPluginClass *plugin_class = NM_DNS_PLUGIN_CLASS (dns_class);
GObjectClass *object_class = G_OBJECT_CLASS (dns_class);
g_type_class_add_private (dns_class, sizeof (NMDnsDnsmasqPrivate));
object_class->dispose = dispose;
plugin_class->child_quit = child_quit;
plugin_class->is_caching = is_caching;
plugin_class->update = update;
plugin_class->get_name = get_name;
}
|