service: Indicate IP Configuration Ready Event
[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         { "OpenVPN.RemoteCertTls", "--remote-cert-tls" },
65 };
66
67 static void ov_append_dns_entries(const char *key, const char *value,
68                                         char **dns_entries)
69 {
70         gchar **options;
71
72         if (g_str_has_prefix(key, "foreign_option_") == FALSE)
73                 return;
74
75         options = g_strsplit(value, " ", 3);
76         if (options[0] != NULL &&
77                 !strcmp(options[0], "dhcp-option") &&
78                         options[1] != NULL &&
79                         !strcmp(options[1], "DNS") &&
80                                 options[2] != NULL) {
81
82                 if (*dns_entries != NULL) {
83                         char *tmp;
84
85                         tmp = g_strjoin(" ", *dns_entries,
86                                                 options[2], NULL);
87                         g_free(*dns_entries);
88                         *dns_entries = tmp;
89                 } else {
90                         *dns_entries = g_strdup(options[2]);
91                 }
92         }
93
94         g_strfreev(options);
95 }
96
97 static int ov_notify(DBusMessage *msg, struct connman_provider *provider)
98 {
99         DBusMessageIter iter, dict;
100         const char *reason, *key, *value;
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         dbus_message_iter_recurse(&iter, &dict);
119
120         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
121                 DBusMessageIter entry;
122
123                 dbus_message_iter_recurse(&dict, &entry);
124                 dbus_message_iter_get_basic(&entry, &key);
125                 dbus_message_iter_next(&entry);
126                 dbus_message_iter_get_basic(&entry, &value);
127
128                 DBG("%s = %s", key, value);
129
130                 if (!strcmp(key, "trusted_ip")) {
131                         connman_provider_set_string(provider, "Gateway", value);
132                         gateway = g_strdup(value);
133                 }
134
135                 if (!strcmp(key, "ifconfig_local")) {
136                         connman_provider_set_string(provider, "Address", value);
137                         address = g_strdup(value);
138                 }
139
140                 if (!strcmp(key, "ifconfig_remote")) {
141                         connman_provider_set_string(provider, "Peer", value);
142                         peer = g_strdup(value);
143                 }
144
145                 if (g_str_has_prefix(key, "route_") == TRUE)
146                         connman_provider_append_route(provider, key, value);
147
148                 ov_append_dns_entries(key, value, &nameservers);
149
150                 dbus_message_iter_next(&dict);
151         }
152
153         ipaddress = connman_ipaddress_alloc(AF_INET);
154         if (ipaddress == NULL) {
155                 g_free(nameservers);
156                 g_free(address);
157                 g_free(gateway);
158                 g_free(peer);
159
160                 return VPN_STATE_FAILURE;
161         }
162
163         connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
164         connman_ipaddress_set_peer(ipaddress, peer);
165         connman_provider_set_ipaddress(provider, ipaddress);
166
167         connman_provider_set_nameservers(provider, nameservers);
168
169         g_free(nameservers);
170         g_free(address);
171         g_free(gateway);
172         g_free(peer);
173         connman_ipaddress_free(ipaddress);
174
175         return VPN_STATE_CONNECT;
176 }
177
178 static int task_append_config_data(struct connman_provider *provider,
179                                         struct connman_task *task)
180 {
181         const char *option;
182         int i;
183
184         for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
185                 option = connman_provider_get_string(provider,
186                                         ov_options[i].cm_opt);
187                 if (option == NULL)
188                         continue;
189
190                 if (connman_task_add_argument(task,
191                                         ov_options[i].ov_opt, option) < 0) {
192                         return -EIO;
193                 }
194         }
195
196         return 0;
197 }
198
199 static int ov_connect(struct connman_provider *provider,
200                 struct connman_task *task, const char *if_name)
201 {
202         const char *option;
203         int err, fd;
204
205         option = connman_provider_get_string(provider, "Host");
206         if (option == NULL) {
207                 connman_error("Host not set; cannot enable VPN");
208                 return -EINVAL;
209         }
210
211         task_append_config_data(provider, task);
212
213         connman_task_add_argument(task, "--syslog", NULL);
214
215         connman_task_add_argument(task, "--script-security", "2");
216
217         connman_task_add_argument(task, "--up",
218                                         SCRIPTDIR "/openvpn-script");
219         connman_task_add_argument(task, "--up-restart", NULL);
220
221         connman_task_add_argument(task, "--setenv", NULL);
222         connman_task_add_argument(task, "CONNMAN_BUSNAME",
223                                         dbus_bus_get_unique_name(connection));
224
225         connman_task_add_argument(task, "--setenv", NULL);
226         connman_task_add_argument(task, "CONNMAN_INTERFACE",
227                                         CONNMAN_TASK_INTERFACE);
228
229         connman_task_add_argument(task, "--setenv", NULL);
230         connman_task_add_argument(task, "CONNMAN_PATH",
231                                         connman_task_get_path(task));
232
233         connman_task_add_argument(task, "--dev", if_name);
234         connman_task_add_argument(task, "--dev-type", "tun");
235
236         connman_task_add_argument(task, "--tls-client", NULL);
237         connman_task_add_argument(task, "--nobind", NULL);
238         connman_task_add_argument(task, "--persist-key", NULL);
239         connman_task_add_argument(task, "--persist-tun", NULL);
240
241         connman_task_add_argument(task, "--route-noexec", NULL);
242         connman_task_add_argument(task, "--ifconfig-noexec", NULL);
243
244         /*
245          * Disable client restarts because we can't handle this at the
246          * moment. The problem is that when OpenVPN decides to switch
247          * from CONNECTED state to RECONNECTING and then to RESOLVE,
248          * it is not possible to do a DNS lookup. The DNS server is
249          * not accessable through the tunnel anymore and so we end up
250          * trying to resolve the OpenVPN servers address.
251          */
252         connman_task_add_argument(task, "--ping-restart", "0");
253
254         connman_task_add_argument(task, "--client", NULL);
255
256         fd = fileno(stderr);
257         err = connman_task_run(task, vpn_died, provider,
258                         NULL, &fd, &fd);
259         if (err < 0) {
260                 connman_error("openvpn failed to start");
261                 return -EIO;
262         }
263
264         return 0;
265 }
266
267 static struct vpn_driver vpn_driver = {
268         .notify = ov_notify,
269         .connect        = ov_connect,
270 };
271
272 static int openvpn_init(void)
273 {
274         connection = connman_dbus_get_connection();
275
276         return vpn_register("openvpn", &vpn_driver, OPENVPN);
277 }
278
279 static void openvpn_exit(void)
280 {
281         vpn_unregister("openvpn");
282
283         dbus_connection_unref(connection);
284 }
285
286 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
287         CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)