5 * Copyright (C) 2010-2014 BMW Car IT GmbH.
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
35 #define CONNMAN_API_SUBJECT_TO_CHANGE
36 #include <connman/plugin.h>
37 #include <connman/log.h>
38 #include <connman/task.h>
39 #include <connman/dbus.h>
40 #include <connman/ipconfig.h>
42 #include "../vpn-provider.h"
46 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
48 static DBusConnection *connection;
55 { "Host", "--remote", 1 },
56 { "OpenVPN.CACert", "--ca", 1 },
57 { "OpenVPN.Cert", "--cert", 1 },
58 { "OpenVPN.Key", "--key", 1 },
59 { "OpenVPN.MTU", "--mtu", 1 },
60 { "OpenVPN.NSCertType", "--ns-cert-type", 1 },
61 { "OpenVPN.Proto", "--proto", 1 },
62 { "OpenVPN.Port", "--port", 1 },
63 { "OpenVPN.AuthUserPass", "--auth-user-pass", 1 },
64 { "OpenVPN.AskPass", "--askpass", 1 },
65 { "OpenVPN.AuthNoCache", "--auth-nocache", 0 },
66 { "OpenVPN.TLSRemote", "--tls-remote", 1 },
67 { "OpenVPN.TLSAuth", NULL, 1 },
68 { "OpenVPN.TLSAuthDir", NULL, 1 },
69 { "OpenVPN.Cipher", "--cipher", 1 },
70 { "OpenVPN.Auth", "--auth", 1 },
71 { "OpenVPN.CompLZO", "--comp-lzo", 0 },
72 { "OpenVPN.RemoteCertTls", "--remote-cert-tls", 1 },
73 { "OpenVPN.ConfigFile", "--config", 1 },
76 struct nameserver_entry {
81 static struct nameserver_entry *ov_append_dns_entries(const char *key,
84 struct nameserver_entry *entry = NULL;
87 if (!g_str_has_prefix(key, "foreign_option_"))
90 options = g_strsplit(value, " ", 3);
92 !strcmp(options[0], "dhcp-option") &&
94 !strcmp(options[1], "DNS") &&
97 entry = g_try_new(struct nameserver_entry, 1);
101 entry->nameserver = g_strdup(options[2]);
102 entry->id = atoi(key + 15); /* foreign_option_XXX */
110 static char *ov_get_domain_name(const char *key, const char *value)
115 if (!g_str_has_prefix(key, "foreign_option_"))
118 options = g_strsplit(value, " ", 3);
120 !strcmp(options[0], "dhcp-option") &&
122 !strcmp(options[1], "DOMAIN") &&
125 domain = g_strdup(options[2]);
133 static gint cmp_ns(gconstpointer a, gconstpointer b)
135 struct nameserver_entry *entry_a = (struct nameserver_entry *)a;
136 struct nameserver_entry *entry_b = (struct nameserver_entry *)b;
138 if (entry_a->id < entry_b->id)
141 if (entry_a->id > entry_b->id)
147 static void free_ns_entry(gpointer data)
149 struct nameserver_entry *entry = data;
151 g_free(entry->nameserver);
155 static int ov_notify(DBusMessage *msg, struct vpn_provider *provider)
157 DBusMessageIter iter, dict;
158 const char *reason, *key, *value;
159 char *address = NULL, *gateway = NULL, *peer = NULL;
160 struct connman_ipaddress *ipaddress;
161 GSList *nameserver_list = NULL;
163 dbus_message_iter_init(msg, &iter);
165 dbus_message_iter_get_basic(&iter, &reason);
166 dbus_message_iter_next(&iter);
169 connman_error("No provider found");
170 return VPN_STATE_FAILURE;
173 if (strcmp(reason, "up"))
174 return VPN_STATE_DISCONNECT;
176 dbus_message_iter_recurse(&iter, &dict);
178 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
179 struct nameserver_entry *ns_entry = NULL;
180 DBusMessageIter entry;
182 dbus_message_iter_recurse(&dict, &entry);
183 dbus_message_iter_get_basic(&entry, &key);
184 dbus_message_iter_next(&entry);
185 dbus_message_iter_get_basic(&entry, &value);
187 DBG("%s = %s", key, value);
189 if (!strcmp(key, "trusted_ip"))
190 gateway = g_strdup(value);
192 if (!strcmp(key, "ifconfig_local"))
193 address = g_strdup(value);
195 if (!strcmp(key, "ifconfig_remote"))
196 peer = g_strdup(value);
198 if (g_str_has_prefix(key, "route_"))
199 vpn_provider_append_route(provider, key, value);
201 if ((ns_entry = ov_append_dns_entries(key, value)))
202 nameserver_list = g_slist_prepend(nameserver_list,
205 char *domain = ov_get_domain_name(key, value);
207 vpn_provider_set_domain(provider, domain);
212 dbus_message_iter_next(&dict);
215 ipaddress = connman_ipaddress_alloc(AF_INET);
217 g_slist_free_full(nameserver_list, free_ns_entry);
222 return VPN_STATE_FAILURE;
225 connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
226 connman_ipaddress_set_peer(ipaddress, peer);
227 vpn_provider_set_ipaddress(provider, ipaddress);
229 if (nameserver_list) {
230 char *nameservers = NULL;
233 nameserver_list = g_slist_sort(nameserver_list, cmp_ns);
234 for (tmp = nameserver_list; tmp;
235 tmp = g_slist_next(tmp)) {
236 struct nameserver_entry *ns = tmp->data;
239 nameservers = g_strdup(ns->nameserver);
242 str = g_strjoin(" ", nameservers,
243 ns->nameserver, NULL);
249 g_slist_free_full(nameserver_list, free_ns_entry);
251 vpn_provider_set_nameservers(provider, nameservers);
259 connman_ipaddress_free(ipaddress);
261 return VPN_STATE_CONNECT;
264 static int ov_save(struct vpn_provider *provider, GKeyFile *keyfile)
269 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
270 if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
271 option = vpn_provider_get_string(provider,
272 ov_options[i].cm_opt);
276 g_key_file_set_string(keyfile,
277 vpn_provider_get_save_group(provider),
278 ov_options[i].cm_opt, option);
284 static int task_append_config_data(struct vpn_provider *provider,
285 struct connman_task *task)
290 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
291 if (!ov_options[i].ov_opt)
294 option = vpn_provider_get_string(provider,
295 ov_options[i].cm_opt);
299 if (connman_task_add_argument(task,
300 ov_options[i].ov_opt,
301 ov_options[i].has_value ? option : NULL) < 0) {
309 static int ov_connect(struct vpn_provider *provider,
310 struct connman_task *task, const char *if_name,
311 vpn_provider_connect_cb_t cb, const char *dbus_sender,
317 option = vpn_provider_get_string(provider, "Host");
319 connman_error("Host not set; cannot enable VPN");
323 task_append_config_data(provider, task);
325 option = vpn_provider_get_string(provider, "OpenVPN.ConfigFile");
328 * Set some default options if user has no config file.
330 option = vpn_provider_get_string(provider, "OpenVPN.TLSAuth");
332 connman_task_add_argument(task, "--tls-auth", option);
333 option = vpn_provider_get_string(provider,
334 "OpenVPN.TLSAuthDir");
336 connman_task_add_argument(task, option, NULL);
339 connman_task_add_argument(task, "--nobind", NULL);
340 connman_task_add_argument(task, "--persist-key", NULL);
341 connman_task_add_argument(task, "--client", NULL);
344 connman_task_add_argument(task, "--syslog", NULL);
346 connman_task_add_argument(task, "--script-security", "2");
348 connman_task_add_argument(task, "--up",
349 SCRIPTDIR "/openvpn-script");
350 connman_task_add_argument(task, "--up-restart", NULL);
352 connman_task_add_argument(task, "--setenv", NULL);
353 connman_task_add_argument(task, "CONNMAN_BUSNAME",
354 dbus_bus_get_unique_name(connection));
356 connman_task_add_argument(task, "--setenv", NULL);
357 connman_task_add_argument(task, "CONNMAN_INTERFACE",
358 CONNMAN_TASK_INTERFACE);
360 connman_task_add_argument(task, "--setenv", NULL);
361 connman_task_add_argument(task, "CONNMAN_PATH",
362 connman_task_get_path(task));
364 connman_task_add_argument(task, "--dev", if_name);
365 connman_task_add_argument(task, "--dev-type", "tun");
367 connman_task_add_argument(task, "--persist-tun", NULL);
369 #if !defined TIZEN_EXT
370 connman_task_add_argument(task, "--route-noexec", NULL);
371 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
375 * Disable client restarts because we can't handle this at the
376 * moment. The problem is that when OpenVPN decides to switch
377 * from CONNECTED state to RECONNECTING and then to RESOLVE,
378 * it is not possible to do a DNS lookup. The DNS server is
379 * not accessable through the tunnel anymore and so we end up
380 * trying to resolve the OpenVPN servers address.
382 connman_task_add_argument(task, "--ping-restart", "0");
385 err = connman_task_run(task, vpn_died, provider,
388 connman_error("openvpn failed to start");
395 cb(provider, user_data, err);
400 static struct vpn_driver vpn_driver = {
402 .connect = ov_connect,
406 static int openvpn_init(void)
408 connection = connman_dbus_get_connection();
410 return vpn_register("openvpn", &vpn_driver, OPENVPN);
413 static void openvpn_exit(void)
415 vpn_unregister("openvpn");
417 dbus_connection_unref(connection);
420 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
421 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)