Imported Upstream version 1.29
[platform/upstream/connman.git] / src / resolver.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 bool 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)
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 && 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 append_fallback_nameservers(void)
211 {
212         GSList *list;
213
214         for (list = entry_list; list; list = list->next) {
215                 struct entry_data *entry = list->data;
216
217                 if (entry->index >= 0 && entry->server)
218                         return;
219         }
220
221         for (list = entry_list; list; list = list->next) {
222                 struct entry_data *entry = list->data;
223
224                 if (entry->index != -1 || !entry->server)
225                         continue;
226
227                 DBG("index %d server %s", entry->index, entry->server);
228
229                 if (dnsproxy_enabled) {
230                         __connman_dnsproxy_append(entry->index, entry->domain,
231                                         entry->server);
232                 } else {
233                         __connman_resolvfile_append(entry->index,
234                                         entry->domain, entry->server);
235                 }
236         }
237 }
238
239 static void remove_fallback_nameservers(void)
240 {
241         GSList *list;
242
243         for (list = entry_list; list; list = list->next) {
244                 struct entry_data *entry = list->data;
245
246                 if (entry->index >= 0 || !entry->server)
247                         continue;
248
249                 DBG("index %d server %s", entry->index, entry->server);
250
251                 if (dnsproxy_enabled) {
252                         __connman_dnsproxy_remove(entry->index, entry->domain,
253                                         entry->server);
254                 } else {
255                         __connman_resolvfile_remove(entry->index,
256                                         entry->domain, entry->server);
257                 }
258         }
259 }
260
261 static void remove_entries(GSList *entries)
262 {
263         GSList *list;
264
265         for (list = entries; list; list = list->next) {
266                 struct entry_data *entry = list->data;
267
268                 entry_list = g_slist_remove(entry_list, entry);
269
270                 if (dnsproxy_enabled) {
271                         __connman_dnsproxy_remove(entry->index, entry->domain,
272                                                         entry->server);
273                 } else {
274                         __connman_resolvfile_remove(entry->index, entry->domain,
275                                                         entry->server);
276                 }
277
278                 if (entry->timeout)
279                         g_source_remove(entry->timeout);
280                 g_free(entry->server);
281                 g_free(entry->domain);
282                 g_free(entry);
283         }
284
285         g_slist_free(entries);
286 }
287
288 static gboolean resolver_expire_cb(gpointer user_data)
289 {
290         struct entry_data *entry = user_data;
291         GSList *list;
292
293         DBG("index %d domain %s server %s",
294                         entry->index, entry->domain, entry->server);
295
296         list = g_slist_prepend(NULL, entry);
297
298         if (entry->index >= 0) {
299                 struct connman_service *service;
300                 service = __connman_service_lookup_from_index(entry->index);
301                 if (service)
302                         __connman_service_nameserver_remove(service,
303                                                         entry->server, true);
304         }
305
306         remove_entries(list);
307
308         return FALSE;
309 }
310
311 static gboolean resolver_refresh_cb(gpointer user_data)
312 {
313         struct entry_data *entry = user_data;
314         unsigned int interval;
315         struct connman_service *service = NULL;
316
317         /* Round up what we have left from lifetime */
318         interval = entry->lifetime *
319                 (1 - RESOLVER_LIFETIME_REFRESH_THRESHOLD) + 1.0;
320
321         DBG("RDNSS start index %d domain %s "
322                         "server %s remaining lifetime %d",
323                         entry->index, entry->domain,
324                         entry->server, interval);
325
326         entry->timeout = g_timeout_add_seconds(interval,
327                         resolver_expire_cb, entry);
328
329         if (entry->index >= 0) {
330                 service = __connman_service_lookup_from_index(entry->index);
331                 if (service) {
332                         /*
333                          * Send Router Solicitation to refresh RDNSS entries
334                          * before their lifetime expires
335                          */
336                         __connman_network_refresh_rs_ipv6(
337                                         __connman_service_get_network(service),
338                                         entry->index);
339                 }
340         }
341         return FALSE;
342 }
343
344 static int append_resolver(int index, const char *domain,
345                                 const char *server, unsigned int lifetime,
346                                                         unsigned int flags)
347 {
348         struct entry_data *entry;
349         unsigned int interval;
350
351         DBG("index %d domain %s server %s lifetime %d flags %d",
352                                 index, domain, server, lifetime, flags);
353
354         if (!server && !domain)
355                 return -EINVAL;
356
357         entry = g_try_new0(struct entry_data, 1);
358         if (!entry)
359                 return -ENOMEM;
360
361         entry->index = index;
362         entry->domain = g_strdup(domain);
363         entry->server = g_strdup(server);
364         entry->flags = flags;
365         entry->lifetime = lifetime;
366
367         if (server)
368                 entry->family = connman_inet_check_ipaddress(server);
369
370         if (lifetime) {
371                 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
372
373                 DBG("RDNSS start index %d domain %s "
374                                 "server %s lifetime threshold %d",
375                                 index, domain, server, interval);
376
377                 entry->timeout = g_timeout_add_seconds(interval,
378                                 resolver_refresh_cb, entry);
379
380                 /*
381                  * We update the service only for those nameservers
382                  * that are automagically added via netlink (lifetime > 0)
383                  */
384                 if (server && entry->index >= 0) {
385                         struct connman_service *service;
386                         service = __connman_service_lookup_from_index(entry->index);
387                         if (service)
388                                 __connman_service_nameserver_append(service,
389                                                                 server, true);
390                 }
391         }
392
393         if (entry->index >= 0 && entry->server)
394                 remove_fallback_nameservers();
395
396         entry_list = g_slist_append(entry_list, entry);
397
398         if (dnsproxy_enabled)
399                 __connman_dnsproxy_append(entry->index, domain, server);
400         else
401                 __connman_resolvfile_append(entry->index, domain, server);
402
403         return 0;
404 }
405
406 /**
407  * connman_resolver_append:
408  * @index: network interface index
409  * @domain: domain limitation
410  * @server: server address
411  *
412  * Append resolver server address to current list
413  */
414 int connman_resolver_append(int index, const char *domain,
415                                                 const char *server)
416 {
417         GSList *list;
418
419         DBG("index %d domain %s server %s", index, domain, server);
420
421         if (!server && !domain)
422                 return -EINVAL;
423
424         for (list = entry_list; list; list = list->next) {
425                 struct entry_data *entry = list->data;
426
427                 if (entry->timeout > 0)
428                         continue;
429
430                 if (entry->index == index &&
431                                 g_strcmp0(entry->domain, domain) == 0 &&
432                                 g_strcmp0(entry->server, server) == 0) {
433                         if (dnsproxy_enabled)
434                                 __connman_dnsproxy_append(entry->index, domain,
435                                                 server);
436
437                         return -EEXIST;
438                 }
439         }
440
441         return append_resolver(index, domain, server, 0, 0);
442 }
443
444 /**
445  * connman_resolver_append_lifetime:
446  * @index: network interface index
447  * @domain: domain limitation
448  * @server: server address
449  * @timeout: server lifetime in seconds
450  *
451  * Append resolver server address to current list
452  */
453 int connman_resolver_append_lifetime(int index, const char *domain,
454                                 const char *server, unsigned int lifetime)
455 {
456         GSList *list;
457         unsigned int interval;
458
459         DBG("index %d domain %s server %s lifetime %d",
460                                 index, domain, server, lifetime);
461
462         if (!server && !domain)
463                 return -EINVAL;
464
465         for (list = entry_list; list; list = list->next) {
466                 struct entry_data *entry = list->data;
467
468                 if (entry->timeout == 0 ||
469                                 entry->index != index ||
470                                 g_strcmp0(entry->domain, domain) != 0 ||
471                                 g_strcmp0(entry->server, server) != 0)
472                         continue;
473
474                 g_source_remove(entry->timeout);
475
476                 if (lifetime == 0) {
477                         resolver_expire_cb(entry);
478                         return 0;
479                 }
480
481                 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
482
483                 DBG("RDNSS start index %d domain %s "
484                                 "server %s lifetime threshold %d",
485                                 index, domain, server, interval);
486
487                 entry->timeout = g_timeout_add_seconds(interval,
488                                 resolver_refresh_cb, entry);
489                 return 0;
490         }
491
492         return append_resolver(index, domain, server, lifetime, 0);
493 }
494
495 /**
496  * connman_resolver_remove:
497  * @index: network interface index
498  * @domain: domain limitation
499  * @server: server address
500  *
501  * Remover resolver server address from current list
502  */
503 int connman_resolver_remove(int index, const char *domain, const char *server)
504 {
505         GSList *list, *matches = NULL;
506
507         DBG("index %d domain %s server %s", index, domain, server);
508
509         for (list = entry_list; list; list = list->next) {
510                 struct entry_data *entry = list->data;
511
512                 if (entry->index != index)
513                         continue;
514
515                 if (g_strcmp0(entry->domain, domain) != 0)
516                         continue;
517
518                 if (g_strcmp0(entry->server, server) != 0)
519                         continue;
520
521                 matches = g_slist_prepend(matches, entry);
522                 break;
523         }
524
525         if (!matches)
526                 return -ENOENT;
527
528         remove_entries(matches);
529
530         return 0;
531 }
532
533 /**
534  * connman_resolver_remove_all:
535  * @index: network interface index
536  *
537  * Remove all resolver server address for the specified interface index
538  */
539 int connman_resolver_remove_all(int index)
540 {
541         GSList *list, *matches = NULL;
542
543         DBG("index %d", index);
544
545         if (index < 0)
546                 return -EINVAL;
547
548         for (list = entry_list; list; list = list->next) {
549                 struct entry_data *entry = list->data;
550
551                 if (entry->index != index)
552                         continue;
553
554                 matches = g_slist_prepend(matches, entry);
555         }
556
557         if (!matches)
558                 return -ENOENT;
559
560         remove_entries(matches);
561
562         return 0;
563 }
564
565 /**
566  * connman_resolver_flush:
567  *
568  * Flush pending resolver requests
569  */
570 void connman_resolver_flush(void)
571 {
572         append_fallback_nameservers();
573
574         if (dnsproxy_enabled)
575                 __connman_dnsproxy_flush();
576
577         return;
578 }
579
580 int __connman_resolver_redo_servers(int index)
581 {
582         GSList *list;
583
584         if (!dnsproxy_enabled)
585                 return 0;
586
587         DBG("index %d", index);
588
589         if (index < 0)
590                 return -EINVAL;
591
592         for (list = entry_list; list; list = list->next) {
593                 struct entry_data *entry = list->data;
594
595                 if (entry->timeout == 0 || entry->index != index)
596                         continue;
597
598                 /*
599                  * This function must only check IPv6 server addresses so
600                  * do not remove IPv4 name servers unnecessarily.
601                  */
602                 if (entry->family != AF_INET6)
603                         continue;
604
605                 /*
606                  * We remove the server, and then re-create so that it will
607                  * use proper source addresses when sending DNS queries.
608                  */
609                 __connman_dnsproxy_remove(entry->index, entry->domain,
610                                         entry->server);
611
612                 __connman_dnsproxy_append(entry->index, entry->domain,
613                                         entry->server);
614         }
615
616         return 0;
617 }
618
619 static void free_entry(gpointer data)
620 {
621         struct entry_data *entry = data;
622         g_free(entry->domain);
623         g_free(entry->server);
624         g_free(entry);
625 }
626
627 static void free_resolvfile(gpointer data)
628 {
629         struct resolvfile_entry *entry = data;
630         g_free(entry->domain);
631         g_free(entry->server);
632         g_free(entry);
633 }
634
635 int __connman_resolver_init(gboolean dnsproxy)
636 {
637         int i;
638         char **ns;
639
640         DBG("dnsproxy %d", dnsproxy);
641
642         if (!dnsproxy)
643                 return 0;
644
645         if (__connman_dnsproxy_init() < 0) {
646                 /* Fall back to resolv.conf */
647                 return 0;
648         }
649
650         dnsproxy_enabled = true;
651
652         ns = connman_setting_get_string_list("FallbackNameservers");
653         for (i = 0; ns && ns[i]; i += 1) {
654                 DBG("server %s", ns[i]);
655                 append_resolver(-1, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
656         }
657
658         return 0;
659 }
660
661 void __connman_resolver_cleanup(void)
662 {
663         DBG("");
664
665         if (dnsproxy_enabled)
666                 __connman_dnsproxy_cleanup();
667         else {
668                 GList *list;
669                 GSList *slist;
670
671                 for (list = resolvfile_list; list; list = g_list_next(list))
672                         free_resolvfile(list->data);
673                 g_list_free(resolvfile_list);
674                 resolvfile_list = NULL;
675
676                 for (slist = entry_list; slist; slist = g_slist_next(slist))
677                         free_entry(slist->data);
678                 g_slist_free(entry_list);
679                 entry_list = NULL;
680         }
681 }