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