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