5 * Copyright (C) 2010 BMW Car IT GmbH. All rights reserved.
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.
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.
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
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/log.h>
37 #include <connman/task.h>
38 #include <connman/dbus.h>
39 #include <connman/ipconfig.h>
41 #include "../vpn-provider.h"
45 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
47 static DBusConnection *connection;
54 { "Host", "--remote", 1 },
55 { "OpenVPN.CACert", "--ca", 1 },
56 { "OpenVPN.Cert", "--cert", 1 },
57 { "OpenVPN.Key", "--key", 1 },
58 { "OpenVPN.MTU", "--mtu", 1 },
59 { "OpenVPN.NSCertType", "--ns-cert-type", 1 },
60 { "OpenVPN.Proto", "--proto", 1 },
61 { "OpenVPN.Port", "--port", 1 },
62 { "OpenVPN.AuthUserPass", "--auth-user-pass", 1 },
63 { "OpenVPN.AskPass", "--askpass", 1 },
64 { "OpenVPN.AuthNoCache", "--auth-nocache", 0 },
65 { "OpenVPN.TLSRemote", "--tls-remote", 1 },
66 { "OpenVPN.TLSAuth", NULL, 1 },
67 { "OpenVPN.TLSAuthDir", NULL, 1 },
68 { "OpenVPN.Cipher", "--cipher", 1 },
69 { "OpenVPN.Auth", "--auth", 1 },
70 { "OpenVPN.CompLZO", "--comp-lzo", 0 },
71 { "OpenVPN.RemoteCertTls", "--remote-cert-tls", 1 },
74 static void ov_append_dns_entries(const char *key, const char *value,
79 if (g_str_has_prefix(key, "foreign_option_") == FALSE)
82 options = g_strsplit(value, " ", 3);
83 if (options[0] != NULL &&
84 !strcmp(options[0], "dhcp-option") &&
86 !strcmp(options[1], "DNS") &&
89 if (*dns_entries != NULL) {
92 tmp = g_strjoin(" ", *dns_entries,
97 *dns_entries = g_strdup(options[2]);
104 static int ov_notify(DBusMessage *msg, struct vpn_provider *provider)
106 DBusMessageIter iter, dict;
107 const char *reason, *key, *value;
108 char *nameservers = NULL;
109 char *address = NULL, *gateway = NULL, *peer = NULL;
110 struct connman_ipaddress *ipaddress;
112 dbus_message_iter_init(msg, &iter);
114 dbus_message_iter_get_basic(&iter, &reason);
115 dbus_message_iter_next(&iter);
118 connman_error("No provider found");
119 return VPN_STATE_FAILURE;
122 if (strcmp(reason, "up"))
123 return VPN_STATE_DISCONNECT;
125 dbus_message_iter_recurse(&iter, &dict);
127 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
128 DBusMessageIter entry;
130 dbus_message_iter_recurse(&dict, &entry);
131 dbus_message_iter_get_basic(&entry, &key);
132 dbus_message_iter_next(&entry);
133 dbus_message_iter_get_basic(&entry, &value);
135 DBG("%s = %s", key, value);
137 if (!strcmp(key, "trusted_ip")) {
138 vpn_provider_set_string(provider, "Gateway", value);
139 gateway = g_strdup(value);
142 if (!strcmp(key, "ifconfig_local")) {
143 vpn_provider_set_string(provider, "Address", value);
144 address = g_strdup(value);
147 if (!strcmp(key, "ifconfig_remote")) {
148 vpn_provider_set_string(provider, "Peer", value);
149 peer = g_strdup(value);
152 if (g_str_has_prefix(key, "route_") == TRUE)
153 vpn_provider_append_route(provider, key, value);
155 ov_append_dns_entries(key, value, &nameservers);
157 dbus_message_iter_next(&dict);
160 ipaddress = connman_ipaddress_alloc(AF_INET);
161 if (ipaddress == NULL) {
167 return VPN_STATE_FAILURE;
170 connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
171 connman_ipaddress_set_peer(ipaddress, peer);
172 vpn_provider_set_ipaddress(provider, ipaddress);
174 vpn_provider_set_nameservers(provider, nameservers);
180 connman_ipaddress_free(ipaddress);
182 return VPN_STATE_CONNECT;
185 static int ov_save(struct vpn_provider *provider, GKeyFile *keyfile)
190 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
191 if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
192 option = vpn_provider_get_string(provider,
193 ov_options[i].cm_opt);
197 g_key_file_set_string(keyfile,
198 vpn_provider_get_save_group(provider),
199 ov_options[i].cm_opt, option);
205 static int task_append_config_data(struct vpn_provider *provider,
206 struct connman_task *task)
211 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
212 if (ov_options[i].ov_opt == NULL)
215 option = vpn_provider_get_string(provider,
216 ov_options[i].cm_opt);
220 if (connman_task_add_argument(task,
221 ov_options[i].ov_opt,
222 ov_options[i].has_value ? option : NULL) < 0) {
230 static int ov_connect(struct vpn_provider *provider,
231 struct connman_task *task, const char *if_name)
236 option = vpn_provider_get_string(provider, "Host");
237 if (option == NULL) {
238 connman_error("Host not set; cannot enable VPN");
242 task_append_config_data(provider, task);
244 option = vpn_provider_get_string(provider, "OpenVPN.TLSAuth");
245 if (option != NULL) {
246 connman_task_add_argument(task, "--tls-auth", option);
247 option = vpn_provider_get_string(provider,
248 "OpenVPN.TLSAuthDir");
250 connman_task_add_argument(task, option, NULL);
253 connman_task_add_argument(task, "--syslog", NULL);
255 connman_task_add_argument(task, "--script-security", "2");
257 connman_task_add_argument(task, "--up",
258 SCRIPTDIR "/openvpn-script");
259 connman_task_add_argument(task, "--up-restart", NULL);
261 connman_task_add_argument(task, "--setenv", NULL);
262 connman_task_add_argument(task, "CONNMAN_BUSNAME",
263 dbus_bus_get_unique_name(connection));
265 connman_task_add_argument(task, "--setenv", NULL);
266 connman_task_add_argument(task, "CONNMAN_INTERFACE",
267 CONNMAN_TASK_INTERFACE);
269 connman_task_add_argument(task, "--setenv", NULL);
270 connman_task_add_argument(task, "CONNMAN_PATH",
271 connman_task_get_path(task));
273 connman_task_add_argument(task, "--dev", if_name);
274 connman_task_add_argument(task, "--dev-type", "tun");
276 connman_task_add_argument(task, "--tls-client", NULL);
277 connman_task_add_argument(task, "--nobind", NULL);
278 connman_task_add_argument(task, "--persist-key", NULL);
279 connman_task_add_argument(task, "--persist-tun", NULL);
281 connman_task_add_argument(task, "--route-noexec", NULL);
282 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
285 * Disable client restarts because we can't handle this at the
286 * moment. The problem is that when OpenVPN decides to switch
287 * from CONNECTED state to RECONNECTING and then to RESOLVE,
288 * it is not possible to do a DNS lookup. The DNS server is
289 * not accessable through the tunnel anymore and so we end up
290 * trying to resolve the OpenVPN servers address.
292 connman_task_add_argument(task, "--ping-restart", "0");
294 connman_task_add_argument(task, "--client", NULL);
297 err = connman_task_run(task, vpn_died, provider,
300 connman_error("openvpn failed to start");
307 static struct vpn_driver vpn_driver = {
309 .connect = ov_connect,
313 static int openvpn_init(void)
315 connection = connman_dbus_get_connection();
317 return vpn_register("openvpn", &vpn_driver, OPENVPN);
320 static void openvpn_exit(void)
322 vpn_unregister("openvpn");
324 dbus_connection_unref(connection);
327 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
328 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)