474c62fb2c58bdee5234890c8747adef3a4df203
[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 #if defined TIZEN_EXT
236 unsigned int connman_setting_get_uint(const char *key)
237 {
238         return 0;
239 }
240
241 int connman_setting_get_int(const char *key)
242 {
243         return 0;
244 }
245 #endif
246
247 char **connman_setting_get_string_list(const char *key)
248 {
249         return NULL;
250 }
251
252 unsigned int *connman_setting_get_uint_list(const char *key)
253 {
254         return NULL;
255 }
256
257 /*
258  * This function will be called from generic src/agent.c code so we have
259  * to use connman_ prefix instead of vpn_ one.
260  */
261 unsigned int connman_timeout_input_request(void)
262 {
263         return connman_vpn_settings.timeout_inputreq;
264 }
265
266 unsigned int connman_timeout_browser_launch(void)
267 {
268         return connman_vpn_settings.timeout_browserlaunch;
269 }
270
271 const char *connman_option_get_string(const char *key)
272 {
273         return NULL;
274 }
275
276 int main(int argc, char *argv[])
277 {
278         GOptionContext *context;
279         GError *error = NULL;
280         DBusConnection *conn;
281         DBusError err;
282         guint signal;
283
284         context = g_option_context_new(NULL);
285         g_option_context_add_main_entries(context, options, NULL);
286
287         if (!g_option_context_parse(context, &argc, &argv, &error)) {
288                 if (error) {
289                         g_printerr("%s\n", error->message);
290                         g_error_free(error);
291                 } else
292                         g_printerr("An unknown error occurred\n");
293                 exit(1);
294         }
295
296         g_option_context_free(context);
297
298         if (option_version) {
299                 printf("%s\n", VERSION);
300                 exit(0);
301         }
302
303         if (option_detach) {
304                 if (daemon(0, 0)) {
305                         perror("Can't start daemon");
306                         exit(1);
307                 }
308         }
309
310         if (mkdir(VPN_STATEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
311                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
312                 if (errno != EEXIST)
313                         perror("Failed to create state directory");
314         }
315
316         /*
317          * At some point the VPN stuff is migrated into VPN_STORAGEDIR
318          * and this mkdir() call can be removed.
319          */
320         if (mkdir(STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
321                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
322                 if (errno != EEXIST)
323                         perror("Failed to create storage directory");
324         }
325
326         if (mkdir(VPN_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
327                                 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
328                 if (errno != EEXIST)
329                         perror("Failed to create VPN storage directory");
330         }
331
332         umask(0077);
333
334         main_loop = g_main_loop_new(NULL, FALSE);
335
336         signal = setup_signalfd();
337
338         dbus_error_init(&err);
339
340         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, VPN_SERVICE, &err);
341         if (!conn) {
342                 if (dbus_error_is_set(&err)) {
343                         fprintf(stderr, "%s\n", err.message);
344                         dbus_error_free(&err);
345                 } else
346                         fprintf(stderr, "Can't register with system bus\n");
347                 exit(1);
348         }
349
350         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
351
352         __connman_log_init(argv[0], option_debug, option_detach, false,
353                         "Connection Manager VPN daemon", VERSION);
354         __connman_dbus_init(conn);
355
356         if (!option_config)
357                 config_init(CONFIGMAINFILE);
358         else
359                 config_init(option_config);
360
361         __connman_inotify_init();
362         __connman_agent_init();
363         __vpn_provider_init(option_routes);
364         __vpn_manager_init();
365         __vpn_ipconfig_init();
366         __vpn_rtnl_init();
367         __connman_task_init();
368         __connman_plugin_init(option_plugin, option_noplugin);
369         __vpn_config_init();
370
371         __vpn_rtnl_start();
372
373         g_free(option_plugin);
374         g_free(option_noplugin);
375
376         g_main_loop_run(main_loop);
377
378         g_source_remove(signal);
379
380         __vpn_config_cleanup();
381         __connman_plugin_cleanup();
382         __connman_task_cleanup();
383         __vpn_rtnl_cleanup();
384         __vpn_ipconfig_cleanup();
385         __vpn_manager_cleanup();
386         __vpn_provider_cleanup();
387         __connman_agent_cleanup();
388         __connman_inotify_cleanup();
389         __connman_dbus_cleanup();
390         __connman_log_cleanup(false);
391
392         dbus_connection_unref(conn);
393
394         g_main_loop_unref(main_loop);
395
396         g_free(option_debug);
397
398         return 0;
399 }