410cfbd78d87553b4c6326c477b33bc20293898b
[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
41 #include "vpn.h"
42
43 static DBusConnection *connection;
44
45 struct ov_route {
46         int family;
47         char *host;
48         char *netmask;
49         char *gateway;
50 };
51
52 static void destroy_route(gpointer user_data)
53 {
54         struct ov_route *route = user_data;
55
56         g_free(route->host);
57         g_free(route->netmask);
58         g_free(route->gateway);
59         g_free(route);
60 }
61
62 static void ov_provider_append_routes(gpointer key, gpointer value,
63                                         gpointer user_data)
64 {
65         struct ov_route *route = value;
66         struct connman_provider *provider = user_data;
67
68         connman_provider_append_route(provider, route->family, route->host,
69                                         route->netmask, route->gateway);
70 }
71
72 static struct ov_route *ov_route_lookup(const char *key, const char *prefix_key,
73                                         GHashTable *routes)
74 {
75         unsigned long idx;
76         const char *start;
77         char *end;
78         struct ov_route *route;
79
80         if (g_str_has_prefix(key, prefix_key) == FALSE)
81                 return NULL;
82
83         start = key + strlen(prefix_key);
84         idx = g_ascii_strtoull(start, &end, 10);
85
86         if (idx == 0 && start == end) {
87                 connman_error("string conversion failed %s", start);
88                 return NULL;
89         }
90
91         route = g_hash_table_lookup(routes, GINT_TO_POINTER(idx));
92         if (route == NULL) {
93                 route = g_try_new0(struct ov_route, 1);
94                 if (route == NULL) {
95                         connman_error("out of memory");
96                         return NULL;
97                 }
98
99                 route->family = AF_INET;
100
101                 g_hash_table_replace(routes, GINT_TO_POINTER(idx),
102                                                 route);
103         }
104
105         return  route;
106 }
107
108 static void ov_append_route(const char *key, const char *value, GHashTable *routes)
109 {
110         struct ov_route *route;
111
112         /*
113          * OpenVPN pushes routing tupples (host, nw, gw) as several
114          * environment values, e.g.
115          *
116          * route_gateway_2 = 10.242.2.13
117          * route_netmask_2 = 255.255.0.0
118          * route_network_2 = 192.168.0.0
119          * route_gateway_1 = 10.242.2.13
120          * route_netmask_1 = 255.255.255.255
121          * route_network_1 = 10.242.2.1
122          *
123          * The hash table is used to group the separate environment
124          * variables together. It also makes sure all tupples are
125          * complete even when OpenVPN pushes the information in a
126          * wrong order (unlikely).
127          */
128
129         route = ov_route_lookup(key, "route_network_", routes);
130         if (route != NULL) {
131                 route->host = g_strdup(value);
132                 return;
133         }
134
135         route = ov_route_lookup(key, "route_netmask_", routes);
136         if (route != NULL) {
137                 route->netmask = g_strdup(value);
138                 return;
139         }
140
141         route = ov_route_lookup(key, "route_gateway_", routes);
142         if (route != NULL)
143                 route->gateway = g_strdup(value);
144 }
145
146 static void ov_append_dns_entries(const char *key, const char *value,
147                                         char **dns_entries)
148 {
149         gchar **options;
150
151         if (g_str_has_prefix(key, "foreign_option_") == FALSE)
152                 return;
153
154         options = g_strsplit(value, " ", 3);
155         if (options[0] != NULL &&
156                 !strcmp(options[0], "dhcp-option") &&
157                         options[1] != NULL &&
158                         !strcmp(options[1], "DNS") &&
159                                 options[2] != NULL) {
160
161                 if (*dns_entries != NULL) {
162                         char *tmp;
163
164                         tmp = g_strjoin(" ", *dns_entries,
165                                                 options[2], NULL);
166                         g_free(*dns_entries);
167                         *dns_entries = tmp;
168                 } else {
169                         *dns_entries = g_strdup(options[2]);
170                 }
171         }
172
173         g_strfreev(options);
174 }
175
176 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
177 {
178         DBusMessageIter iter, dict;
179         const char *reason, *key, *value;
180         const char *domain = NULL;
181         char *dns_entries = NULL;
182         GHashTable *routes;
183
184         dbus_message_iter_init(msg, &iter);
185
186         dbus_message_iter_get_basic(&iter, &reason);
187         dbus_message_iter_next(&iter);
188
189         if (!provider) {
190                 connman_error("No provider found");
191                 return VPN_STATE_FAILURE;
192         }
193
194         if (strcmp(reason, "up"))
195                 return VPN_STATE_DISCONNECT;
196
197         domain = connman_provider_get_string(provider, "VPN.Domain");
198
199         dbus_message_iter_recurse(&iter, &dict);
200
201         routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
202                                         NULL, destroy_route);
203
204         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
205                 DBusMessageIter entry;
206
207                 dbus_message_iter_recurse(&dict, &entry);
208                 dbus_message_iter_get_basic(&entry, &key);
209                 dbus_message_iter_next(&entry);
210                 dbus_message_iter_get_basic(&entry, &value);
211
212                 DBG("%s = %s", key, value);
213
214                 if (!strcmp(key, "trusted_ip"))
215                         connman_provider_set_string(provider, "Gateway", value);
216
217                 if (!strcmp(key, "ifconfig_local"))
218                         connman_provider_set_string(provider, "Address", value);
219
220                 if (!strcmp(key, "ifconfig_remote"))
221                         connman_provider_set_string(provider, "Peer", value);
222
223                 ov_append_route(key, value, routes);
224
225                 ov_append_dns_entries(key, value, &dns_entries);
226
227                 dbus_message_iter_next(&dict);
228         }
229
230         if (dns_entries != NULL) {
231                 connman_provider_set_string(provider, "DNS", dns_entries);
232                 g_free(dns_entries);
233         }
234
235         g_hash_table_foreach(routes, ov_provider_append_routes, provider);
236
237         g_hash_table_destroy(routes);
238
239         return VPN_STATE_CONNECT;
240 }
241
242 static int ov_connect(struct connman_provider *provider,
243                 struct connman_task *task, const char *if_name)
244 {
245         const char *vpnhost, *cafile, *mtu, *certfile, *keyfile;
246         const char *proto, *port, *auth_user_pass;
247         const char *tls_remote, *cipher, *auth, *comp_lzo;
248         int err, fd;
249
250         vpnhost = connman_provider_get_string(provider, "Host");
251         if (!vpnhost) {
252                 connman_error("Host not set; cannot enable VPN");
253                 return -EINVAL;
254         }
255
256         cafile = connman_provider_get_string(provider, "OpenVPN.CACert");
257         certfile = connman_provider_get_string(provider, "OpenVPN.Cert");
258         keyfile = connman_provider_get_string(provider, "OpenVPN.Key");
259         mtu = connman_provider_get_string(provider, "VPN.MTU");
260         proto = connman_provider_get_string(provider, "OpenVPN.Proto");
261         port = connman_provider_get_string(provider, "OpenVPN.Port");
262         auth_user_pass = connman_provider_get_string(provider,
263                                                         "OpenVPN.AuthUserPass");
264         tls_remote = connman_provider_get_string(provider, "OpenVPN.TLSRemote");
265         cipher = connman_provider_get_string(provider, "OpenVPN.Cipher");
266         auth = connman_provider_get_string(provider, "OpenVPN.Auth");
267         comp_lzo = connman_provider_get_string(provider, "OpenVPN.CompLZO");
268
269         if (mtu != NULL)
270                 connman_task_add_argument(task, "--mtu", (char *)mtu);
271
272         if (proto != NULL)
273                 connman_task_add_argument(task, "--proto", (char *)proto);
274
275         if (port != NULL)
276                 connman_task_add_argument(task, "--port", (char *)port);
277
278         if (auth_user_pass != NULL) {
279                 connman_task_add_argument(task, "--auth-user-pass",
280                                                 (char *)auth_user_pass);
281         }
282
283         if (tls_remote != NULL) {
284                 connman_task_add_argument(task, "--tls-remote",
285                                                 (char *)tls_remote);
286         }
287
288         if (cipher != NULL)
289                 connman_task_add_argument(task, "--cipher", (char *)cipher);
290
291         if (auth != NULL)
292                 connman_task_add_argument(task, "--auth", (char *)auth);
293
294         if (comp_lzo)
295                 connman_task_add_argument(task, "--comp-lzo", (char *)comp_lzo);
296
297         connman_task_add_argument(task, "--syslog", NULL);
298
299         connman_task_add_argument(task, "--script-security", "2");
300
301         connman_task_add_argument(task, "--up",
302                                         SCRIPTDIR "/openvpn-script");
303         connman_task_add_argument(task, "--up-restart", NULL);
304
305         connman_task_add_argument(task, "--setenv", NULL);
306         connman_task_add_argument(task, "CONNMAN_BUSNAME",
307                                         dbus_bus_get_unique_name(connection));
308
309         connman_task_add_argument(task, "--setenv", NULL);
310         connman_task_add_argument(task, "CONNMAN_INTERFACE",
311                                         CONNMAN_TASK_INTERFACE);
312
313         connman_task_add_argument(task, "--setenv", NULL);
314         connman_task_add_argument(task, "CONNMAN_PATH",
315                                         connman_task_get_path(task));
316
317         connman_task_add_argument(task, "--dev", if_name);
318         connman_task_add_argument(task, "--dev-type", "tun");
319
320         connman_task_add_argument(task, "--tls-client", NULL);
321         connman_task_add_argument(task, "--remote", (char *)vpnhost);
322         connman_task_add_argument(task, "--nobind", NULL);
323         connman_task_add_argument(task, "--persist-key", NULL);
324         connman_task_add_argument(task, "--persist-tun", NULL);
325
326         connman_task_add_argument(task, "--route-noexec", NULL);
327         connman_task_add_argument(task, "--ifconfig-noexec", NULL);
328
329         /*
330          * Disable client restarts because we can't handle this at the
331          * moment. The problem is that when OpenVPN decides to switch
332          * from CONNECTED state to RECONNECTING and then to RESOLVE,
333          * it is not possible to do a DNS lookup. The DNS server is
334          * not accessable through the tunnel anymore and so we end up
335          * trying to resolve the OpenVPN servers address.
336          */
337         connman_task_add_argument(task, "--ping-restart", "0");
338
339         connman_task_add_argument(task, "--client", NULL);
340
341         if (cafile) {
342                 connman_task_add_argument(task, "--ca",
343                                                 (char *)cafile);
344         }
345
346         if (certfile) {
347                 connman_task_add_argument(task, "--cert",
348                                                 (char *)certfile);
349         }
350
351         if (keyfile) {
352                 connman_task_add_argument(task, "--key",
353                                                 (char *)keyfile);
354         }
355
356         fd = fileno(stderr);
357         err = connman_task_run(task, vpn_died, provider,
358                         NULL, &fd, &fd);
359         if (err < 0) {
360                 connman_error("openvpn failed to start");
361                 return -EIO;
362         }
363
364         return 0;
365 }
366
367 static struct vpn_driver vpn_driver = {
368         .notify = ov_notify,
369         .connect        = ov_connect,
370 };
371
372 static int openvpn_init(void)
373 {
374         connection = connman_dbus_get_connection();
375
376         return vpn_register("openvpn", &vpn_driver, OPENVPN);
377 }
378
379 static void openvpn_exit(void)
380 {
381         vpn_unregister("openvpn");
382
383         dbus_connection_unref(connection);
384 }
385
386 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
387         CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)