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