Imported Upstream version 1.35
[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 RESOLV_CONF_STATEDIR STATEDIR"/resolv.conf"
39 #define RESOLV_CONF_ETC "/etc/resolv.conf"
40
41 #define RESOLVER_FLAG_PUBLIC (1 << 0)
42
43 /*
44  * Threshold for RDNSS lifetime. Will be used to trigger RS
45  * before RDNSS entries actually expire
46  */
47 #define RESOLVER_LIFETIME_REFRESH_THRESHOLD 0.8
48
49 struct entry_data {
50         int index;
51         char *domain;
52         char *server;
53         int family;
54         unsigned int flags;
55         unsigned int lifetime;
56         guint timeout;
57 };
58
59 static GSList *entry_list = NULL;
60 static bool dnsproxy_enabled = false;
61
62 struct resolvfile_entry {
63         int index;
64         char *domain;
65         char *server;
66 };
67
68 static GList *resolvfile_list = NULL;
69
70 static void resolvfile_remove_entries(GList *entries)
71 {
72         GList *list;
73
74         for (list = entries; list; list = list->next) {
75                 struct resolvfile_entry *entry = list->data;
76
77                 resolvfile_list = g_list_remove(resolvfile_list, entry);
78
79                 g_free(entry->server);
80                 g_free(entry->domain);
81                 g_free(entry);
82         }
83
84         g_list_free(entries);
85 }
86
87 static int resolvfile_export(void)
88 {
89         GList *list;
90         GString *content;
91         int fd, err;
92         unsigned int count;
93         mode_t old_umask;
94
95         content = g_string_new("# Generated by Connection Manager\n");
96
97         /*
98          * Domains and nameservers are added in reverse so that the most
99          * recently appended entry is the primary one. No more than
100          * MAXDNSRCH/MAXNS entries are used.
101          */
102
103         for (count = 0, list = g_list_first(resolvfile_list);
104                                                 list && (count < MAXDNSRCH);
105                                                 list = g_list_next(list)) {
106                 struct resolvfile_entry *entry = list->data;
107
108                 if (!entry->domain)
109                         continue;
110
111                 if (count == 0)
112                         g_string_append_printf(content, "search ");
113
114                 g_string_append_printf(content, "%s ", entry->domain);
115                 count++;
116         }
117
118         if (count)
119                 g_string_append_printf(content, "\n");
120
121         for (count = 0, list = g_list_first(resolvfile_list);
122                                                 list && (count < MAXNS);
123                                                 list = g_list_next(list)) {
124                 struct resolvfile_entry *entry = list->data;
125
126                 if (!entry->server)
127                         continue;
128
129                 g_string_append_printf(content, "nameserver %s\n",
130                                                                 entry->server);
131                 count++;
132         }
133
134         old_umask = umask(022);
135
136         fd = open(RESOLV_CONF_STATEDIR, O_RDWR | O_CREAT | O_CLOEXEC,
137                                         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
138         if (fd < 0) {
139                 connman_warn_once("Cannot create "RESOLV_CONF_STATEDIR" "
140                         "falling back to "RESOLV_CONF_ETC);
141
142                 fd = open(RESOLV_CONF_ETC, O_RDWR | O_CREAT | O_CLOEXEC,
143                                         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
144
145                 if (fd < 0) {
146                         err = -errno;
147                         goto done;
148                 }
149         }
150
151         if (ftruncate(fd, 0) < 0) {
152                 err = -errno;
153                 goto failed;
154         }
155
156         err = 0;
157
158         if (write(fd, content->str, content->len) < 0)
159                 err = -errno;
160
161 failed:
162         close(fd);
163
164 done:
165         g_string_free(content, TRUE);
166         umask(old_umask);
167
168         return err;
169 }
170
171 int __connman_resolvfile_append(int index, const char *domain,
172                                                         const char *server)
173 {
174         struct resolvfile_entry *entry;
175
176         DBG("index %d server %s", index, server);
177
178         if (index < 0)
179                 return -ENOENT;
180
181         entry = g_try_new0(struct resolvfile_entry, 1);
182         if (!entry)
183                 return -ENOMEM;
184
185         entry->index = index;
186         entry->domain = g_strdup(domain);
187         entry->server = g_strdup(server);
188
189         resolvfile_list = g_list_append(resolvfile_list, entry);
190
191         return resolvfile_export();
192 }
193
194 int __connman_resolvfile_remove(int index, const char *domain,
195                                                         const char *server)
196 {
197         GList *list, *matches = NULL;
198
199         DBG("index %d server %s", index, server);
200
201         for (list = resolvfile_list; list; list = g_list_next(list)) {
202                 struct resolvfile_entry *entry = list->data;
203
204                 if (index >= 0 && entry->index != index)
205                         continue;
206
207                 if (domain && g_strcmp0(entry->domain, domain) != 0)
208                         continue;
209
210                 if (g_strcmp0(entry->server, server) != 0)
211                         continue;
212
213                 matches = g_list_append(matches, entry);
214         }
215
216         resolvfile_remove_entries(matches);
217
218         return resolvfile_export();
219 }
220
221 void __connman_resolver_append_fallback_nameservers(void)
222 {
223         GSList *list;
224
225         for (list = entry_list; list; list = list->next) {
226                 struct entry_data *entry = list->data;
227
228                 if (entry->index >= 0 && entry->server)
229                         return;
230         }
231
232         for (list = entry_list; list; list = list->next) {
233                 struct entry_data *entry = list->data;
234
235                 if (entry->index != -1 || !entry->server)
236                         continue;
237
238                 DBG("index %d server %s", entry->index, entry->server);
239
240                 if (dnsproxy_enabled) {
241                         __connman_dnsproxy_append(entry->index, entry->domain,
242                                         entry->server);
243                 } else {
244                         __connman_resolvfile_append(entry->index,
245                                         entry->domain, entry->server);
246                 }
247         }
248 }
249
250 static void remove_fallback_nameservers(void)
251 {
252         GSList *list;
253
254         for (list = entry_list; list; list = list->next) {
255                 struct entry_data *entry = list->data;
256
257                 if (entry->index >= 0 || !entry->server)
258                         continue;
259
260                 DBG("index %d server %s", entry->index, entry->server);
261
262                 if (dnsproxy_enabled) {
263                         __connman_dnsproxy_remove(entry->index, entry->domain,
264                                         entry->server);
265                 } else {
266                         __connman_resolvfile_remove(entry->index,
267                                         entry->domain, entry->server);
268                 }
269         }
270 }
271
272 static void remove_entries(GSList *entries)
273 {
274         GSList *list;
275
276         for (list = entries; list; list = list->next) {
277                 struct entry_data *entry = list->data;
278
279                 entry_list = g_slist_remove(entry_list, entry);
280
281                 if (dnsproxy_enabled) {
282                         __connman_dnsproxy_remove(entry->index, entry->domain,
283                                                         entry->server);
284                 } else {
285                         __connman_resolvfile_remove(entry->index, entry->domain,
286                                                         entry->server);
287                 }
288
289                 if (entry->timeout)
290                         g_source_remove(entry->timeout);
291                 g_free(entry->server);
292                 g_free(entry->domain);
293                 g_free(entry);
294         }
295
296         g_slist_free(entries);
297
298         __connman_resolver_append_fallback_nameservers();
299 }
300
301 static gboolean resolver_expire_cb(gpointer user_data)
302 {
303         struct entry_data *entry = user_data;
304         GSList *list;
305
306         DBG("index %d domain %s server %s",
307                         entry->index, entry->domain, entry->server);
308
309         list = g_slist_prepend(NULL, entry);
310
311         if (entry->index >= 0) {
312                 struct connman_service *service;
313                 service = __connman_service_lookup_from_index(entry->index);
314                 if (service)
315                         __connman_service_nameserver_remove(service,
316                                                         entry->server, true);
317         }
318
319         remove_entries(list);
320
321         return FALSE;
322 }
323
324 static gboolean resolver_refresh_cb(gpointer user_data)
325 {
326         struct entry_data *entry = user_data;
327         unsigned int interval;
328         struct connman_service *service = NULL;
329
330         /* Round up what we have left from lifetime */
331         interval = entry->lifetime *
332                 (1 - RESOLVER_LIFETIME_REFRESH_THRESHOLD) + 1.0;
333
334         DBG("RDNSS start index %d domain %s "
335                         "server %s remaining lifetime %d",
336                         entry->index, entry->domain,
337                         entry->server, interval);
338
339         entry->timeout = g_timeout_add_seconds(interval,
340                         resolver_expire_cb, entry);
341
342         if (entry->index >= 0) {
343                 service = __connman_service_lookup_from_index(entry->index);
344                 if (service) {
345                         /*
346                          * Send Router Solicitation to refresh RDNSS entries
347                          * before their lifetime expires
348                          */
349                         __connman_network_refresh_rs_ipv6(
350                                         __connman_service_get_network(service),
351                                         entry->index);
352                 }
353         }
354         return FALSE;
355 }
356
357 static int append_resolver(int index, const char *domain,
358                                 const char *server, unsigned int lifetime,
359                                                         unsigned int flags)
360 {
361         struct entry_data *entry;
362         unsigned int interval;
363
364         DBG("index %d domain %s server %s lifetime %d flags %d",
365                                 index, domain, server, lifetime, flags);
366
367         if (!server && !domain)
368                 return -EINVAL;
369
370         entry = g_try_new0(struct entry_data, 1);
371         if (!entry)
372                 return -ENOMEM;
373
374         entry->index = index;
375         entry->domain = g_strdup(domain);
376         entry->server = g_strdup(server);
377         entry->flags = flags;
378         entry->lifetime = lifetime;
379
380         if (server)
381                 entry->family = connman_inet_check_ipaddress(server);
382
383         if (lifetime) {
384                 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
385
386                 DBG("RDNSS start index %d domain %s "
387                                 "server %s lifetime threshold %d",
388                                 index, domain, server, interval);
389
390                 entry->timeout = g_timeout_add_seconds(interval,
391                                 resolver_refresh_cb, entry);
392         }
393
394         if (entry->index >= 0 && entry->server)
395                 remove_fallback_nameservers();
396
397         entry_list = g_slist_append(entry_list, entry);
398
399         if (dnsproxy_enabled)
400                 __connman_dnsproxy_append(entry->index, domain, server);
401         else
402                 __connman_resolvfile_append(entry->index, domain, server);
403
404         /*
405          * We update the service only for those nameservers
406          * that are automagically added via netlink (lifetime > 0)
407          */
408         if (server && entry->index >= 0 && lifetime) {
409                 struct connman_service *service;
410                 service = __connman_service_lookup_from_index(entry->index);
411                 if (service)
412                         __connman_service_nameserver_append(service,
413                                                         server, true);
414         }
415
416         return 0;
417 }
418
419 /**
420  * connman_resolver_append:
421  * @index: network interface index
422  * @domain: domain limitation
423  * @server: server address
424  *
425  * Append resolver server address to current list
426  */
427 int connman_resolver_append(int index, const char *domain,
428                                                 const char *server)
429 {
430         GSList *list;
431
432         DBG("index %d domain %s server %s", index, domain, server);
433
434         if (!server && !domain)
435                 return -EINVAL;
436
437         for (list = entry_list; list; list = list->next) {
438                 struct entry_data *entry = list->data;
439
440                 if (entry->timeout > 0)
441                         continue;
442
443                 if (entry->index == index &&
444                                 g_strcmp0(entry->domain, domain) == 0 &&
445                                 g_strcmp0(entry->server, server) == 0) {
446                         if (dnsproxy_enabled)
447                                 __connman_dnsproxy_append(entry->index, domain,
448                                                 server);
449
450                         return -EEXIST;
451                 }
452         }
453
454         return append_resolver(index, domain, server, 0, 0);
455 }
456
457 /**
458  * connman_resolver_append_lifetime:
459  * @index: network interface index
460  * @domain: domain limitation
461  * @server: server address
462  * @timeout: server lifetime in seconds
463  *
464  * Append resolver server address to current list
465  */
466 int connman_resolver_append_lifetime(int index, const char *domain,
467                                 const char *server, unsigned int lifetime)
468 {
469         GSList *list;
470         unsigned int interval;
471
472         DBG("index %d domain %s server %s lifetime %d",
473                                 index, domain, server, lifetime);
474
475         if (!server && !domain)
476                 return -EINVAL;
477
478         for (list = entry_list; list; list = list->next) {
479                 struct entry_data *entry = list->data;
480
481                 if (entry->timeout == 0 ||
482                                 entry->index != index ||
483                                 g_strcmp0(entry->domain, domain) != 0 ||
484                                 g_strcmp0(entry->server, server) != 0)
485                         continue;
486
487                 g_source_remove(entry->timeout);
488
489                 if (lifetime == 0) {
490                         resolver_expire_cb(entry);
491                         return 0;
492                 }
493
494                 interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;
495
496                 DBG("RDNSS start index %d domain %s "
497                                 "server %s lifetime threshold %d",
498                                 index, domain, server, interval);
499
500                 entry->timeout = g_timeout_add_seconds(interval,
501                                 resolver_refresh_cb, entry);
502                 return 0;
503         }
504
505         return append_resolver(index, domain, server, lifetime, 0);
506 }
507
508 /**
509  * connman_resolver_remove:
510  * @index: network interface index
511  * @domain: domain limitation
512  * @server: server address
513  *
514  * Remover resolver server address from current list
515  */
516 int connman_resolver_remove(int index, const char *domain, const char *server)
517 {
518         GSList *list, *matches = NULL;
519
520         DBG("index %d domain %s server %s", index, domain, server);
521
522         for (list = entry_list; list; list = list->next) {
523                 struct entry_data *entry = list->data;
524
525                 if (entry->index != index)
526                         continue;
527
528                 if (g_strcmp0(entry->domain, domain) != 0)
529                         continue;
530
531                 if (g_strcmp0(entry->server, server) != 0)
532                         continue;
533
534                 matches = g_slist_prepend(matches, entry);
535                 break;
536         }
537
538         if (!matches)
539                 return -ENOENT;
540
541         remove_entries(matches);
542
543         return 0;
544 }
545
546 /**
547  * connman_resolver_remove_all:
548  * @index: network interface index
549  *
550  * Remove all resolver server address for the specified interface index
551  */
552 int connman_resolver_remove_all(int index)
553 {
554         GSList *list, *matches = NULL;
555
556         DBG("index %d", index);
557
558         if (index < 0)
559                 return -EINVAL;
560
561         for (list = entry_list; list; list = list->next) {
562                 struct entry_data *entry = list->data;
563
564                 if (entry->index != index)
565                         continue;
566
567                 matches = g_slist_prepend(matches, entry);
568         }
569
570         if (!matches)
571                 return -ENOENT;
572
573         remove_entries(matches);
574
575         return 0;
576 }
577
578 int __connman_resolver_redo_servers(int index)
579 {
580         GSList *list;
581
582         if (!dnsproxy_enabled)
583                 return 0;
584
585         DBG("index %d", index);
586
587         if (index < 0)
588                 return -EINVAL;
589
590         for (list = entry_list; list; list = list->next) {
591                 struct entry_data *entry = list->data;
592
593                 if (entry->timeout == 0 || entry->index != index)
594                         continue;
595
596                 /*
597                  * This function must only check IPv6 server addresses so
598                  * do not remove IPv4 name servers unnecessarily.
599                  */
600                 if (entry->family != AF_INET6)
601                         continue;
602
603                 /*
604                  * We remove the server, and then re-create so that it will
605                  * use proper source addresses when sending DNS queries.
606                  */
607                 __connman_dnsproxy_remove(entry->index, entry->domain,
608                                         entry->server);
609
610                 __connman_dnsproxy_append(entry->index, entry->domain,
611                                         entry->server);
612         }
613
614         /*
615          * We want to re-add all search domains back to search
616          * domain lists as they just got removed for RDNSS IPv6-servers
617          * (above).
618          * Removal of search domains is not necessary
619          * as there can be only one instance of each search domain
620          * in the each dns-servers search domain list.
621         */
622
623         for (list = entry_list; list; list = list->next) {
624                 struct entry_data *entry = list->data;
625
626                 if (entry->index != index)
627                         continue;
628
629                 if (entry->server)
630                         continue;
631
632                 __connman_dnsproxy_append(entry->index, entry->domain,
633                                         NULL);
634         }
635
636         return 0;
637 }
638
639 static void free_entry(gpointer data)
640 {
641         struct entry_data *entry = data;
642         g_free(entry->domain);
643         g_free(entry->server);
644         g_free(entry);
645 }
646
647 static void free_resolvfile(gpointer data)
648 {
649         struct resolvfile_entry *entry = data;
650         g_free(entry->domain);
651         g_free(entry->server);
652         g_free(entry);
653 }
654
655 int __connman_resolver_init(gboolean dnsproxy)
656 {
657         int i;
658         char **ns;
659
660         DBG("dnsproxy %d", dnsproxy);
661
662         /* get autoip nameservers */
663         ns = __connman_inet_get_pnp_nameservers(NULL);
664         for (i = 0; ns && ns[i]; i += 1) {
665                 DBG("pnp server %s", ns[i]);
666                 append_resolver(i, NULL, ns[i], 86400, 0);
667         }
668         g_strfreev(ns);
669
670         if (!dnsproxy)
671                 return 0;
672
673         if (__connman_dnsproxy_init() < 0) {
674                 /* Fall back to resolv.conf */
675                 return 0;
676         }
677
678         dnsproxy_enabled = true;
679
680         ns = connman_setting_get_string_list("FallbackNameservers");
681         for (i = 0; ns && ns[i]; i += 1) {
682                 DBG("server %s", ns[i]);
683                 append_resolver(-1, NULL, ns[i], 0, RESOLVER_FLAG_PUBLIC);
684         }
685
686         return 0;
687 }
688
689 void __connman_resolver_cleanup(void)
690 {
691         DBG("");
692
693         if (dnsproxy_enabled)
694                 __connman_dnsproxy_cleanup();
695         else {
696                 GList *list;
697                 GSList *slist;
698
699                 for (list = resolvfile_list; list; list = g_list_next(list))
700                         free_resolvfile(list->data);
701                 g_list_free(resolvfile_list);
702                 resolvfile_list = NULL;
703
704                 for (slist = entry_list; slist; slist = g_slist_next(slist))
705                         free_entry(slist->data);
706                 g_slist_free(entry_list);
707                 entry_list = NULL;
708         }
709 }