DA: Skip initializing failed_bssids list when eapol failure case
[platform/upstream/connman.git] / vpn / main.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2012-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 "../src/connman.h"
41 #include "vpn.h"
42
43 #include "connman/vpn-dbus.h"
44
45 #define CONFIGMAINFILE CONFIGDIR "/connman-vpn.conf"
46
47 static GMainLoop *main_loop = NULL;
48
49 static unsigned int __terminated = 0;
50
51 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
52                                                         gpointer user_data)
53 {
54         struct signalfd_siginfo si;
55         ssize_t result;
56         int fd;
57
58         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
59                 return FALSE;
60
61         fd = g_io_channel_unix_get_fd(channel);
62
63         result = read(fd, &si, sizeof(si));
64         if (result != sizeof(si))
65                 return FALSE;
66
67         switch (si.ssi_signo) {
68         case SIGINT:
69         case SIGTERM:
70                 if (__terminated == 0) {
71                         connman_info("Terminating");
72                         g_main_loop_quit(main_loop);
73                 }
74
75                 __terminated = 1;
76                 break;
77         }
78
79         return TRUE;
80 }
81
82 static guint setup_signalfd(void)
83 {
84         GIOChannel *channel;
85         guint source;
86         sigset_t mask;
87         int fd;
88
89         sigemptyset(&mask);
90         sigaddset(&mask, SIGINT);
91         sigaddset(&mask, SIGTERM);
92
93         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
94                 perror("Failed to set signal mask");
95                 return 0;
96         }
97
98         fd = signalfd(-1, &mask, 0);
99         if (fd < 0) {
100                 perror("Failed to create signal descriptor");
101                 return 0;
102         }
103
104         channel = g_io_channel_unix_new(fd);
105
106         g_io_channel_set_close_on_unref(channel, TRUE);
107         g_io_channel_set_encoding(channel, NULL, NULL);
108         g_io_channel_set_buffered(channel, FALSE);
109
110         source = g_io_add_watch(channel,
111                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
112                                 signal_handler, NULL);
113
114         g_io_channel_unref(channel);
115
116         return source;
117 }
118
119 static void disconnect_callback(DBusConnection *conn, void *user_data)
120 {
121         connman_error("D-Bus disconnect");
122
123         g_main_loop_quit(main_loop);
124 }
125
126 static gchar *option_config = NULL;
127 static gchar *option_debug = NULL;
128 static gchar *option_plugin = NULL;
129 static gchar *option_noplugin = NULL;
130 static bool option_detach = true;
131 static bool option_version = false;
132
133 static bool parse_debug(const char *key, const char *value,
134                                         gpointer user_data, GError **error)
135 {
136         if (value)
137                 option_debug = g_strdup(value);
138         else
139                 option_debug = g_strdup("*");
140
141         return true;
142 }
143
144 static GOptionEntry options[] = {
145         { "config", 'c', 0, G_OPTION_ARG_STRING, &option_config,
146                                 "Load the specified configuration file "
147                                 "instead of " CONFIGMAINFILE, "FILE" },
148         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
149                                 G_OPTION_ARG_CALLBACK, parse_debug,
150                                 "Specify debug options to enable", "DEBUG" },
151         { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
152                                 "Specify plugins to load", "NAME,..." },
153         { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
154                                 "Specify plugins not to load", "NAME,..." },
155         { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
156                                 G_OPTION_ARG_NONE, &option_detach,
157                                 "Don't fork daemon to background" },
158         { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
159                                 "Show version information and exit" },
160         { NULL },
161 };
162
163 #if defined TIZEN_EXT
164 bool connman_setting_get_bool(const char *key)
165 {
166         return false;
167 }
168
169 unsigned int connman_setting_get_uint(const char *key)
170 {
171         return 0;
172 }
173
174 int connman_setting_get_int(const char *key)
175 {
176         return 0;
177 }
178
179 char *connman_setting_get_string(const char *key)
180 {
181         return NULL;
182 }
183
184 char **connman_setting_get_string_list(const char *key)
185 {
186         return NULL;
187 }
188
189 unsigned int *connman_setting_get_uint_list(const char *key)
190 {
191         return NULL;
192 }
193
194 unsigned int connman_timeout_browser_launch(void)
195 {
196         return 0;
197 }
198 #endif
199
200 /*
201  * This function will be called from generic src/agent.c code so we have
202  * to use connman_ prefix instead of vpn_ one.
203  */
204 unsigned int connman_timeout_input_request(void)
205 {
206         return __vpn_settings_get_timeout_inputreq();
207 }
208
209 int main(int argc, char *argv[])
210 {
211         GOptionContext *context;
212         GError *error = NULL;
213         DBusConnection *conn;
214         DBusError err;
215         guint signal;
216
217         context = g_option_context_new(NULL);
218         g_option_context_add_main_entries(context, options, NULL);
219
220         if (!g_option_context_parse(context, &argc, &argv, &error)) {
221                 if (error) {
222                         g_printerr("%s\n", error->message);
223                         g_error_free(error);
224                 } else
225                         g_printerr("An unknown error occurred\n");
226                 exit(1);
227         }
228
229         g_option_context_free(context);
230
231         if (option_version) {
232                 printf("%s\n", VERSION);
233                 exit(0);
234         }
235
236         if (option_detach) {
237                 if (daemon(0, 0)) {
238                         perror("Can't start daemon");
239                         exit(1);
240                 }
241         }
242
243         if (mkdir(VPN_STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
244                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
245                 if (errno != EEXIST)
246                         perror("Failed to create state directory");
247         }
248
249         /*
250          * At some point the VPN stuff is migrated into VPN_STORAGEDIR
251          * and this mkdir() call can be removed.
252          */
253         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
254                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
255                 if (errno != EEXIST)
256                         perror("Failed to create storage directory");
257         }
258
259         if (mkdir(VPN_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
260                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
261                 if (errno != EEXIST)
262                         perror("Failed to create VPN storage directory");
263         }
264
265         umask(0077);
266
267         main_loop = g_main_loop_new(NULL, FALSE);
268
269         signal = setup_signalfd();
270
271         dbus_error_init(&err);
272
273         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, VPN_SERVICE, &err);
274         if (!conn) {
275                 if (dbus_error_is_set(&err)) {
276                         fprintf(stderr, "%s\n", err.message);
277                         dbus_error_free(&err);
278                 } else
279                         fprintf(stderr, "Can't register with system bus\n");
280                 exit(1);
281         }
282
283         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
284
285         __connman_log_init(argv[0], option_debug, option_detach, false,
286                         "Connection Manager VPN daemon", VERSION);
287         __connman_dbus_init(conn);
288
289         if (!option_config)
290                 __vpn_settings_init(CONFIGMAINFILE);
291         else
292                 __vpn_settings_init(option_config);
293
294         __connman_inotify_init();
295         __connman_agent_init();
296         __vpn_provider_init();
297         __vpn_manager_init();
298         __vpn_ipconfig_init();
299         __vpn_rtnl_init();
300         __connman_task_init();
301         __connman_plugin_init(option_plugin, option_noplugin);
302         __vpn_config_init();
303
304         __vpn_rtnl_start();
305
306         g_free(option_plugin);
307         g_free(option_noplugin);
308
309         g_main_loop_run(main_loop);
310
311         g_source_remove(signal);
312
313         __vpn_config_cleanup();
314         __connman_plugin_cleanup();
315         __connman_task_cleanup();
316         __vpn_rtnl_cleanup();
317         __vpn_ipconfig_cleanup();
318         __vpn_manager_cleanup();
319         __vpn_provider_cleanup();
320         __connman_agent_cleanup();
321         __connman_inotify_cleanup();
322         __connman_dbus_cleanup();
323         __connman_log_cleanup(false);
324         __vpn_settings_cleanup();
325
326         dbus_connection_unref(conn);
327
328         g_main_loop_unref(main_loop);
329
330         g_free(option_debug);
331
332         return 0;
333 }