Add support to get PMF information
[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         if (!simplified_log)
110                 DBG("added server %s", server);
111
112         if (!simplified_log)
113 #endif
114         DBG("status %d", status);
115
116         if (status == G_RESOLV_RESULT_STATUS_SUCCESS) {
117                 if (results) {
118                         /* prepend the results in reverse order */
119
120                         for (i = 0; results[i]; i++)
121                                 /* count */;
122                         i--;
123
124                         for (; i >= 0; i--) {
125                                 DBG("result[%d]: %s", i, results[i]);
126
127                                 ts_list = __connman_timeserver_add_list(
128                                         ts_list, results[i]);
129                         }
130                 }
131         }
132
133         sync_next();
134 }
135
136 /*
137  * Once the timeserver list (timeserver_list) is created, we start
138  * querying the servers one by one. If resolving fails on one of them,
139  * we move to the next one. The user can enter either an IP address or
140  * a URL for the timeserver. We only resolve the URLs. Once we have an
141  * IP for the NTP server, we start querying it for time corrections.
142  */
143 static void timeserver_sync_start(void)
144 {
145         GSList *list;
146
147         for (list = timeservers_list; list; list = list->next) {
148                 char *timeserver = list->data;
149
150                 ts_list = g_slist_prepend(ts_list, g_strdup(timeserver));
151         }
152         ts_list = g_slist_reverse(ts_list);
153
154         sync_next();
155 }
156
157 static gboolean timeserver_sync_restart(gpointer user_data)
158 {
159         timeserver_sync_start();
160         ts_backoff_id = 0;
161
162         return FALSE;
163 }
164
165 /*
166  * Select the next time server from the working list (ts_list) because
167  * for some reason the first time server in the list didn't work. If
168  * none of the server did work we start over with the first server
169  * with a backoff.
170  */
171 static void sync_next(void)
172 {
173         if (ts_current) {
174                 g_free(ts_current);
175                 ts_current = NULL;
176         }
177
178         __connman_ntp_stop();
179
180         while (ts_list) {
181                 ts_current = ts_list->data;
182                 ts_list = g_slist_delete_link(ts_list, ts_list);
183
184                 /* if it's an IP, directly query it. */
185                 if (connman_inet_check_ipaddress(ts_current) > 0) {
186                         DBG("Using timeserver %s", ts_current);
187                         __connman_ntp_start(ts_current, ntp_callback, NULL);
188                         return;
189                 }
190 #if defined TIZEN_EXT
191         if (!simplified_log)
192 #endif
193                 DBG("Resolving timeserver %s", ts_current);
194 #if defined TIZEN_EXT
195                 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
196                                                 resolv_result, ts_current);
197 #else
198                 resolv_id = g_resolv_lookup_hostname(resolv, ts_current,
199                                                 resolv_result, NULL);
200 #endif
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         GSList *ts;
294
295         ts = __connman_timeserver_get_all(connman_service_get_default());
296
297         if (!ts) {
298                 DBG("timeservers disabled");
299
300                 return TRUE;
301         }
302
303         if (g_strcmp0(ts_current, ts->data) != 0) {
304                 DBG("current %s preferred %s", ts_current, (char *)ts->data);
305
306                 g_slist_free_full(ts, g_free);
307
308                 __connman_timeserver_sync(NULL);
309
310                 return FALSE;
311         }
312
313         DBG("");
314
315         g_slist_free_full(ts, g_free);
316
317         return TRUE;
318 }
319
320 static void ts_recheck_disable(void)
321 {
322         if (ts_recheck_id == 0)
323                 return;
324
325         g_source_remove(ts_recheck_id);
326         ts_recheck_id = 0;
327
328         if (ts_backoff_id) {
329                 g_source_remove(ts_backoff_id);
330                 ts_backoff_id = 0;
331         }
332
333         if (ts_current) {
334                 g_free(ts_current);
335                 ts_current = NULL;
336         }
337 }
338
339 static void ts_recheck_enable(void)
340 {
341         if (ts_recheck_id > 0)
342                 return;
343
344         ts_recheck_id = g_timeout_add_seconds(TS_RECHECK_INTERVAL, ts_recheck,
345                         NULL);
346 }
347
348 /*
349  * This function must be called every time the default service changes, the
350  * service timeserver(s) or gateway changes or the global timeserver(s) changes.
351  */
352 int __connman_timeserver_sync(struct connman_service *default_service)
353 {
354         struct connman_service *service;
355         char **nameservers;
356         int i;
357
358         if (default_service)
359                 service = default_service;
360         else
361                 service = connman_service_get_default();
362
363         if (!service)
364                 return -EINVAL;
365
366 #if !defined TIZEN_EXT
367         if (service == ts_service)
368                 return -EALREADY;
369 #endif
370
371         if (!resolv)
372                 return 0;
373         /*
374          * Before we start creating the new timeserver list we must stop
375          * any ongoing ntp query and server resolution.
376          */
377
378         __connman_ntp_stop();
379
380         ts_recheck_disable();
381
382         if (resolv_id > 0)
383                 g_resolv_cancel_lookup(resolv, resolv_id);
384
385         g_resolv_flush_nameservers(resolv);
386
387         nameservers = connman_service_get_nameservers(service);
388         if (!nameservers)
389                 return -EINVAL;
390
391         for (i = 0; nameservers[i]; i++)
392                 g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
393
394         g_strfreev(nameservers);
395
396         g_slist_free_full(timeservers_list, g_free);
397
398         timeservers_list = __connman_timeserver_get_all(service);
399
400         __connman_service_timeserver_changed(service, timeservers_list);
401
402         if (!timeservers_list) {
403                 DBG("No timeservers set.");
404                 return 0;
405         }
406
407         ts_recheck_enable();
408
409         ts_service = service;
410         timeserver_sync_start();
411
412         return 0;
413 }
414
415 static int timeserver_start(struct connman_service *service)
416 {
417         char **nameservers;
418         int i;
419
420         DBG("service %p", service);
421
422         i = __connman_service_get_index(service);
423         if (i < 0)
424                 return -EINVAL;
425
426         nameservers = connman_service_get_nameservers(service);
427
428         /* Stop an already ongoing resolution, if there is one */
429         if (resolv && resolv_id > 0)
430                 g_resolv_cancel_lookup(resolv, resolv_id);
431
432         /* get rid of the old resolver */
433         if (resolv) {
434                 g_resolv_unref(resolv);
435                 resolv = NULL;
436         }
437
438         resolv = g_resolv_new(i);
439         if (!resolv) {
440                 g_strfreev(nameservers);
441                 return -ENOMEM;
442         }
443
444         if (getenv("CONNMAN_RESOLV_DEBUG"))
445                 g_resolv_set_debug(resolv, resolv_debug, "RESOLV");
446
447         if (nameservers) {
448                 for (i = 0; nameservers[i]; i++)
449                         g_resolv_add_nameserver(resolv, nameservers[i], 53, 0);
450
451                 g_strfreev(nameservers);
452         }
453
454         return __connman_timeserver_sync(service);
455 }
456
457 static void timeserver_stop(void)
458 {
459         DBG(" ");
460
461         ts_service = NULL;
462
463         if (resolv) {
464                 g_resolv_unref(resolv);
465                 resolv = NULL;
466         }
467
468         g_slist_free_full(timeservers_list, g_free);
469         timeservers_list = NULL;
470
471         g_slist_free_full(ts_list, g_free);
472         ts_list = NULL;
473
474         __connman_ntp_stop();
475
476         ts_recheck_disable();
477 }
478
479 int __connman_timeserver_system_set(char **servers)
480 {
481         save_timeservers(servers);
482
483         __connman_timeserver_sync(NULL);
484
485         return 0;
486 }
487
488 char **__connman_timeserver_system_get()
489 {
490         char **servers;
491
492         servers = load_timeservers();
493         return servers;
494 }
495
496 static void default_changed(struct connman_service *default_service)
497 {
498         if (default_service)
499                 timeserver_start(default_service);
500         else
501                 timeserver_stop();
502 }
503
504 static const struct connman_notifier timeserver_notifier = {
505         .name                   = "timeserver",
506         .default_changed        = default_changed,
507 };
508
509 int __connman_timeserver_init(void)
510 {
511         DBG("");
512
513         connman_notifier_register(&timeserver_notifier);
514
515         return 0;
516 }
517
518 void __connman_timeserver_cleanup(void)
519 {
520         DBG("");
521
522         connman_notifier_unregister(&timeserver_notifier);
523 }