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