summaryrefslogtreecommitdiff
path: root/src/resolver.c
blob: 59adeab4b022e01089eac2d80035d00f9fc6c081 (plain)
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
409
410
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>

#include "connman.h"

#define RESOLVER_FLAG_PUBLIC (1 << 0)

struct entry_data {
	struct connman_resolver *resolver;
	char *interface;
	char *domain;
	char *server;
	unsigned int flags;
};

static GSList *entry_list = NULL;
static GSList *resolver_list = NULL;

static void remove_entries(GSList *entries)
{
	GSList *list;

	for (list = entries; list; list = list->next) {
		struct entry_data *entry = list->data;
		struct connman_resolver *resolver = entry->resolver;

		entry_list = g_slist_remove(entry_list, entry);

		if (resolver && resolver->remove)
			resolver->remove(entry->interface, entry->domain,
								entry->server);

		g_free(entry->server);
		g_free(entry->domain);
		g_free(entry->interface);
		g_free(entry);
	}

	g_slist_free(entries);
}

static gint compare_priority(gconstpointer a, gconstpointer b)
{
	const struct connman_resolver *resolver1 = a;
	const struct connman_resolver *resolver2 = b;

	return resolver2->priority - resolver1->priority;
}

/**
 * connman_resolver_register:
 * @resolver: resolver module
 *
 * Register a new resolver module
 *
 * Returns: %0 on success
 */
int connman_resolver_register(struct connman_resolver *resolver)
{
	GSList *list;

	DBG("resolver %p name %s", resolver, resolver->name);

	resolver_list = g_slist_insert_sorted(resolver_list, resolver,
							compare_priority);

	if (resolver->append == NULL)
		return 0;

	for (list = entry_list; list; list = list->next) {
		struct entry_data *entry = list->data;

		if (entry->resolver)
			continue;

		if (resolver->append(entry->interface, entry->domain,
							entry->server) == 0)
			entry->resolver = resolver;
	}

	return 0;
}

/**
 * connman_resolver_unregister:
 * @resolver: resolver module
 *
 * Remove a previously registered resolver module
 */
void connman_resolver_unregister(struct connman_resolver *resolver)
{
	GSList *list, *matches = NULL;

	DBG("resolver %p name %s", resolver, resolver->name);

	resolver_list = g_slist_remove(resolver_list, resolver);

	for (list = entry_list; list; list = list->next) {
		struct entry_data *entry = list->data;

		if (entry->resolver != resolver)
			continue;

		matches = g_slist_append(matches, entry);
	}

	remove_entries(matches);
}

static int append_resolver(const char *interface, const char *domain,
					const char *server, unsigned int flags)
{
	struct entry_data *entry;
	GSList *list;

	DBG("interface %s domain %s server %s flags %d",
					interface, domain, server, flags);

	if (server == NULL)
		return -EINVAL;

	entry = g_try_new0(struct entry_data, 1);
	if (entry == NULL)
		return -ENOMEM;

	entry->interface = g_strdup(interface);
	entry->domain = g_strdup(domain);
	entry->server = g_strdup(server);
	entry->flags = flags;

	entry_list = g_slist_append(entry_list, entry);

	for (list = resolver_list; list; list = list->next) {
		struct connman_resolver *resolver = list->data;

		if (resolver->append == NULL)
			continue;

		if (resolver->append(interface, domain, server) == 0) {
			entry->resolver = resolver;
			break;
		}
	}

	return 0;
}

/**
 * connman_resolver_append:
 * @interface: network interface
 * @domain: domain limitation
 * @server: server address
 *
 * Append resolver server address to current list
 */
int connman_resolver_append(const char *interface, const char *domain,
							const char *server)
{
	DBG("interface %s domain %s server %s", interface, domain, server);

	return append_resolver(interface, domain, server, 0);
}

/**
 * connman_resolver_remove:
 * @interface: network interface
 * @domain: domain limitation
 * @server: server address
 *
 * Remover resolver server address from current list
 */
int connman_resolver_remove(const char *interface, const char *domain,
							const char *server)
{
	GSList *list, *matches = NULL;

	DBG("interface %s domain %s server %s", interface, domain, server);

	if (server == NULL)
		return -EINVAL;

	for (list = entry_list; list; list = list->next) {
		struct entry_data *entry = list->data;

		if (interface != NULL &&
				g_strcmp0(entry->interface, interface) != 0)
			continue;

		if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
			continue;

		if (g_strcmp0(entry->server, server) != 0)
			continue;

		matches = g_slist_append(matches, entry);
	}

	if (matches == NULL)
		return -ENOENT;

	remove_entries(matches);

	return 0;
}

/**
 * connman_resolver_remove_all:
 * @interface: network interface
 *
 * Remove all resolver server address for the specified interface
 */
int connman_resolver_remove_all(const char *interface)
{
	GSList *list, *matches = NULL;

	DBG("interface %s", interface);

	if (interface == NULL)
		return -EINVAL;

	for (list = entry_list; list; list = list->next) {
		struct entry_data *entry = list->data;

		if (g_strcmp0(entry->interface, interface) != 0)
			continue;

		matches = g_slist_append(matches, entry);
	}

	if (matches == NULL)
		return -ENOENT;

	remove_entries(matches);

	return 0;
}

/**
 * connman_resolver_append_public_server:
 * @server: server address
 *
 * Append public resolver server address to current list
 */
int connman_resolver_append_public_server(const char *server)
{
	DBG("server %s", server);

	return append_resolver(NULL, NULL, server, RESOLVER_FLAG_PUBLIC);
}

/**
 * connman_resolver_remove_public_server:
 * @server: server address
 *
 * Remove public resolver server address to current list
 */
int connman_resolver_remove_public_server(const char *server)
{
	DBG("server %s", server);

	return connman_resolver_remove(NULL, NULL, server);
}

static int selftest_append(const char *interface, const char *domain,
							const char *server)
{
	DBG("server %s", server);

	return 0;
}

static int selftest_remove(const char *interface, const char *domain,
							const char *server)
{
	DBG("server %s", server);

	return 0;
}

static struct connman_resolver selftest_resolver = {
	.name     = "selftest",
	.priority = CONNMAN_RESOLVER_PRIORITY_HIGH + 42,
	.append   = selftest_append,
	.remove   = selftest_remove,
};

int __connman_resolver_selftest(void)
{
	connman_resolver_append("wlan0", "lwn.net", "192.168.0.1");

	connman_resolver_register(&selftest_resolver);

	connman_resolver_append("eth0", "moblin.org", "192.168.42.1");
	connman_resolver_append("wlan0", "lwn.net", "192.168.0.2");

	connman_resolver_append_public_server("8.8.8.8");

	connman_resolver_remove_public_server("8.8.8.8");

	connman_resolver_remove_all("wlan0");

	connman_resolver_unregister(&selftest_resolver);

	return 0;
}

static int resolvfile_append(const char *interface, const char *domain,
							const char *server)
{
	char *cmd;
	int fd, len, err;
	mode_t old_umask;

	DBG("interface %s server %s", interface, server);

	if (interface == NULL)
		return -ENOENT;

	old_umask = umask(022);

	fd = open("/etc/resolv.conf", O_RDWR | O_CREAT,
					S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
	if (fd < 0) {
		err = -errno;
		goto done;
	}

	if (ftruncate(fd, 0) < 0) {
		err = -errno;
		goto failed;
	}

	err = 0;

	cmd = g_strdup_printf("# Generated by Connection Manager\n"
						"options edns0\n"
						"nameserver %s\n", server);

	len = write(fd, cmd, strlen(cmd));
	if (len < 0)
		err = -errno;

	g_free(cmd);

failed:
	close(fd);

done:
	umask(old_umask);

	return err;
}

static int resolvfile_remove(const char *interface, const char *domain,
							const char *server)
{
	DBG("interface %s server %s", interface, server);

	return 0;
}

static struct connman_resolver resolvfile_resolver = {
	.name		= "resolvfile",
	.priority	= CONNMAN_RESOLVER_PRIORITY_LOW,
	.append		= resolvfile_append,
	.remove		= resolvfile_remove,
};

int __connman_resolver_init(void)
{
	DBG("");

	return connman_resolver_register(&resolvfile_resolver);
}

void __connman_resolver_cleanup(void)
{
	DBG("");

	connman_resolver_unregister(&resolvfile_resolver);
}