ntp: Fix ntpd_running return TRUE most of the time
[framework/connectivity/connman.git] / plugins / openvpn.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2010  BMW Car IT GmbH. 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 <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <net/if.h>
31
32 #include <glib.h>
33
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/provider.h>
37 #include <connman/log.h>
38 #include <connman/task.h>
39 #include <connman/dbus.h>
40 #include <connman/ipconfig.h>
41
42 #include "vpn.h"
43
44 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
45
46 static DBusConnection *connection;
47
48 struct {
49         const char *cm_opt;
50         const char *ov_opt;
51 } ov_options[] = {
52         { "Host", "--remote" },
53         { "OpenVPN.CACert", "--ca" },
54         { "OpenVPN.Cert", "--cert" },
55         { "OpenVPN.Key", "--key" },
56         { "OpenVPN.MTU", "--mtu" },
57         { "OpenVPN.Proto", "--proto" },
58         { "OpenVPN.Port", "--port" },
59         { "OpenVPN.AuthUserPass", "--auth-user-pass" },
60         { "OpenVPN.TLSRemote", "--tls-remote" },
61         { "OpenVPN.Cipher", "--cipher" },
62         { "OpenVPN.Auth", "--auth" },
63         { "OpenVPN.CompLZO", "--comp-lzo" },
64 };
65
66 static void ov_append_dns_entries(const char *key, const char *value,
67                                         char **dns_entries)
68 {
69         gchar **options;
70
71         if (g_str_has_prefix(key, "foreign_option_") == FALSE)
72                 return;
73
74         options = g_strsplit(value, " ", 3);
75         if (options[0] != NULL &&
76                 !strcmp(options[0], "dhcp-option") &&
77                         options[1] != NULL &&
78                         !strcmp(options[1], "DNS") &&
79                                 options[2] != NULL) {
80
81                 if (*dns_entries != NULL) {
82                         char *tmp;
83
84                         tmp = g_strjoin(" ", *dns_entries,
85                                                 options[2], NULL);
86                         g_free(*dns_entries);
87                         *dns_entries = tmp;
88                 } else {
89                         *dns_entries = g_strdup(options[2]);
90                 }
91         }
92
93         g_strfreev(options);
94 }
95
96 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
97 {
98         DBusMessageIter iter, dict;
99         const char *reason, *key, *value;
100         const char *domain = NULL;
101         char *nameservers = NULL;
102         char *address = NULL, *gateway = NULL, *peer = NULL;
103         struct connman_ipaddress *ipaddress;
104
105         dbus_message_iter_init(msg, &iter);
106
107         dbus_message_iter_get_basic(&iter, &reason);
108         dbus_message_iter_next(&iter);
109
110         if (!provider) {
111                 connman_error("No provider found");
112                 return VPN_STATE_FAILURE;
113         }
114
115         if (strcmp(reason, "up"))
116                 return VPN_STATE_DISCONNECT;
117
118         domain = connman_provider_get_string(provider, "VPN.Domain");
119
120         dbus_message_iter_recurse(&iter, &dict);
121
122         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
123                 DBusMessageIter entry;
124
125                 dbus_message_iter_recurse(&dict, &entry);
126                 dbus_message_iter_get_basic(&entry, &key);
127                 dbus_message_iter_next(&entry);
128                 dbus_message_iter_get_basic(&entry, &value);
129
130                 DBG("%s = %s", key, value);
131
132                 if (!strcmp(key, "trusted_ip")) {
133                         connman_provider_set_string(provider, "Gateway", value);
134                         gateway = g_strdup(value);
135                 }
136
137                 if (!strcmp(key, "ifconfig_local")) {
138                         connman_provider_set_string(provider, "Address", value);
139                         address = g_strdup(value);
140                 }
141
142                 if (!strcmp(key, "ifconfig_remote")) {
143                         connman_provider_set_string(provider, "Peer", value);
144                         peer = g_strdup(value);
145                 }
146
147                 if (g_str_has_prefix(key, "route_") == TRUE)
148                         connman_provider_append_route(provider, key, value);
149
150                 ov_append_dns_entries(key, value, &nameservers);
151
152                 dbus_message_iter_next(&dict);
153         }
154
155         ipaddress = connman_ipaddress_alloc(AF_INET);
156         if (ipaddress == NULL) {
157                 g_free(nameservers);
158                 g_free(address);
159                 g_free(gateway);
160                 g_free(peer);
161
162                 return VPN_STATE_FAILURE;
163         }
164
165         connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
166         connman_ipaddress_set_peer(ipaddress, peer);
167         connman_provider_set_ipaddress(provider, ipaddress);
168
169         connman_provider_set_nameservers(provider, nameservers);
170
171         g_free(nameservers);
172         g_free(address);
173         g_free(gateway);
174         g_free(peer);
175         connman_ipaddress_free(ipaddress);
176
177         return VPN_STATE_CONNECT;
178 }
179
180 static int task_append_config_data(struct connman_provider *provider,
181                                         struct connman_task *task)
182 {
183         const char *option;
184         int i;
185
186         for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
187                 option = connman_provider_get_string(provider,
188                                         ov_options[i].cm_opt);
189                 if (option == NULL)
190                         continue;
191
192                 if (connman_task_add_argument(task,
193                                         ov_options[i].ov_opt, option) < 0) {
194                         return -EIO;
195                 }
196         }
197
198         return 0;
199 }
200
201 static int ov_connect(struct connman_provider *provider,
202                 struct connman_task *task, const char *if_name)
203 {
204         const char *option;
205         int err, fd;
206
207         option = connman_provider_get_string(provider, "Host");
208         if (option == NULL) {
209                 connman_error("Host not set; cannot enable VPN");
210                 return -EINVAL;
211         }
212
213         task_append_config_data(provider, task);
214
215         connman_task_add_argument(task, "--syslog", NULL);
216
217         connman_task_add_argument(task, "--script-security", "2");
218
219         connman_task_add_argument(task, "--up",
220                                         SCRIPTDIR "/openvpn-script");
221         connman_task_add_argument(task, "--up-restart", NULL);
222
223         connman_task_add_argument(task, "--setenv", NULL);
224         connman_task_add_argument(task, "CONNMAN_BUSNAME",
225                                         dbus_bus_get_unique_name(connection));
226
227         connman_task_add_argument(task, "--setenv", NULL);
228         connman_task_add_argument(task, "CONNMAN_INTERFACE",
229                                         CONNMAN_TASK_INTERFACE);
230
231         connman_task_add_argument(task, "--setenv", NULL);
232         connman_task_add_argument(task, "CONNMAN_PATH",
233                                         connman_task_get_path(task));
234
235         connman_task_add_argument(task, "--dev", if_name);
236         connman_task_add_argument(task, "--dev-type", "tun");
237
238         connman_task_add_argument(task, "--tls-client", NULL);
239         connman_task_add_argument(task, "--nobind", NULL);
240         connman_task_add_argument(task, "--persist-key", NULL);
241         connman_task_add_argument(task, "--persist-tun", NULL);
242
243         connman_task_add_argument(task, "--route-noexec", NULL);
244         connman_task_add_argument(task, "--ifconfig-noexec", NULL);
245
246         /*
247          * Disable client restarts because we can't handle this at the
248          * moment. The problem is that when OpenVPN decides to switch
249          * from CONNECTED state to RECONNECTING and then to RESOLVE,
250          * it is not possible to do a DNS lookup. The DNS server is
251          * not accessable through the tunnel anymore and so we end up
252          * trying to resolve the OpenVPN servers address.
253          */
254         connman_task_add_argument(task, "--ping-restart", "0");
255
256         connman_task_add_argument(task, "--client", NULL);
257
258         fd = fileno(stderr);
259         err = connman_task_run(task, vpn_died, provider,
260                         NULL, &fd, &fd);
261         if (err < 0) {
262                 connman_error("openvpn failed to start");
263                 return -EIO;
264         }
265
266         return 0;
267 }
268
269 static struct vpn_driver vpn_driver = {
270         .notify = ov_notify,
271         .connect        = ov_connect,
272 };
273
274 static int openvpn_init(void)
275 {
276         connection = connman_dbus_get_connection();
277
278         return vpn_register("openvpn", &vpn_driver, OPENVPN);
279 }
280
281 static void openvpn_exit(void)
282 {
283         vpn_unregister("openvpn");
284
285         dbus_connection_unref(connection);
286 }
287
288 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
289         CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)