gsupplicant: Properly handle WPS properties update
[framework/connectivity/connman.git] / src / main.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <sys/signalfd.h>
33 #include <getopt.h>
34 #include <sys/stat.h>
35 #include <net/if.h>
36 #include <netdb.h>
37
38 #include <gdbus.h>
39
40 #include "connman.h"
41
42 #define DEFAULT_INPUT_REQUEST_TIMEOUT 120 * 1000
43 #define DEFAULT_BROWSER_LAUNCH_TIMEOUT 300 * 1000
44
45 static struct {
46         connman_bool_t bg_scan;
47         char **pref_timeservers;
48         unsigned int *auto_connect;
49         unsigned int *preferred_techs;
50         char **fallback_nameservers;
51         unsigned int timeout_inputreq;
52         unsigned int timeout_browserlaunch;
53 } connman_settings  = {
54         .bg_scan = TRUE,
55         .pref_timeservers = NULL,
56         .auto_connect = NULL,
57         .preferred_techs = NULL,
58         .fallback_nameservers = NULL,
59         .timeout_inputreq = DEFAULT_INPUT_REQUEST_TIMEOUT,
60         .timeout_browserlaunch = DEFAULT_BROWSER_LAUNCH_TIMEOUT,
61 };
62
63 static GKeyFile *load_config(const char *file)
64 {
65         GError *err = NULL;
66         GKeyFile *keyfile;
67
68         keyfile = g_key_file_new();
69
70         g_key_file_set_list_separator(keyfile, ',');
71
72         if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
73                 if (err->code != G_FILE_ERROR_NOENT) {
74                         connman_error("Parsing %s failed: %s", file,
75                                                                 err->message);
76                 }
77
78                 g_error_free(err);
79                 g_key_file_free(keyfile);
80                 return NULL;
81         }
82
83         return keyfile;
84 }
85
86 static uint *parse_service_types(char **str_list, gsize len)
87 {
88         unsigned int *type_list;
89         int i, j;
90         enum connman_service_type type;
91
92         type_list = g_try_new0(unsigned int, len + 1);
93         if (type_list == NULL)
94                 return NULL;
95
96         i = 0;
97         j = 0;
98         while (str_list[i] != NULL)
99         {
100                 type = __connman_service_string2type(str_list[i]);
101
102                 if (type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
103                         type_list[j] = type;
104                         j += 1;
105                 }
106                 i += 1;
107         }
108
109         return type_list;
110 }
111
112 static char **parse_fallback_nameservers(char **nameservers, gsize len)
113 {
114         char **servers;
115         int i, j;
116         struct addrinfo hints;
117         struct addrinfo *addr;
118
119         servers = g_try_new0(char *, len + 1);
120         if (servers == NULL)
121                 return NULL;
122
123         i = 0;
124         j = 0;
125         while (nameservers[i] != NULL) {
126                 memset(&hints, 0, sizeof(struct addrinfo));
127                 hints.ai_flags = AI_NUMERICHOST;
128                 addr = NULL;
129                 if (getaddrinfo(nameservers[i], NULL, &hints, &addr) == 0) {
130                         servers[j] = g_strdup(nameservers[i]);
131                         j += 1;
132                 }
133
134                 freeaddrinfo(addr);
135                 i += 1;
136         }
137
138         return servers;
139 }
140
141 static void parse_config(GKeyFile *config)
142 {
143         GError *error = NULL;
144         gboolean boolean;
145         char **timeservers;
146         char **str_list;
147         gsize len;
148         char *default_auto_connect[] = {
149                 "wifi",
150                 "ethernet",
151                 "cellular",
152                 NULL
153         };
154         int timeout;
155
156         if (config == NULL) {
157                 connman_settings.auto_connect =
158                         parse_service_types(default_auto_connect, 3);
159                 return;
160         }
161
162         DBG("parsing main.conf");
163
164         boolean = g_key_file_get_boolean(config, "General",
165                                                 "BackgroundScanning", &error);
166         if (error == NULL)
167                 connman_settings.bg_scan = boolean;
168
169         g_clear_error(&error);
170
171         timeservers = g_key_file_get_string_list(config, "General",
172                                                 "FallbackTimeservers", NULL, &error);
173         if (error == NULL)
174                 connman_settings.pref_timeservers = timeservers;
175
176         g_clear_error(&error);
177
178         str_list = g_key_file_get_string_list(config, "General",
179                         "DefaultAutoConnectTechnologies", &len, &error);
180
181         if (error == NULL)
182                 connman_settings.auto_connect =
183                         parse_service_types(str_list, len);
184         else
185                 connman_settings.auto_connect =
186                         parse_service_types(default_auto_connect, 3);
187
188         g_strfreev(str_list);
189
190         g_clear_error(&error);
191
192         str_list = g_key_file_get_string_list(config, "General",
193                         "PreferredTechnologies", &len, &error);
194
195         if (error == NULL)
196                 connman_settings.preferred_techs =
197                         parse_service_types(str_list, len);
198
199         g_strfreev(str_list);
200
201         g_clear_error(&error);
202
203         str_list = g_key_file_get_string_list(config, "General",
204                         "FallbackNameservers", &len, &error);
205
206         if (error == NULL)
207                 connman_settings.fallback_nameservers =
208                         parse_fallback_nameservers(str_list, len);
209
210         g_strfreev(str_list);
211
212         g_clear_error(&error);
213
214         timeout = g_key_file_get_integer(config, "General",
215                         "InputRequestTimeout", &error);
216         if (error == NULL && timeout >= 0)
217                 connman_settings.timeout_inputreq = timeout * 1000;
218
219         g_clear_error(&error);
220
221         timeout = g_key_file_get_integer(config, "General",
222                         "BrowserLaunchTimeout", &error);
223         if (error == NULL && timeout >= 0)
224                 connman_settings.timeout_browserlaunch = timeout * 1000;
225
226         g_clear_error(&error);
227 }
228
229 static GMainLoop *main_loop = NULL;
230
231 static unsigned int __terminated = 0;
232
233 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
234                                                         gpointer user_data)
235 {
236         struct signalfd_siginfo si;
237         ssize_t result;
238         int fd;
239
240         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
241                 return FALSE;
242
243         fd = g_io_channel_unix_get_fd(channel);
244
245         result = read(fd, &si, sizeof(si));
246         if (result != sizeof(si))
247                 return FALSE;
248
249         switch (si.ssi_signo) {
250         case SIGINT:
251         case SIGTERM:
252                 if (__terminated == 0) {
253                         connman_info("Terminating");
254                         g_main_loop_quit(main_loop);
255                 }
256
257                 __terminated = 1;
258                 break;
259         }
260
261         return TRUE;
262 }
263
264 static guint setup_signalfd(void)
265 {
266         GIOChannel *channel;
267         guint source;
268         sigset_t mask;
269         int fd;
270
271         sigemptyset(&mask);
272         sigaddset(&mask, SIGINT);
273         sigaddset(&mask, SIGTERM);
274
275         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
276                 perror("Failed to set signal mask");
277                 return 0;
278         }
279
280         fd = signalfd(-1, &mask, 0);
281         if (fd < 0) {
282                 perror("Failed to create signal descriptor");
283                 return 0;
284         }
285
286         channel = g_io_channel_unix_new(fd);
287
288         g_io_channel_set_close_on_unref(channel, TRUE);
289         g_io_channel_set_encoding(channel, NULL, NULL);
290         g_io_channel_set_buffered(channel, FALSE);
291
292         source = g_io_add_watch(channel,
293                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
294                                 signal_handler, NULL);
295
296         g_io_channel_unref(channel);
297
298         return source;
299 }
300
301 static void disconnect_callback(DBusConnection *conn, void *user_data)
302 {
303         connman_error("D-Bus disconnect");
304
305         g_main_loop_quit(main_loop);
306 }
307
308 static gchar *option_debug = NULL;
309 static gchar *option_device = NULL;
310 static gchar *option_plugin = NULL;
311 static gchar *option_nodevice = NULL;
312 static gchar *option_noplugin = NULL;
313 static gchar *option_wifi = NULL;
314 static gboolean option_detach = TRUE;
315 static gboolean option_dnsproxy = TRUE;
316 static gboolean option_compat = FALSE;
317 static gboolean option_version = FALSE;
318
319 static gboolean parse_debug(const char *key, const char *value,
320                                         gpointer user_data, GError **error)
321 {
322         if (value)
323                 option_debug = g_strdup(value);
324         else
325                 option_debug = g_strdup("*");
326
327         return TRUE;
328 }
329
330 static GOptionEntry options[] = {
331         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
332                                 G_OPTION_ARG_CALLBACK, parse_debug,
333                                 "Specify debug options to enable", "DEBUG" },
334         { "device", 'i', 0, G_OPTION_ARG_STRING, &option_device,
335                         "Specify networking device or interface", "DEV" },
336         { "nodevice", 'I', 0, G_OPTION_ARG_STRING, &option_nodevice,
337                         "Specify networking interface to ignore", "DEV" },
338         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
339                                 "Specify plugins to load", "NAME,..." },
340         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
341                                 "Specify plugins not to load", "NAME,..." },
342         { "wifi", 'W', 0, G_OPTION_ARG_STRING, &option_wifi,
343                                 "Specify driver for WiFi/Supplicant", "NAME" },
344         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
345                                 G_OPTION_ARG_NONE, &option_detach,
346                                 "Don't fork daemon to background" },
347         { "nodnsproxy", 'r', G_OPTION_FLAG_REVERSE,
348                                 G_OPTION_ARG_NONE, &option_dnsproxy,
349                                 "Don't enable DNS Proxy" },
350         { "compat", 'c', 0, G_OPTION_ARG_NONE, &option_compat,
351                                 "(obsolete)" },
352         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
353                                 "Show version information and exit" },
354         { NULL },
355 };
356
357 const char *connman_option_get_string(const char *key)
358 {
359         if (g_strcmp0(key, "wifi") == 0) {
360                 if (option_wifi == NULL)
361                         return "nl80211,wext";
362                 else
363                         return option_wifi;
364         }
365
366         return NULL;
367 }
368
369 connman_bool_t connman_setting_get_bool(const char *key)
370 {
371         if (g_str_equal(key, "BackgroundScanning") == TRUE)
372                 return connman_settings.bg_scan;
373
374         return FALSE;
375 }
376
377 char **connman_setting_get_string_list(const char *key)
378 {
379         if (g_str_equal(key, "FallbackTimeservers") == TRUE)
380                 return connman_settings.pref_timeservers;
381
382         if (g_str_equal(key, "FallbackNameservers") == TRUE)
383                 return connman_settings.fallback_nameservers;
384
385         return NULL;
386 }
387
388 unsigned int *connman_setting_get_uint_list(const char *key)
389 {
390         if (g_str_equal(key, "DefaultAutoConnectTechnologies") == TRUE)
391                 return connman_settings.auto_connect;
392
393         if (g_str_equal(key, "PreferredTechnologies") == TRUE)
394                 return connman_settings.preferred_techs;
395
396         return NULL;
397 }
398
399 unsigned int connman_timeout_input_request(void) {
400         return connman_settings.timeout_inputreq;
401 }
402
403 unsigned int connman_timeout_browser_launch(void) {
404         return connman_settings.timeout_browserlaunch;
405 }
406
407 int main(int argc, char *argv[])
408 {
409         GOptionContext *context;
410         GError *error = NULL;
411         DBusConnection *conn;
412         DBusError err;
413         GKeyFile *config;
414         guint signal;
415
416 #ifdef NEED_THREADS
417         if (g_thread_supported() == FALSE)
418                 g_thread_init(NULL);
419 #endif
420
421         context = g_option_context_new(NULL);
422         g_option_context_add_main_entries(context, options, NULL);
423
424         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
425                 if (error != NULL) {
426                         g_printerr("%s\n", error->message);
427                         g_error_free(error);
428                 } else
429                         g_printerr("An unknown error occurred\n");
430                 exit(1);
431         }
432
433         g_option_context_free(context);
434
435         if (option_version == TRUE) {
436                 printf("%s\n", VERSION);
437                 exit(0);
438         }
439
440         if (option_detach == TRUE) {
441                 if (daemon(0, 0)) {
442                         perror("Can't start daemon");
443                         exit(1);
444                 }
445         }
446
447         if (mkdir(STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
448                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
449                 if (errno != EEXIST)
450                         perror("Failed to create state directory");
451         }
452
453         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
454                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
455                 if (errno != EEXIST)
456                         perror("Failed to create storage directory");
457         }
458
459         umask(0077);
460
461         main_loop = g_main_loop_new(NULL, FALSE);
462
463 #ifdef NEED_THREADS
464         if (dbus_threads_init_default() == FALSE) {
465                 fprintf(stderr, "Can't init usage of threads\n");
466                 exit(1);
467         }
468 #endif
469
470         signal = setup_signalfd();
471
472         dbus_error_init(&err);
473
474         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, CONNMAN_SERVICE, &err);
475         if (conn == NULL) {
476                 if (dbus_error_is_set(&err) == TRUE) {
477                         fprintf(stderr, "%s\n", err.message);
478                         dbus_error_free(&err);
479                 } else
480                         fprintf(stderr, "Can't register with system bus\n");
481                 exit(1);
482         }
483
484         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
485
486         __connman_log_init(argv[0], option_debug, option_detach);
487
488         __connman_dbus_init(conn);
489
490         config = load_config(CONFIGDIR "/main.conf");
491         parse_config(config);
492         if (config != NULL)
493                 g_key_file_free(config);
494
495         __connman_storage_migrate();
496         __connman_technology_init();
497         __connman_notifier_init();
498         __connman_service_init();
499         __connman_provider_init();
500         __connman_network_init();
501         __connman_device_init(option_device, option_nodevice);
502
503         __connman_agent_init();
504         __connman_ippool_init();
505         __connman_iptables_init();
506         __connman_nat_init();
507         __connman_tethering_init();
508         __connman_counter_init();
509         __connman_manager_init();
510         __connman_config_init();
511         __connman_stats_init();
512         __connman_clock_init();
513
514         __connman_resolver_init(option_dnsproxy);
515         __connman_ipconfig_init();
516         __connman_rtnl_init();
517         __connman_task_init();
518         __connman_proxy_init();
519         __connman_detect_init();
520         __connman_session_init();
521         __connman_timeserver_init();
522         __connman_connection_init();
523
524         __connman_plugin_init(option_plugin, option_noplugin);
525
526         __connman_rtnl_start();
527         __connman_dhcp_init();
528         __connman_dhcpv6_init();
529         __connman_wpad_init();
530         __connman_wispr_init();
531         __connman_rfkill_init();
532
533         g_free(option_device);
534         g_free(option_plugin);
535         g_free(option_nodevice);
536         g_free(option_noplugin);
537
538         g_main_loop_run(main_loop);
539
540         g_source_remove(signal);
541
542         __connman_rfkill_cleanup();
543         __connman_wispr_cleanup();
544         __connman_wpad_cleanup();
545         __connman_dhcpv6_cleanup();
546         __connman_dhcp_cleanup();
547         __connman_provider_cleanup();
548         __connman_plugin_cleanup();
549         __connman_connection_cleanup();
550         __connman_timeserver_cleanup();
551         __connman_session_cleanup();
552         __connman_detect_cleanup();
553         __connman_proxy_cleanup();
554         __connman_task_cleanup();
555         __connman_rtnl_cleanup();
556         __connman_resolver_cleanup();
557
558         __connman_clock_cleanup();
559         __connman_stats_cleanup();
560         __connman_config_cleanup();
561         __connman_manager_cleanup();
562         __connman_counter_cleanup();
563         __connman_agent_cleanup();
564         __connman_tethering_cleanup();
565         __connman_nat_cleanup();
566         __connman_iptables_cleanup();
567         __connman_ippool_cleanup();
568         __connman_device_cleanup();
569         __connman_network_cleanup();
570         __connman_service_cleanup();
571         __connman_ipconfig_cleanup();
572         __connman_notifier_cleanup();
573         __connman_technology_cleanup();
574
575         __connman_dbus_cleanup();
576
577         __connman_log_cleanup();
578
579         dbus_connection_unref(conn);
580
581         g_main_loop_unref(main_loop);
582
583         if (connman_settings.pref_timeservers != NULL)
584                 g_strfreev(connman_settings.pref_timeservers);
585
586         g_free(connman_settings.auto_connect);
587         g_free(connman_settings.preferred_techs);
588         g_strfreev(connman_settings.fallback_nameservers);
589
590         g_free(option_debug);
591
592         return 0;
593 }