5 * Copyright (C) 2010,2013 BMW Car IT GmbH.
6 * Copyright (C) 2010,2012-2013 Intel Corporation. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * 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/ipconfig.h>
40 #include <connman/dbus.h>
42 #include "../vpn-provider.h"
46 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
48 static DBusConnection *connection;
58 const char *vpnc_default;
62 { "Host", "IPSec gateway", NULL, OPT_STRING, true },
63 { "VPNC.IPSec.ID", "IPSec ID", NULL, OPT_STRING, true },
64 { "VPNC.IPSec.Secret", "IPSec secret", NULL, OPT_STRING, false },
65 { "VPNC.Xauth.Username", "Xauth username", NULL, OPT_STRING, false },
66 { "VPNC.Xauth.Password", "Xauth password", NULL, OPT_STRING, false },
67 { "VPNC.IKE.Authmode", "IKE Authmode", NULL, OPT_STRING, true },
68 { "VPNC.IKE.DHGroup", "IKE DH Group", NULL, OPT_STRING, true },
69 { "VPNC.PFS", "Perfect Forward Secrecy", NULL, OPT_STRING, true },
70 { "VPNC.Domain", "Domain", NULL, OPT_STRING, true },
71 { "VPNC.Vendor", "Vendor", NULL, OPT_STRING, true },
72 { "VPNC.LocalPort", "Local Port", "0", OPT_STRING, true, },
73 { "VPNC.CiscoPort", "Cisco UDP Encapsulation Port", "0", OPT_STRING,
75 { "VPNC.AppVersion", "Application Version", NULL, OPT_STRING, true },
76 { "VPNC.NATTMode", "NAT Traversal Mode", "cisco-udp", OPT_STRING,
78 { "VPNC.DPDTimeout", "DPD idle timeout (our side)", NULL, OPT_STRING,
80 { "VPNC.SingleDES", "Enable Single DES", NULL, OPT_BOOLEAN, true },
81 { "VPNC.NoEncryption", "Enable no encryption", NULL, OPT_BOOLEAN,
85 static int vc_notify(DBusMessage *msg, struct vpn_provider *provider)
87 DBusMessageIter iter, dict;
88 char *address = NULL, *netmask = NULL, *gateway = NULL;
89 struct connman_ipaddress *ipaddress;
90 const char *reason, *key, *value;
92 dbus_message_iter_init(msg, &iter);
94 dbus_message_iter_get_basic(&iter, &reason);
95 dbus_message_iter_next(&iter);
98 connman_error("No provider found");
99 return VPN_STATE_FAILURE;
102 if (strcmp(reason, "connect"))
103 return VPN_STATE_DISCONNECT;
105 dbus_message_iter_recurse(&iter, &dict);
107 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
108 DBusMessageIter entry;
110 dbus_message_iter_recurse(&dict, &entry);
111 dbus_message_iter_get_basic(&entry, &key);
112 dbus_message_iter_next(&entry);
113 dbus_message_iter_get_basic(&entry, &value);
115 DBG("%s = %s", key, value);
117 if (!strcmp(key, "VPNGATEWAY"))
118 gateway = g_strdup(value);
120 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
121 address = g_strdup(value);
123 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
124 netmask = g_strdup(value);
126 if (!strcmp(key, "INTERNAL_IP4_DNS"))
127 vpn_provider_set_nameservers(provider, value);
129 if (!strcmp(key, "CISCO_DEF_DOMAIN"))
130 vpn_provider_set_domain(provider, value);
132 if (g_str_has_prefix(key, "CISCO_SPLIT_INC") ||
133 g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC"))
134 vpn_provider_append_route(provider, key, value);
136 dbus_message_iter_next(&dict);
140 ipaddress = connman_ipaddress_alloc(AF_INET);
146 return VPN_STATE_FAILURE;
149 connman_ipaddress_set_ipv4(ipaddress, address, netmask, gateway);
150 vpn_provider_set_ipaddress(provider, ipaddress);
155 connman_ipaddress_free(ipaddress);
157 return VPN_STATE_CONNECT;
160 static ssize_t full_write(int fd, const void *buf, size_t len)
165 byte_write = write(fd, buf, len);
166 if (byte_write < 0) {
167 connman_error("failed to write config to vpnc: %s\n",
178 static ssize_t write_option(int fd, const char *key, const char *value)
184 buf = g_strdup_printf("%s %s\n", key, value);
185 ret = full_write(fd, buf, strlen(buf));
193 static ssize_t write_bool_option(int fd, const char *key, const char *value)
199 if (strcasecmp(value, "yes") == 0 ||
200 strcasecmp(value, "true") == 0 ||
201 strcmp(value, "1") == 0) {
202 buf = g_strdup_printf("%s\n", key);
203 ret = full_write(fd, buf, strlen(buf));
212 static int vc_write_config_data(struct vpn_provider *provider, int fd)
217 for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
218 opt_s = vpn_provider_get_string(provider,
219 vpnc_options[i].cm_opt);
221 opt_s = vpnc_options[i].vpnc_default;
226 if (vpnc_options[i].type == OPT_STRING) {
228 vpnc_options[i].vpnc_opt, opt_s) < 0)
230 } else if (vpnc_options[i].type == OPT_BOOLEAN) {
231 if (write_bool_option(fd,
232 vpnc_options[i].vpnc_opt, opt_s) < 0)
241 static int vc_save(struct vpn_provider *provider, GKeyFile *keyfile)
246 for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
247 if (strncmp(vpnc_options[i].cm_opt, "VPNC.", 5) == 0) {
249 if (!vpnc_options[i].cm_save)
252 option = vpn_provider_get_string(provider,
253 vpnc_options[i].cm_opt);
257 g_key_file_set_string(keyfile,
258 vpn_provider_get_save_group(provider),
259 vpnc_options[i].cm_opt, option);
265 static int vc_connect(struct vpn_provider *provider,
266 struct connman_task *task, const char *if_name,
267 vpn_provider_connect_cb_t cb, const char *dbus_sender,
273 option = vpn_provider_get_string(provider, "Host");
275 connman_error("Host not set; cannot enable VPN");
279 option = vpn_provider_get_string(provider, "VPNC.IPSec.ID");
281 connman_error("Group not set; cannot enable VPN");
286 connman_task_add_argument(task, "--non-inter", NULL);
287 connman_task_add_argument(task, "--no-detach", NULL);
289 connman_task_add_argument(task, "--ifname", if_name);
290 connman_task_add_argument(task, "--ifmode", "tun");
292 connman_task_add_argument(task, "--script",
293 SCRIPTDIR "/openconnect-script");
295 option = vpn_provider_get_string(provider, "VPNC.Debug");
297 connman_task_add_argument(task, "--debug", option);
299 connman_task_add_argument(task, "-", NULL);
301 err = connman_task_run(task, vpn_died, provider,
304 connman_error("vpnc failed to start");
309 err = vc_write_config_data(provider, fd);
315 cb(provider, user_data, err);
320 static int vc_error_code(struct vpn_provider *provider, int exit_code)
324 return VPN_PROVIDER_ERROR_CONNECT_FAILED;
326 return VPN_PROVIDER_ERROR_LOGIN_FAILED;
328 return VPN_PROVIDER_ERROR_UNKNOWN;
332 static struct vpn_driver vpn_driver = {
334 .connect = vc_connect,
335 .error_code = vc_error_code,
339 static int vpnc_init(void)
341 connection = connman_dbus_get_connection();
343 return vpn_register("vpnc", &vpn_driver, VPNC);
346 static void vpnc_exit(void)
348 vpn_unregister("vpnc");
350 dbus_connection_unref(connection);
353 CONNMAN_PLUGIN_DEFINE(vpnc, "vpnc plugin", VERSION,
354 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpnc_init, vpnc_exit)