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