Imported Upstream version 1.40
[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 #include <sys/time.h>
33
34 #include "connman.h"
35
36 #define TS_RECHECK_INTERVAL     7200
37
38 static struct connman_service *ts_service;
39 static GSList *timeservers_list = NULL;
40 static GSList *ts_list = NULL;
41 static char *ts_current = NULL;
42 static int ts_recheck_id = 0;
43 static int ts_backoff_id = 0;
44 static bool ts_is_synced = false;
45
46 static GResolv *resolv = NULL;
47 static int resolv_id = 0;
48
49 static void sync_next(void);
50
51 static void resolv_debug(const char *str, void *data)
52 {
53         connman_info("%s: %s\n", (const char *) data, str);
54 }
55
56 static void ntp_callback(bool success, void *user_data)
57 {
58         dbus_uint64_t timestamp;
59         struct timeval tv;
60
61         DBG("success %d", success);
62
63         __connman_timeserver_set_synced(success);
64         if (!success) {
65                 sync_next();
66                 return;
67         }
68
69         if (gettimeofday(&tv, NULL) < 0) {
70                 connman_warn("Failed to get current time");
71         }
72
73         timestamp = tv.tv_sec;
74         connman_dbus_property_changed_basic(
75                                         CONNMAN_MANAGER_PATH,
76                                         CONNMAN_CLOCK_INTERFACE, "Time",
77                                         DBUS_TYPE_UINT64, &timestamp);
78 }
79
80 static void save_timeservers(char **servers)
81 {
82         GKeyFile *keyfile;
83         int cnt;
84
85         keyfile = __connman_storage_load_global();
86         if (!keyfile)
87                 keyfile = g_key_file_new();
88
89         for (cnt = 0; servers && servers[cnt]; cnt++);
90
91         g_key_file_set_string_list(keyfile, "global", "Timeservers",
92                            (const gchar **)servers, cnt);
93
94         __connman_storage_save_global(keyfile);
95
96         g_key_file_free(keyfile);
97 }
98
99 static char **load_timeservers(void)
100 {
101         GKeyFile *keyfile;
102         char **servers = NULL;
103
104         keyfile = __connman_storage_load_global();
105         if (!keyfile)
106                 return NULL;
107
108         servers = g_key_file_get_string_list(keyfile, "global",
109                                                 "Timeservers", NULL, NULL);
110
111         g_key_file_free(keyfile);
112
113         return servers;
114 }
115
116 static void resolv_result(GResolvResultStatus status, char **results,
117                                 gpointer user_data)
118 {
119         int i;
120
121         DBG("status %d", status);
122
123         if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
124                 if (results) {
125                         /* prepend the results in reverse order */
126
127                         for (i = 0; results[i]; i++)
128                                 /* count */;
129                         i--;
130
131                         for (; i >= 0; i--) {
132                                 DBG("result[%d]: %s", i, results[i]);
133
134                                 ts_list = __connman_timeserver_add_list(
135                                         ts_list, results[i]);
136                         }
137                 }
138         }
139
140         sync_next();
141 }
142
143 /*
144  * Once the timeserver list (timeserver_list) is created, we start
145  * querying the servers one by one. If resolving fails on one of them,
146  * we move to the next one. The user can enter either an IP address or
147  * a URL for the timeserver. We only resolve the URLs. Once we have an
148  * IP for the NTP server, we start querying it for time corrections.
149  */
150 static void timeserver_sync_start(void)
151 {
152         GSList *list;
153
154         for (list = timeservers_list; list; list = list->next) {
155                 char *timeserver = list->data;
156
157                 ts_list = g_slist_prepend(ts_list, g_strdup(timeserver));
158         }
159         ts_list = g_slist_reverse(ts_list);
160
161         sync_next();
162 }
163
164 static gboolean timeserver_sync_restart(gpointer user_data)
165 {
166         timeserver_sync_start();
167         ts_backoff_id = 0;
168
169         return FALSE;
170 }
171
172 /*
173  * Select the next time server from the working list (ts_list) because
174  * for some reason the first time server in the list didn't work. If
175  * none of the server did work we start over with the first server
176  * with a backoff.
177  */
178 static void sync_next(void)
179 {
180         if (ts_current) {
181                 g_free(ts_current);
182                 ts_current = NULL;
183         }
184
185         __connman_ntp_stop();
186
187         while (ts_list) {
188                 ts_current = ts_list->data;
189                 ts_list = g_slist_delete_link(ts_list, ts_list);
190
191                 /* if it's an IP, directly query it. */
192                 if (connman_inet_check_ipaddress(ts_current) > 0) {
193                         DBG("Using timeserver %s", ts_current);
194                         __connman_ntp_start(ts_current, ntp_callback, NULL);
195                         return;
196                 }
197
198                 DBG("Resolving timeserver %s", ts_current);
199                 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
200                                                 resolv_result, NULL);
201                 return;
202         }
203
204         DBG("No timeserver could be used, restart probing in 5 seconds");
205         ts_backoff_id = g_timeout_add_seconds(5, timeserver_sync_restart, NULL);
206 }
207
208 GSList *__connman_timeserver_add_list(GSList *server_list,
209                 const char *timeserver)
210 {
211         GSList *list = server_list;
212
213         if (!timeserver)
214                 return server_list;
215
216         while (list) {
217                 char *existing_server = list->data;
218                 if (strcmp(timeserver, existing_server) == 0)
219                         return server_list;
220                 list = g_slist_next(list);
221         }
222         return g_slist_prepend(server_list, g_strdup(timeserver));
223 }
224
225 /*
226  * __connman_timeserver_get_all function creates the timeserver
227  * list which will be used to determine NTP server for time corrections.
228  * The service settings take priority over the global timeservers.
229  */
230 GSList *__connman_timeserver_get_all(struct connman_service *service)
231 {
232         GSList *list = NULL;
233         struct connman_network *network;
234         char **timeservers;
235         char **service_ts;
236         char **service_ts_config;
237         const char *service_gw;
238         char **fallback_ts;
239         int index, i;
240
241         if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
242                 return NULL;
243
244         service_ts_config = connman_service_get_timeservers_config(service);
245
246         /* First add Service Timeservers.Configuration to the list */
247         for (i = 0; service_ts_config && service_ts_config[i];
248                         i++)
249                 list = __connman_timeserver_add_list(list,
250                                 service_ts_config[i]);
251
252         service_ts = connman_service_get_timeservers(service);
253
254         /* Then add Service Timeservers via DHCP to the list */
255         for (i = 0; service_ts && service_ts[i]; i++)
256                 list = __connman_timeserver_add_list(list, service_ts[i]);
257
258         /*
259          * Then add Service Gateway to the list, if UseGatewaysAsTimeservers
260          * configuration option is set to true.
261          */
262         if (connman_setting_get_bool("UseGatewaysAsTimeservers")) {
263                 network = __connman_service_get_network(service);
264                 if (network) {
265                         index = connman_network_get_index(network);
266                         service_gw = __connman_ipconfig_get_gateway_from_index(index,
267                                 CONNMAN_IPCONFIG_TYPE_ALL);
268
269                         if (service_gw)
270                                 list = __connman_timeserver_add_list(list, service_gw);
271                 }
272         }
273
274         /* Then add Global Timeservers to the list */
275         timeservers = load_timeservers();
276
277         for (i = 0; timeservers && timeservers[i]; i++)
278                 list = __connman_timeserver_add_list(list, timeservers[i]);
279
280         g_strfreev(timeservers);
281
282         fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
283
284         /* Lastly add the fallback servers */
285         for (i = 0; fallback_ts && fallback_ts[i]; i++)
286                 list = __connman_timeserver_add_list(list, fallback_ts[i]);
287
288         return g_slist_reverse(list);
289 }
290
291 static gboolean ts_recheck(gpointer user_data)
292 {
293         struct connman_service *service;
294         GSList *ts;
295
296         ts = __connman_timeserver_get_all(connman_service_get_default());
297
298         if (!ts) {
299                 DBG("timeservers disabled");
300
301                 return TRUE;
302         }
303
304         if (g_strcmp0(ts_current, ts->data) != 0) {
305                 DBG("current %s preferred %s", ts_current, (char *)ts->data);
306
307                 g_slist_free_full(ts, g_free);
308
309                 service = connman_service_get_default();
310                 __connman_timeserver_sync(service);
311
312                 return FALSE;
313         }
314
315         DBG("");
316
317         g_slist_free_full(ts, g_free);
318
319         return TRUE;
320 }
321
322 static void ts_recheck_disable(void)
323 {
324         if (ts_recheck_id == 0)
325                 return;
326
327         g_source_remove(ts_recheck_id);
328         ts_recheck_id = 0;
329
330         if (ts_backoff_id) {
331                 g_source_remove(ts_backoff_id);
332                 ts_backoff_id = 0;
333         }
334
335         if (ts_current) {
336                 g_free(ts_current);
337                 ts_current = NULL;
338         }
339 }
340
341 static void ts_recheck_enable(void)
342 {
343         if (ts_recheck_id > 0)
344                 return;
345
346         ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
347                         NULL);
348 }
349
350 static void ts_reset(struct connman_service *service)
351 {
352         char **nameservers;
353         int i;
354
355         if (!resolv)
356                 return;
357
358         __connman_timeserver_set_synced(false);
359
360         /*
361          * Before we start creating the new timeserver list we must stop
362          * any ongoing ntp query and server resolution.
363          */
364
365         __connman_ntp_stop();
366
367         ts_recheck_disable();
368
369         if (resolv_id > 0)
370                 g_resolv_cancel_lookup(resolv, resolv_id);
371
372         g_resolv_flush_nameservers(resolv);
373
374         nameservers = connman_service_get_nameservers(service);
375         if (nameservers) {
376                 for (i = 0; nameservers[i]; i++)
377                         g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
378
379                 g_strfreev(nameservers);
380         }
381
382         g_slist_free_full(timeservers_list, g_free);
383
384         timeservers_list = __connman_timeserver_get_all(service);
385
386         __connman_service_timeserver_changed(service, timeservers_list);
387
388         if (!timeservers_list) {
389                 DBG("No timeservers set.");
390                 return;
391         }
392
393         ts_recheck_enable();
394
395         ts_service = service;
396         timeserver_sync_start();
397 }
398
399 void __connman_timeserver_sync(struct connman_service *service)
400 {
401         if (!service || ts_service == service)
402                 return;
403
404         ts_reset(service);
405 }
406
407 void __connman_timeserver_conf_update(struct connman_service *service)
408 {
409         if (!service || (ts_service && ts_service != service))
410                 return;
411
412         ts_reset(service);
413 }
414
415
416 bool __connman_timeserver_is_synced(void)
417 {
418         return ts_is_synced;
419 }
420
421 void __connman_timeserver_set_synced(bool status)
422 {
423         dbus_bool_t is_synced;
424
425         if (ts_is_synced == status)
426                 return;
427
428         ts_is_synced = status;
429         is_synced = status;
430         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
431                                 CONNMAN_CLOCK_INTERFACE, "TimeserverSynced",
432                                 DBUS_TYPE_BOOLEAN, &is_synced);
433 }
434
435 static int timeserver_start(struct connman_service *service)
436 {
437         char **nameservers;
438         int i;
439
440         DBG("service %p", service);
441
442         i = __connman_service_get_index(service);
443         if (i < 0)
444                 return -EINVAL;
445
446         nameservers = connman_service_get_nameservers(service);
447
448         /* Stop an already ongoing resolution, if there is one */
449         if (resolv && resolv_id > 0)
450                 g_resolv_cancel_lookup(resolv, resolv_id);
451
452         /* get rid of the old resolver */
453         if (resolv) {
454                 g_resolv_unref(resolv);
455                 resolv = NULL;
456         }
457
458         resolv = g_resolv_new(i);
459         if (!resolv) {
460                 g_strfreev(nameservers);
461                 return -ENOMEM;
462         }
463
464         if (getenv("CONNMAN_RESOLV_DEBUG"))
465                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
466
467         if (nameservers) {
468                 for (i = 0; nameservers[i]; i++)
469                         g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
470
471                 g_strfreev(nameservers);
472         }
473
474         __connman_timeserver_sync(service);
475
476         return 0;
477 }
478
479 static void timeserver_stop(void)
480 {
481         DBG(" ");
482
483         ts_service = NULL;
484
485         if (resolv) {
486                 g_resolv_unref(resolv);
487                 resolv = NULL;
488         }
489
490         g_slist_free_full(timeservers_list, g_free);
491         timeservers_list = NULL;
492
493         g_slist_free_full(ts_list, g_free);
494         ts_list = NULL;
495
496         __connman_ntp_stop();
497
498         ts_recheck_disable();
499 }
500
501 int __connman_timeserver_system_set(char **servers)
502 {
503         struct connman_service *service;
504
505         save_timeservers(servers);
506
507         service = connman_service_get_default();
508         if (service)
509                 ts_reset(service);
510
511         return 0;
512 }
513
514 char **__connman_timeserver_system_get()
515 {
516         char **servers;
517
518         servers = load_timeservers();
519         return servers;
520 }
521
522 static void default_changed(struct connman_service *default_service)
523 {
524         if (default_service)
525                 timeserver_start(default_service);
526         else
527                 timeserver_stop();
528 }
529
530 static const struct connman_notifier timeserver_notifier = {
531         .name                   = "timeserver",
532         .default_changed        = default_changed,
533 };
534
535 int __connman_timeserver_init(void)
536 {
537         DBG("");
538
539         connman_notifier_register(&timeserver_notifier);
540
541         return 0;
542 }
543
544 void __connman_timeserver_cleanup(void)
545 {
546         DBG("");
547
548         connman_notifier_unregister(&timeserver_notifier);
549 }