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