Add a missing dbus rule for Manager interface
[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 (ts_current[0] == '\0') {
210                         DBG("current time server is empty. ignore next time server..");
211                         return;
212                 }
213
214                 if (!simplified_log)
215 #endif
216                 DBG("Resolving timeserver %s", ts_current);
217 #if defined TIZEN_EXT
218                 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
219                                                 resolv_result, ts_current);
220 #else
221                 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
222                                                 resolv_result, NULL);
223 #endif
224                 return;
225         }
226
227         DBG("No timeserver could be used, restart probing in 5 seconds");
228         ts_backoff_id = g_timeout_add_seconds(5, timeserver_sync_restart, NULL);
229 }
230
231 GSList *__connman_timeserver_add_list(GSList *server_list,
232                 const char *timeserver)
233 {
234         GSList *list = server_list;
235
236         if (!timeserver)
237                 return server_list;
238
239         while (list) {
240                 char *existing_server = list->data;
241                 if (strcmp(timeserver, existing_server) == 0)
242                         return server_list;
243                 list = g_slist_next(list);
244         }
245         return g_slist_prepend(server_list, g_strdup(timeserver));
246 }
247
248 /*
249  * __connman_timeserver_get_all function creates the timeserver
250  * list which will be used to determine NTP server for time corrections.
251  * The service settings take priority over the global timeservers.
252  */
253 GSList *__connman_timeserver_get_all(struct connman_service *service)
254 {
255         GSList *list = NULL;
256         struct connman_network *network;
257         char **timeservers;
258         char **service_ts;
259         char **service_ts_config;
260         const char *service_gw;
261         char **fallback_ts;
262         int index, i;
263
264         if (__connman_clock_timeupdates() == TIME_UPDATES_MANUAL)
265                 return NULL;
266
267         service_ts_config = connman_service_get_timeservers_config(service);
268
269         /* First add Service Timeservers.Configuration to the list */
270         for (i = 0; service_ts_config && service_ts_config[i];
271                         i++)
272                 list = __connman_timeserver_add_list(list,
273                                 service_ts_config[i]);
274
275         service_ts = connman_service_get_timeservers(service);
276
277         /* Then add Service Timeservers via DHCP to the list */
278         for (i = 0; service_ts && service_ts[i]; i++)
279                 list = __connman_timeserver_add_list(list, service_ts[i]);
280
281         /*
282          * Then add Service Gateway to the list, if UseGatewaysAsTimeservers
283          * configuration option is set to true.
284          */
285         if (connman_setting_get_bool("UseGatewaysAsTimeservers")) {
286                 network = __connman_service_get_network(service);
287                 if (network) {
288                         index = connman_network_get_index(network);
289                         service_gw = __connman_ipconfig_get_gateway_from_index(index,
290                                 CONNMAN_IPCONFIG_TYPE_ALL);
291
292                         if (service_gw)
293                                 list = __connman_timeserver_add_list(list, service_gw);
294                 }
295         }
296
297         /* Then add Global Timeservers to the list */
298         timeservers = load_timeservers();
299
300         for (i = 0; timeservers && timeservers[i]; i++)
301                 list = __connman_timeserver_add_list(list, timeservers[i]);
302
303         g_strfreev(timeservers);
304
305         fallback_ts = connman_setting_get_string_list("FallbackTimeservers");
306
307         /* Lastly add the fallback servers */
308         for (i = 0; fallback_ts && fallback_ts[i]; i++)
309                 list = __connman_timeserver_add_list(list, fallback_ts[i]);
310
311         return g_slist_reverse(list);
312 }
313
314 static gboolean ts_recheck(gpointer user_data)
315 {
316         struct connman_service *service;
317         GSList *ts;
318
319         ts = __connman_timeserver_get_all(connman_service_get_default());
320
321         if (!ts) {
322                 DBG("timeservers disabled");
323
324                 return TRUE;
325         }
326
327         if (g_strcmp0(ts_current, ts->data) != 0) {
328                 DBG("current %s preferred %s", ts_current, (char *)ts->data);
329
330                 g_slist_free_full(ts, g_free);
331
332                 service = connman_service_get_default();
333                 __connman_timeserver_sync(service);
334
335                 return FALSE;
336         }
337
338         DBG("");
339
340         g_slist_free_full(ts, g_free);
341
342         return TRUE;
343 }
344
345 static void ts_recheck_disable(void)
346 {
347         if (ts_recheck_id == 0)
348                 return;
349
350         g_source_remove(ts_recheck_id);
351         ts_recheck_id = 0;
352
353         if (ts_backoff_id) {
354                 g_source_remove(ts_backoff_id);
355                 ts_backoff_id = 0;
356         }
357
358         if (ts_current) {
359                 g_free(ts_current);
360                 ts_current = NULL;
361         }
362 }
363
364 static void ts_recheck_enable(void)
365 {
366         if (ts_recheck_id > 0)
367                 return;
368
369         ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
370                         NULL);
371 }
372
373 static void ts_reset(struct connman_service *service)
374 {
375         char **nameservers;
376         int i;
377
378         if (!resolv)
379                 return;
380
381         __connman_timeserver_set_synced(false);
382
383         /*
384          * Before we start creating the new timeserver list we must stop
385          * any ongoing ntp query and server resolution.
386          */
387
388         __connman_ntp_stop();
389
390         ts_recheck_disable();
391
392         if (resolv_id > 0)
393                 g_resolv_cancel_lookup(resolv, resolv_id);
394
395         g_resolv_flush_nameservers(resolv);
396
397         nameservers = connman_service_get_nameservers(service);
398         if (nameservers) {
399                 for (i = 0; nameservers[i]; i++)
400                         g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
401
402                 g_strfreev(nameservers);
403         }
404
405         g_slist_free_full(timeservers_list, g_free);
406
407         timeservers_list = __connman_timeserver_get_all(service);
408
409         __connman_service_timeserver_changed(service, timeservers_list);
410
411         if (!timeservers_list) {
412                 DBG("No timeservers set.");
413                 return;
414         }
415
416         ts_recheck_enable();
417
418         ts_service = service;
419         timeserver_sync_start();
420 }
421
422 void __connman_timeserver_sync(struct connman_service *service)
423 {
424         if (!service || ts_service == service)
425                 return;
426
427         ts_reset(service);
428 }
429
430 void __connman_timeserver_conf_update(struct connman_service *service)
431 {
432         if (!service || (ts_service && ts_service != service))
433                 return;
434
435         ts_reset(service);
436 }
437
438
439 bool __connman_timeserver_is_synced(void)
440 {
441         return ts_is_synced;
442 }
443
444 void __connman_timeserver_set_synced(bool status)
445 {
446         dbus_bool_t is_synced;
447
448         if (ts_is_synced == status)
449                 return;
450
451         ts_is_synced = status;
452         is_synced = status;
453         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
454                                 CONNMAN_CLOCK_INTERFACE, "TimeserverSynced",
455                                 DBUS_TYPE_BOOLEAN, &is_synced);
456 }
457
458 static int timeserver_start(struct connman_service *service)
459 {
460         char **nameservers;
461         int i;
462
463         DBG("service %p", service);
464
465         i = __connman_service_get_index(service);
466         if (i < 0)
467                 return -EINVAL;
468
469         nameservers = connman_service_get_nameservers(service);
470
471         /* Stop an already ongoing resolution, if there is one */
472         if (resolv && resolv_id > 0)
473                 g_resolv_cancel_lookup(resolv, resolv_id);
474
475         /* get rid of the old resolver */
476         if (resolv) {
477                 g_resolv_unref(resolv);
478                 resolv = NULL;
479         }
480
481         resolv = g_resolv_new(i);
482         if (!resolv) {
483                 g_strfreev(nameservers);
484                 return -ENOMEM;
485         }
486
487         if (getenv("CONNMAN_RESOLV_DEBUG"))
488                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
489
490         if (nameservers) {
491                 for (i = 0; nameservers[i]; i++)
492                         g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
493
494                 g_strfreev(nameservers);
495         }
496
497         __connman_timeserver_sync(service);
498
499         return 0;
500 }
501
502 static void timeserver_stop(void)
503 {
504         DBG(" ");
505
506         ts_service = NULL;
507
508         if (resolv) {
509                 g_resolv_unref(resolv);
510                 resolv = NULL;
511         }
512
513         g_slist_free_full(timeservers_list, g_free);
514         timeservers_list = NULL;
515
516         g_slist_free_full(ts_list, g_free);
517         ts_list = NULL;
518
519         __connman_ntp_stop();
520
521         ts_recheck_disable();
522 }
523
524 int __connman_timeserver_system_set(char **servers)
525 {
526         struct connman_service *service;
527
528         save_timeservers(servers);
529
530         service = connman_service_get_default();
531         if (service)
532                 ts_reset(service);
533
534         return 0;
535 }
536
537 char **__connman_timeserver_system_get()
538 {
539         char **servers;
540
541         servers = load_timeservers();
542         return servers;
543 }
544
545 static void default_changed(struct connman_service *default_service)
546 {
547         if (default_service)
548                 timeserver_start(default_service);
549         else
550                 timeserver_stop();
551 }
552
553 static const struct connman_notifier timeserver_notifier = {
554         .name                   = "timeserver",
555         .default_changed        = default_changed,
556 };
557
558 int __connman_timeserver_init(void)
559 {
560         DBG("");
561
562         connman_notifier_register(&timeserver_notifier);
563
564         return 0;
565 }
566
567 void __connman_timeserver_cleanup(void)
568 {
569         DBG("");
570
571         connman_notifier_unregister(&timeserver_notifier);
572 }