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