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