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