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