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
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_") == FALSE)
90 options = g_strsplit(value, " ", 3);
91 if (options[0] != NULL &&
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_") == FALSE)
118 options = g_strsplit(value, " ", 3);
119 if (options[0] != NULL &&
120 !strcmp(options[0], "dhcp-option") &&
121 options[1] != NULL &&
122 !strcmp(options[1], "DOMAIN") &&
123 options[2] != NULL) {
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 vpn_provider_set_string(provider, "Gateway", value);
191 gateway = g_strdup(value);
194 if (!strcmp(key, "ifconfig_local")) {
195 vpn_provider_set_string(provider, "Address", value);
196 address = g_strdup(value);
199 if (!strcmp(key, "ifconfig_remote")) {
200 vpn_provider_set_string(provider, "Peer", value);
201 peer = g_strdup(value);
204 if (g_str_has_prefix(key, "route_") == TRUE)
205 vpn_provider_append_route(provider, key, value);
207 if ((ns_entry = ov_append_dns_entries(key, value)) != NULL)
208 nameserver_list = g_slist_prepend(nameserver_list,
211 char *domain = ov_get_domain_name(key, value);
212 if (domain != NULL) {
213 vpn_provider_set_domain(provider, domain);
218 dbus_message_iter_next(&dict);
221 ipaddress = connman_ipaddress_alloc(AF_INET);
222 if (ipaddress == NULL) {
223 g_slist_free_full(nameserver_list, free_ns_entry);
228 return VPN_STATE_FAILURE;
231 connman_ipaddress_set_ipv4(ipaddress, address, NULL, gateway);
232 connman_ipaddress_set_peer(ipaddress, peer);
233 vpn_provider_set_ipaddress(provider, ipaddress);
235 if (nameserver_list != NULL) {
236 char *nameservers = NULL;
239 nameserver_list = g_slist_sort(nameserver_list, cmp_ns);
240 for (tmp = nameserver_list; tmp != NULL;
241 tmp = g_slist_next(tmp)) {
242 struct nameserver_entry *ns = tmp->data;
244 if (nameservers == NULL) {
245 nameservers = g_strdup(ns->nameserver);
248 str = g_strjoin(" ", nameservers,
249 ns->nameserver, NULL);
255 g_slist_free_full(nameserver_list, free_ns_entry);
257 vpn_provider_set_nameservers(provider, nameservers);
265 connman_ipaddress_free(ipaddress);
267 return VPN_STATE_CONNECT;
270 static int ov_save(struct vpn_provider *provider, GKeyFile *keyfile)
275 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
276 if (strncmp(ov_options[i].cm_opt, "OpenVPN.", 8) == 0) {
277 option = vpn_provider_get_string(provider,
278 ov_options[i].cm_opt);
282 g_key_file_set_string(keyfile,
283 vpn_provider_get_save_group(provider),
284 ov_options[i].cm_opt, option);
290 static int task_append_config_data(struct vpn_provider *provider,
291 struct connman_task *task)
296 for (i = 0; i < (int)ARRAY_SIZE(ov_options); i++) {
297 if (ov_options[i].ov_opt == NULL)
300 option = vpn_provider_get_string(provider,
301 ov_options[i].cm_opt);
305 if (connman_task_add_argument(task,
306 ov_options[i].ov_opt,
307 ov_options[i].has_value ? option : NULL) < 0) {
315 static int ov_connect(struct vpn_provider *provider,
316 struct connman_task *task, const char *if_name,
317 vpn_provider_connect_cb_t cb, void *user_data)
322 option = vpn_provider_get_string(provider, "Host");
323 if (option == NULL) {
324 connman_error("Host not set; cannot enable VPN");
328 task_append_config_data(provider, task);
330 option = vpn_provider_get_string(provider, "OpenVPN.ConfigFile");
331 if (option == NULL) {
333 * Set some default options if user has no config file.
335 option = vpn_provider_get_string(provider, "OpenVPN.TLSAuth");
336 if (option != NULL) {
337 connman_task_add_argument(task, "--tls-auth", option);
338 option = vpn_provider_get_string(provider,
339 "OpenVPN.TLSAuthDir");
341 connman_task_add_argument(task, option, NULL);
344 connman_task_add_argument(task, "--nobind", NULL);
345 connman_task_add_argument(task, "--persist-key", NULL);
346 connman_task_add_argument(task, "--client", NULL);
349 connman_task_add_argument(task, "--syslog", NULL);
351 connman_task_add_argument(task, "--script-security", "2");
353 connman_task_add_argument(task, "--up",
354 SCRIPTDIR "/openvpn-script");
355 connman_task_add_argument(task, "--up-restart", NULL);
357 connman_task_add_argument(task, "--setenv", NULL);
358 connman_task_add_argument(task, "CONNMAN_BUSNAME",
359 dbus_bus_get_unique_name(connection));
361 connman_task_add_argument(task, "--setenv", NULL);
362 connman_task_add_argument(task, "CONNMAN_INTERFACE",
363 CONNMAN_TASK_INTERFACE);
365 connman_task_add_argument(task, "--setenv", NULL);
366 connman_task_add_argument(task, "CONNMAN_PATH",
367 connman_task_get_path(task));
369 connman_task_add_argument(task, "--dev", if_name);
370 connman_task_add_argument(task, "--dev-type", "tun");
372 connman_task_add_argument(task, "--persist-tun", NULL);
374 connman_task_add_argument(task, "--route-noexec", NULL);
375 connman_task_add_argument(task, "--ifconfig-noexec", NULL);
378 * Disable client restarts because we can't handle this at the
379 * moment. The problem is that when OpenVPN decides to switch
380 * from CONNECTED state to RECONNECTING and then to RESOLVE,
381 * it is not possible to do a DNS lookup. The DNS server is
382 * not accessable through the tunnel anymore and so we end up
383 * trying to resolve the OpenVPN servers address.
385 connman_task_add_argument(task, "--ping-restart", "0");
388 err = connman_task_run(task, vpn_died, provider,
391 connman_error("openvpn failed to start");
398 cb(provider, user_data, err);
403 static struct vpn_driver vpn_driver = {
405 .connect = ov_connect,
409 static int openvpn_init(void)
411 connection = connman_dbus_get_connection();
413 return vpn_register("openvpn", &vpn_driver, OPENVPN);
416 static void openvpn_exit(void)
418 vpn_unregister("openvpn");
420 dbus_connection_unref(connection);
423 CONNMAN_PLUGIN_DEFINE(openvpn, "OpenVPN plugin", VERSION,
424 CONNMAN_PLUGIN_PRIORITY_DEFAULT, openvpn_init, openvpn_exit)