b965778b6508b232cd56cc7f5c78211003036601
[platform/upstream/connman.git] / src / resolver.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <resolv.h>
34 #include <netdb.h>
35
36 #include "connman.h"
37
38 #define RESOLVER_FLAG_PUBLIC (1 << 0)
39
40 /*
41  * Threshold for RDNSS lifetime. Will be used to trigger RS
42  * before RDNSS entries actually expire
43  */
44 #define RESOLVER_LIFETIME_REFRESH_THRESHOLD 0.8
45
46 struct entry_data {
47         int index;
48         char *domain;
49         char *server;
50         int family;
51         unsigned int flags;
52         unsigned int lifetime;
53         guint timeout;
54 };
55
56 static GSList *entry_list = NULL;
57 static connman_bool_t dnsproxy_enabled = FALSE;
58
59 struct resolvfile_entry {
60         int index;
61         char *domain;
62         char *server;
63 };
64
65 static GList *resolvfile_list = NULL;
66
67 static void resolvfile_remove_entries(GList *entries)
68 {
69         GList *list;
70
71         for (list = entries; list; list = list->next) {
72                 struct resolvfile_entry *entry = list->data;
73
74                 resolvfile_list = g_list_remove(resolvfile_list, entry);
75
76                 g_free(entry->server);
77                 g_free(entry->domain);
78                 g_free(entry);
79         }
80
81         g_list_free(entries);
82 }
83
84 static int resolvfile_export(void)
85 {
86         GList *list;
87         GString *content;
88         int fd, err;
89         unsigned int count;
90         mode_t old_umask;
91
92         content = g_string_new("# Generated by Connection Manager\n");
93
94         /*
95          * Domains and nameservers are added in reverse so that the most
96          * recently appended entry is the primary one. No more than
97          * MAXDNSRCH/MAXNS entries are used.
98          */
99
100         for (count = 0, list = g_list_last(resolvfile_list);
101                                                 list && (count < MAXDNSRCH);
102                                                 list = g_list_previous(list)) {
103                 struct resolvfile_entry *entry = list->data;
104
105                 if (!entry->domain)
106                         continue;
107
108                 if (count == 0)
109                         g_string_append_printf(content, "search ");
110
111                 g_string_append_printf(content, "%s ", entry->domain);
112                 count++;
113         }
114
115         if (count)
116                 g_string_append_printf(content, "\n");
117
118         for (count = 0, list = g_list_last(resolvfile_list);
119                                                 list && (count < MAXNS);
120                                                 list = g_list_previous(list)) {
121                 struct resolvfile_entry *entry = list->data;
122
123                 if (!entry->server)
124                         continue;
125
126                 g_string_append_printf(content, "nameserver %s\n",
127                                                                 entry->server);
128                 count++;
129         }
130
131         old_umask = umask(022);
132
133         fd = open("/etc/resolv.conf", O_RDWR | O_CREAT | O_CLOEXEC,
134                                         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
135         if (fd < 0) {
136                 err = -errno;
137                 goto done;
138         }
139
140         if (ftruncate(fd, 0) < 0) {
141                 err = -errno;
142                 goto failed;
143         }
144
145         err = 0;
146
147         if (write(fd, content->str, content->len) < 0)
148                 err = -errno;
149
150 failed:
151         close(fd);
152
153 done:
154         g_string_free(content, TRUE);
155         umask(old_umask);
156
157         return err;
158 }
159
160 int __connman_resolvfile_append(int index, const char *domain,
161                                                         const char *server)
162 {
163         struct resolvfile_entry *entry;
164
165         DBG("index %d server %s", index, server);
166
167         if (index < 0)
168                 return -ENOENT;
169
170         entry = g_try_new0(struct resolvfile_entry, 1);
171         if (entry == NULL)
172                 return -ENOMEM;
173
174         entry->index = index;
175         entry->domain = g_strdup(domain);
176         entry->server = g_strdup(server);
177
178         resolvfile_list = g_list_append(resolvfile_list, entry);
179
180         return resolvfile_export();
181 }
182
183 int __connman_resolvfile_remove(int index, const char *domain,
184                                                         const char *server)
185 {
186         GList *list, *matches = NULL;
187
188         DBG("index %d server %s", index, server);
189
190         for (list = resolvfile_list; list; list = g_list_next(list)) {
191                 struct resolvfile_entry *entry = list->data;
192
193                 if (index >= 0 && entry->index != index)
194                         continue;
195
196                 if (domain != NULL && g_strcmp0(entry->domain, domain) != 0)
197                         continue;
198
199                 if (g_strcmp0(entry->server, server) != 0)
200                         continue;
201
202                 matches = g_list_append(matches, entry);
203         }
204
205         resolvfile_remove_entries(matches);
206
207         return resolvfile_export();
208 }
209
210 static void remove_entries(GSList *entries)
211 {
212         GSList *list;
213
214         for (list = entries; list; list = list->next) {
215                 struct entry_data *entry = list->data;
216
217                 entry_list = g_slist_remove(entry_list, entry);
218
219                 if (dnsproxy_enabled == TRUE) {
220                         __connman_dnsproxy_remove(entry->index, entry->domain,
221                                                         entry->server);
222                 } else {
223                         __connman_resolvfile_remove(entry->index, entry->domain,
224                                                         entry->server);
225                 }
226
227                 if (entry->timeout)
228                         g_source_remove(entry->timeout);
229                 g_free(entry->server);
230                 g_free(entry->domain);
231                 g_free(entry);
232         }
233
234         g_slist_free(entries);
235 }
236
237 static gboolean resolver_expire_cb(gpointer user_data)
238 {
239         struct entry_data *entry = user_data;
240         GSList *list;
241
242         DBG("index %d domain %s server %s",
243                         entry->index, entry->domain, entry->server);
244
245         list = g_slist_prepend(NULL, entry);
246
247         if (entry->index >= 0) {
248                 struct connman_service *service;
249                 service = __connman_service_lookup_from_index(entry->index);
250                 if (service != NULL)
251                         __connman_service_nameserver_remove(service,
252                                                         entry->server, TRUE);
253         }
254
255         remove_entries(list);
256
257         return FALSE;
258 }
259
260 static gboolean resolver_refresh_cb(gpointer user_data)
261 {
262         struct entry_data *entry = user_data;
263         unsigned int interval;
264         struct connman_service *service = NULL;
265
266         /* Round up what we have left from lifetime */
267         interval = entry->lifetime *
268                 (1 - RESOLVER_LIFETIME_REFRESH_THRESHOLD) + 1.0;
269
270         DBG("RDNSS start index %d domain %s "
271                         "server %s remaining lifetime %d",
272                         entry->index, entry->domain,
273                         entry->server, interval);
274
275         entry->timeout = g_timeout_add_seconds(interval,
276                         resolver_expire_cb, entry);
277
278         if (entry->index >= 0) {
279                 service = __connman_service_lookup_from_index(entry->index);
280                 if (service != NULL) {
281                         /*
282                          * Send Router Solicitation to refresh RDNSS entries
283                          * before their lifetime expires
284                          */
285                         __connman_refresh_rs_ipv6(
286                                         __connman_service_get_network(service),
287                                         entry->index);
288                 }
289         }
290         return FALSE;
291 }
292
293 static int append_resolver(int index, const char *domain,
294                                 const char *server, unsigned int lifetime,
295                                                         unsigned int flags)
296 {
297         struct entry_data *entry;
298         unsigned int interval;
299
300         DBG("index %d domain %s server %s lifetime %d flags %d",
301                                 index, domain, server, lifetime, flags);
302
303         if (server == NULL && domain == NULL)
304                 return -EINVAL;
305
306         entry = g_try_new0(struct entry_data, 1);
307         if (entry == NULL)
308                 return -ENOMEM;
309
310         entry->index = index;
311         entry->domain = g_strdup(domain);
312         entry->server = g_strdup(server);
313         entry->flags = flags;
314         entry->lifetime = lifetime;
315
316         if (server != NULL)
317                 entry->family = connman_inet_check_ipaddress(server);
318
319         if (lifetime) {
320                 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
321
322                 DBG("RDNSS start index %d domain %s "
323                                 "server %s lifetime threshold %d",
324                                 index, domain, server, interval);
325
326                 entry->timeout = g_timeout_add_seconds(interval,
327                                 resolver_refresh_cb, entry);
328
329                 /*
330                  * We update the service only for those nameservers
331                  * that are automagically added via netlink (lifetime > 0)
332                  */
333                 if (server != NULL && entry->index >= 0) {
334                         struct connman_service *service;
335                         service = __connman_service_lookup_from_index(entry->index);
336                         if (service != NULL)
337                                 __connman_service_nameserver_append(service,
338                                                                 server, TRUE);
339                 }
340         }
341         entry_list = g_slist_append(entry_list, entry);
342
343         if (dnsproxy_enabled == TRUE)
344                 __connman_dnsproxy_append(entry->index, domain, server);
345         else
346                 __connman_resolvfile_append(entry->index, domain, server);
347
348         return 0;
349 }
350
351 /**
352  * connman_resolver_append:
353  * @index: network interface index
354  * @domain: domain limitation
355  * @server: server address
356  *
357  * Append resolver server address to current list
358  */
359 int connman_resolver_append(int index, const char *domain,
360                                                 const char *server)
361 {
362         GSList *list;
363
364         DBG("index %d domain %s server %s", index, domain, server);
365
366         if (server == NULL && domain == NULL)
367                 return -EINVAL;
368
369         for (list = entry_list; list; list = list->next) {
370                 struct entry_data *entry = list->data;
371
372                 if (entry->timeout > 0)
373                         continue;
374
375                 if (entry->index == index &&
376                                 g_strcmp0(entry->domain, domain) == 0 &&
377                                 g_strcmp0(entry->server, server) == 0)
378                         return -EEXIST;
379         }
380
381         return append_resolver(index, domain, server, 0, 0);
382 }
383
384 /**
385  * connman_resolver_append_lifetime:
386  * @index: network interface index
387  * @domain: domain limitation
388  * @server: server address
389  * @timeout: server lifetime in seconds
390  *
391  * Append resolver server address to current list
392  */
393 int connman_resolver_append_lifetime(int index, const char *domain,
394                                 const char *server, unsigned int lifetime)
395 {
396         GSList *list;
397         unsigned int interval;
398
399         DBG("index %d domain %s server %s lifetime %d",
400                                 index, domain, server, lifetime);
401
402         if (server == NULL && domain == NULL)
403                 return -EINVAL;
404
405         for (list = entry_list; list; list = list->next) {
406                 struct entry_data *entry = list->data;
407
408                 if (entry->timeout == 0 ||
409                                 entry->index != index ||
410                                 g_strcmp0(entry->domain, domain) != 0 ||
411                                 g_strcmp0(entry->server, server) != 0)
412                         continue;
413
414                 g_source_remove(entry->timeout);
415
416                 if (lifetime == 0) {
417                         resolver_expire_cb(entry);
418                         return 0;
419                 }
420
421                 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
422
423                 DBG("RDNSS start index %d domain %s "
424                                 "server %s lifetime threshold %d",
425                                 index, domain, server, interval);
426
427                 entry->timeout = g_timeout_add_seconds(interval,
428                                 resolver_refresh_cb, entry);
429                 return 0;
430         }
431
432         return append_resolver(index, domain, server, lifetime, 0);
433 }
434
435 /**
436  * connman_resolver_remove:
437  * @index: network interface index
438  * @domain: domain limitation
439  * @server: server address
440  *
441  * Remover resolver server address from current list
442  */
443 int connman_resolver_remove(int index, const char *domain, const char *server)
444 {
445         GSList *list, *matches = NULL;
446
447         DBG("index %d domain %s server %s", index, domain, server);
448
449         for (list = entry_list; list; list = list->next) {
450                 struct entry_data *entry = list->data;
451
452                 if (entry->index != index)
453                         continue;
454
455                 if (g_strcmp0(entry->domain, domain) != 0)
456                         continue;
457
458                 if (g_strcmp0(entry->server, server) != 0)
459                         continue;
460
461                 matches = g_slist_prepend(matches, entry);
462                 break;
463         }
464
465         if (matches == NULL)
466                 return -ENOENT;
467
468         remove_entries(matches);
469
470         return 0;
471 }
472
473 /**
474  * connman_resolver_remove_all:
475  * @index: network interface index
476  *
477  * Remove all resolver server address for the specified interface index
478  */
479 int connman_resolver_remove_all(int index)
480 {
481         GSList *list, *matches = NULL;
482
483         DBG("index %d", index);
484
485         if (index < 0)
486                 return -EINVAL;
487
488         for (list = entry_list; list; list = list->next) {
489                 struct entry_data *entry = list->data;
490
491                 if (entry->index != index)
492                         continue;
493
494                 matches = g_slist_prepend(matches, entry);
495         }
496
497         if (matches == NULL)
498                 return -ENOENT;
499
500         remove_entries(matches);
501
502         return 0;
503 }
504
505 /**
506  * connman_resolver_flush:
507  *
508  * Flush pending resolver requests
509  */
510 void connman_resolver_flush(void)
511 {
512         if (dnsproxy_enabled == TRUE)
513                 __connman_dnsproxy_flush();
514
515         return;
516 }
517
518 int __connman_resolver_redo_servers(int index)
519 {
520         GSList *list;
521
522         if (dnsproxy_enabled == FALSE)
523                 return 0;
524
525         DBG("index %d", index);
526
527         if (index < 0)
528                 return -EINVAL;
529
530         for (list = entry_list; list; list = list->next) {
531                 struct entry_data *entry = list->data;
532
533                 if (entry->timeout == 0 || entry->index != index)
534                         continue;
535
536                 /*
537                  * This function must only check IPv6 server addresses so
538                  * do not remove IPv4 name servers unnecessarily.
539                  */
540                 if (entry->family != AF_INET6)
541                         continue;
542
543                 /*
544                  * We remove the server, and then re-create so that it will
545                  * use proper source addresses when sending DNS queries.
546                  */
547                 __connman_dnsproxy_remove(entry->index, entry->domain,
548                                         entry->server);
549                 /*
550                  * Remove also the resolver timer for the old server entry.
551                  * A new timer will be set for the new server entry
552                  * when the next Router Advertisement message arrives
553                  * with RDNSS/DNSSL settings.
554                  */
555                 g_source_remove(entry->timeout);
556                 entry->timeout = 0;
557
558                 __connman_dnsproxy_append(entry->index, entry->domain,
559                                         entry->server);
560         }
561
562         return 0;
563 }
564
565 static void free_entry(gpointer data)
566 {
567         struct entry_data *entry = data;
568         g_free(entry->domain);
569         g_free(entry->server);
570         g_free(entry);
571 }
572
573 static void free_resolvfile(gpointer data)
574 {
575         struct resolvfile_entry *entry = data;
576         g_free(entry->domain);
577         g_free(entry->server);
578         g_free(entry);
579 }
580
581 int __connman_resolver_init(connman_bool_t dnsproxy)
582 {
583         int i;
584         char **ns;
585
586         DBG("dnsproxy %d", dnsproxy);
587
588         if (dnsproxy == FALSE)
589                 return 0;
590
591         if (__connman_dnsproxy_init() < 0) {
592                 /* Fall back to resolv.conf */
593                 return 0;
594         }
595
596         dnsproxy_enabled = TRUE;
597
598         ns = connman_setting_get_string_list("FallbackNameservers");
599         for (i = 0; ns != NULL && ns[i] != NULL; i += 1) {
600                 DBG("server %s", ns[i]);
601                 append_resolver(-1, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
602         }
603
604         return 0;
605 }
606
607 void __connman_resolver_cleanup(void)
608 {
609         DBG("");
610
611         if (dnsproxy_enabled == TRUE)
612                 __connman_dnsproxy_cleanup();
613         else {
614                 GList *list;
615                 GSList *slist;
616
617                 for (list = resolvfile_list; list; list = g_list_next(list))
618                         free_resolvfile(list->data);
619                 g_list_free(resolvfile_list);
620                 resolvfile_list = NULL;
621
622                 for (slist = entry_list; slist; slist = g_slist_next(slist))
623                         free_entry(slist->data);
624                 g_slist_free(entry_list);
625                 entry_list = NULL;
626         }
627 }