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