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