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