Imported Upstream version 1.35
[platform/upstream/connman.git] / src / timeserver.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 #include <errno.h>
27
28 #include <glib.h>
29 #include <stdlib.h>
30 #include <gweb/gresolv.h>
31 #include <netdb.h>
32
33 #include "connman.h"
34
35 #define TS_RECHECK_INTERVAL     7200
36
37 static GSList *ts_list = NULL;
38 static char *ts_current = NULL;
39 static int ts_recheck_id = 0;
40
41 static GResolv *resolv = NULL;
42 static int resolv_id = 0;
43
44 static void resolv_debug(const char *str, void *data)
45 {
46         connman_info("%s: %s\n", (const char *) data, str);
47 }
48 static void save_timeservers(char **servers)
49 {
50         GKeyFile *keyfile;
51         int cnt;
52
53         keyfile = __connman_storage_load_global();
54         if (!keyfile)
55                 keyfile = g_key_file_new();
56
57         for (cnt = 0; servers && servers[cnt]; cnt++);
58
59         g_key_file_set_string_list(keyfile, "global", "Timeservers",
60                            (const gchar **)servers, cnt);
61
62         __connman_storage_save_global(keyfile);
63
64         g_key_file_free(keyfile);
65
66         return;
67 }
68
69 static char **load_timeservers(void)
70 {
71         GKeyFile *keyfile;
72         char **servers = NULL;
73
74         keyfile = __connman_storage_load_global();
75         if (!keyfile)
76                 return NULL;
77
78         servers = g_key_file_get_string_list(keyfile, "global",
79                                                 "Timeservers", NULL, NULL);
80
81         g_key_file_free(keyfile);
82
83         return servers;
84 }
85
86 static void resolv_result(GResolvResultStatus status, char **results,
87                                 gpointer user_data)
88 {
89         int i;
90
91         DBG("status %d", status);
92
93         if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
94                 if (results) {
95                         for (i = 0; results[i]; i++) {
96                                 DBG("result[%d]: %s", i, results[i]);
97                                 if (i == 0)
98                                         continue;
99
100                                 ts_list = __connman_timeserver_add_list(
101                                                         ts_list, results[i]);
102                         }
103
104                         DBG("Using timeserver %s", results[0]);
105
106                         __connman_ntp_start(results[0]);
107
108                         return;
109                 }
110         }
111
112         /* If resolving fails, move to the next server */
113         __connman_timeserver_sync_next();
114 }
115
116 /*
117  * Once the timeserver list (ts_list) is created, we start querying the
118  * servers one by one. If resolving fails on one of them, we move to the
119  * next one. The user can enter either an IP address or a URL for the
120  * timeserver. We only resolve the URLs. Once we have an IP for the NTP
121  * server, we start querying it for time corrections.
122  */
123 void __connman_timeserver_sync_next()
124 {
125         if (ts_current) {
126                 g_free(ts_current);
127                 ts_current = NULL;
128         }
129
130         __connman_ntp_stop();
131
132         /* Get the 1st server in the list */
133         if (!ts_list)
134                 return;
135
136         ts_current = ts_list->data;
137
138         ts_list = g_slist_delete_link(ts_list, ts_list);
139
140         /* if it's an IP, directly query it. */
141         if (connman_inet_check_ipaddress(ts_current) > 0) {
142                 DBG("Using timeserver %s", ts_current);
143
144                 __connman_ntp_start(ts_current);
145
146                 return;
147         }
148
149         DBG("Resolving timeserver %s", ts_current);
150
151         resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
152                                                 resolv_result, NULL);
153
154         return;
155 }
156
157 GSList *__connman_timeserver_add_list(GSList *server_list,
158                 const char *timeserver)
159 {
160         GSList *list = server_list;
161
162         if (!timeserver)
163                 return server_list;
164
165         while (list) {
166                 char *existing_server = list->data;
167                 if (strcmp(timeserver, existing_server) == 0)
168                         return server_list;
169                 list = g_slist_next(list);
170         }
171         return g_slist_prepend(server_list, g_strdup(timeserver));
172 }
173
174 /*
175  * __connman_timeserver_get_all function creates the timeserver
176  * list which will be used to determine NTP server for time corrections.
177  * The service settings take priority over the global timeservers.
178  */
179 GSList *__connman_timeserver_get_all(struct connman_service *service)
180 {
181         GSList *list = NULL;
182         struct connman_network *network;
183         char **timeservers;
184         char **service_ts;
185         char **service_ts_config;
186         const char *service_gw;
187         char **fallback_ts;
188         int index, i;
189
190         if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
191                 return NULL;
192
193         service_ts_config = connman_service_get_timeservers_config(service);
194
195         /* First add Service Timeservers.Configuration to the list */
196         for (i = 0; service_ts_config && service_ts_config[i];
197                         i++)
198                 list = __connman_timeserver_add_list(list,
199                                 service_ts_config[i]);
200
201         service_ts = connman_service_get_timeservers(service);
202
203         /* Then add Service Timeservers via DHCP to the list */
204         for (i = 0; service_ts && service_ts[i]; i++)
205                 list = __connman_timeserver_add_list(list, service_ts[i]);
206
207         network = __connman_service_get_network(service);
208         if (network) {
209                 index = connman_network_get_index(network);
210                 service_gw = __connman_ipconfig_get_gateway_from_index(index,
211                         CONNMAN_IPCONFIG_TYPE_ALL);
212
213                 /* Then add Service Gateway to the list */
214                 if (service_gw)
215                         list = __connman_timeserver_add_list(list, service_gw);
216         }
217
218         /* Then add Global Timeservers to the list */
219         timeservers = load_timeservers();
220
221         for (i = 0; timeservers && timeservers[i]; i++)
222                 list = __connman_timeserver_add_list(list, timeservers[i]);
223
224         g_strfreev(timeservers);
225
226         fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
227
228         /* Lastly add the fallback servers */
229         for (i = 0; fallback_ts && fallback_ts[i]; i++)
230                 list = __connman_timeserver_add_list(list, fallback_ts[i]);
231
232         return g_slist_reverse(list);
233 }
234
235 static gboolean ts_recheck(gpointer user_data)
236 {
237         GSList *ts;
238
239         ts = __connman_timeserver_get_all(__connman_service_get_default());
240
241         if (!ts) {
242                 DBG("timeservers disabled");
243
244                 return TRUE;
245         }
246
247         if (g_strcmp0(ts_current, ts->data) != 0) {
248                 DBG("current %s preferred %s", ts_current, (char *)ts->data);
249
250                 g_slist_free_full(ts, g_free);
251
252                 __connman_timeserver_sync(NULL);
253
254                 return FALSE;
255         }
256
257         DBG("");
258
259         g_slist_free_full(ts, g_free);
260
261         return TRUE;
262 }
263
264 static void ts_recheck_disable(void)
265 {
266         if (ts_recheck_id == 0)
267                 return;
268
269         g_source_remove(ts_recheck_id);
270         ts_recheck_id = 0;
271
272         if (ts_current) {
273                 g_free(ts_current);
274                 ts_current = NULL;
275         }
276 }
277
278 static void ts_recheck_enable(void)
279 {
280         if (ts_recheck_id > 0)
281                 return;
282
283         ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
284                         NULL);
285 }
286
287 /*
288  * This function must be called everytime the default service changes, the
289  * service timeserver(s) or gateway changes or the global timeserver(s) changes.
290  */
291 int __connman_timeserver_sync(struct connman_service *default_service)
292 {
293         struct connman_service *service;
294         char **nameservers;
295         int i;
296
297         if (default_service)
298                 service = default_service;
299         else
300                 service = __connman_service_get_default();
301
302         if (!service)
303                 return -EINVAL;
304
305         if (!resolv)
306                 return 0;
307         /*
308          * Before we start creating the new timeserver list we must stop
309          * any ongoing ntp query and server resolution.
310          */
311
312         __connman_ntp_stop();
313
314         ts_recheck_disable();
315
316         if (resolv_id > 0)
317                 g_resolv_cancel_lookup(resolv, resolv_id);
318
319         g_resolv_flush_nameservers(resolv);
320
321         nameservers = connman_service_get_nameservers(service);
322         if (!nameservers)
323                 return -EINVAL;
324
325         for (i = 0; nameservers[i]; i++)
326                 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
327
328         g_strfreev(nameservers);
329
330         g_slist_free_full(ts_list, g_free);
331
332         ts_list = __connman_timeserver_get_all(service);
333
334         __connman_service_timeserver_changed(service, ts_list);
335
336         if (!ts_list) {
337                 DBG("No timeservers set.");
338                 return 0;
339         }
340
341         ts_recheck_enable();
342
343         __connman_timeserver_sync_next();
344
345         return 0;
346 }
347
348 static int timeserver_start(struct connman_service *service)
349 {
350         char **nameservers;
351         int i;
352
353         DBG("service %p", service);
354
355         i = __connman_service_get_index(service);
356         if (i < 0)
357                 return -EINVAL;
358
359         nameservers = connman_service_get_nameservers(service);
360         if (!nameservers)
361                 return -EINVAL;
362
363         /* Stop an already ongoing resolution, if there is one */
364         if (resolv && resolv_id > 0)
365                 g_resolv_cancel_lookup(resolv, resolv_id);
366
367         /* get rid of the old resolver */
368         if (resolv) {
369                 g_resolv_unref(resolv);
370                 resolv = NULL;
371         }
372
373         resolv = g_resolv_new(i);
374         if (!resolv) {
375                 g_strfreev(nameservers);
376                 return -ENOMEM;
377         }
378
379         if (getenv("CONNMAN_RESOLV_DEBUG"))
380                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
381
382         for (i = 0; nameservers[i]; i++)
383                 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
384
385         g_strfreev(nameservers);
386
387         return __connman_timeserver_sync(service);
388 }
389
390 static void timeserver_stop(void)
391 {
392         DBG(" ");
393
394         if (resolv) {
395                 g_resolv_unref(resolv);
396                 resolv = NULL;
397         }
398
399         g_slist_free_full(ts_list, g_free);
400
401         ts_list = NULL;
402
403         __connman_ntp_stop();
404
405         ts_recheck_disable();
406 }
407
408 int __connman_timeserver_system_set(char **servers)
409 {
410         save_timeservers(servers);
411
412         __connman_timeserver_sync(NULL);
413
414         return 0;
415 }
416
417 char **__connman_timeserver_system_get()
418 {
419         char **servers;
420
421         servers = load_timeservers();
422         return servers;
423 }
424
425 static void default_changed(struct connman_service *default_service)
426 {
427         if (default_service)
428                 timeserver_start(default_service);
429         else
430                 timeserver_stop();
431 }
432
433 static struct connman_notifier timeserver_notifier = {
434         .name                   = "timeserver",
435         .default_changed        = default_changed,
436 };
437
438 int __connman_timeserver_init(void)
439 {
440         DBG("");
441
442         connman_notifier_register(&timeserver_notifier);
443
444         return 0;
445 }
446
447 void __connman_timeserver_cleanup(void)
448 {
449         DBG("");
450
451         connman_notifier_unregister(&timeserver_notifier);
452 }