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/provider.h>
37 #include <connman/log.h>
38 #include <connman/task.h>
39 #include <connman/ipconfig.h>
40 #include <connman/dbus.h>
44 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
46 static DBusConnection *connection;
56 const char *vpnc_default;
59 { "Host", "IPSec gateway", NULL, OPT_STRING },
60 { "VPNC.IPSec.ID", "IPSec ID", NULL, OPT_STRING },
61 { "VPNC.IPSec.Secret", "IPSec secret", NULL, OPT_STRING },
62 { "VPNC.Xauth.Username", "Xauth username", NULL, OPT_STRING },
63 { "VPNC.Xauth.Password", "Xauth password", NULL, OPT_STRING },
64 { "VPNC.IKE.Authmode", "IKE Authmode", NULL, OPT_STRING },
65 { "VPNC.IKE.DHGroup", "IKE DH Group", NULL, OPT_STRING },
66 { "VPNC.PFS", "Perfect Forward Secrecy", NULL, OPT_STRING },
67 { "VPNC.Domain", "Domain", NULL, OPT_STRING },
68 { "VPNC.Vendor", "Vendor", NULL, OPT_STRING },
69 { "VPNC.LocalPort", "Local Port", "0", OPT_STRING },
70 { "VPNC.CiscoPort","Cisco UDP Encapsulation Port", "0", OPT_STRING },
71 { "VPNC.AppVersion", "Application Version", NULL, OPT_STRING },
72 { "VPNC.NATTMode", "NAT Traversal Mode", "cisco-udp", OPT_STRING },
73 { "VPNC.DPDTimeout", "DPD idle timeout (our side)", NULL, OPT_STRING },
74 { "VPNC.SingleDES", "Enable Single DES", NULL, OPT_BOOLEAN },
75 { "VPNC.NoEncryption", "Enable no encryption", NULL, OPT_BOOLEAN },
78 static int vc_notify(DBusMessage *msg, struct connman_provider *provider)
80 DBusMessageIter iter, dict;
81 char *address = NULL, *netmask = NULL, *gateway = NULL;
82 struct connman_ipaddress *ipaddress;
83 const char *reason, *key, *value;
85 dbus_message_iter_init(msg, &iter);
87 dbus_message_iter_get_basic(&iter, &reason);
88 dbus_message_iter_next(&iter);
91 connman_error("No provider found");
92 return VPN_STATE_FAILURE;
95 if (strcmp(reason, "connect"))
96 return VPN_STATE_DISCONNECT;
98 dbus_message_iter_recurse(&iter, &dict);
100 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
101 DBusMessageIter entry;
103 dbus_message_iter_recurse(&dict, &entry);
104 dbus_message_iter_get_basic(&entry, &key);
105 dbus_message_iter_next(&entry);
106 dbus_message_iter_get_basic(&entry, &value);
108 DBG("%s = %s", key, value);
110 if (!strcmp(key, "VPNGATEWAY"))
111 gateway = g_strdup(value);
113 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
114 address = g_strdup(value);
116 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
117 netmask = g_strdup(value);
119 if (!strcmp(key, "INTERNAL_IP4_DNS"))
120 connman_provider_set_nameservers(provider, value);
122 if (!strcmp(key, "CISCO_DEF_DOMAIN"))
123 connman_provider_set_domain(provider, value);
125 if (g_str_has_prefix(key, "CISCO_SPLIT_INC") == TRUE ||
126 g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC") == TRUE)
127 connman_provider_append_route(provider, key, value);
129 dbus_message_iter_next(&dict);
133 ipaddress = connman_ipaddress_alloc(AF_INET);
134 if (ipaddress == NULL) {
139 return VPN_STATE_FAILURE;
142 connman_ipaddress_set_ipv4(ipaddress, address, netmask, gateway);
143 connman_provider_set_ipaddress(provider, ipaddress);
148 connman_ipaddress_free(ipaddress);
150 return VPN_STATE_CONNECT;
153 static ssize_t full_write(int fd, const void *buf, size_t len)
158 byte_write = write(fd, buf, len);
159 if (byte_write < 0) {
160 connman_error("failed to write config to vpnc: %s\n",
171 static ssize_t write_option(int fd, const char *key, const char *value)
176 if (key != NULL && value != NULL) {
177 buf = g_strdup_printf("%s %s\n", key, value);
178 ret = full_write(fd, buf, strlen(buf));
186 static ssize_t write_bool_option(int fd, const char *key, const char *value)
191 if (key != NULL && value != NULL) {
192 if (strcmp(value, "yes") == 0) {
193 buf = g_strdup_printf("%s\n", key);
194 ret = full_write(fd, buf, strlen(buf));
203 static int vc_write_config_data(struct connman_provider *provider, int fd)
208 for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
209 opt_s = connman_provider_get_string(provider,
210 vpnc_options[i].cm_opt);
212 opt_s= vpnc_options[i].vpnc_default;
217 if (vpnc_options[i].type == OPT_STRING) {
219 vpnc_options[i].vpnc_opt, opt_s) < 0)
221 } else if (vpnc_options[i].type == OPT_BOOLEAN) {
222 if (write_bool_option(fd,
223 vpnc_options[i].vpnc_opt, opt_s) < 0)
232 static int vc_save(struct connman_provider *provider, GKeyFile *keyfile)
237 for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
238 if (strncmp(vpnc_options[i].cm_opt, "VPNC.", 5) == 0) {
239 option = connman_provider_get_string(provider,
240 vpnc_options[i].cm_opt);
244 g_key_file_set_string(keyfile,
245 connman_provider_get_save_group(provider),
246 vpnc_options[i].cm_opt, option);
252 static int vc_connect(struct connman_provider *provider,
253 struct connman_task *task, const char *if_name)
258 option = connman_provider_get_string(provider, "Host");
259 if (option == NULL) {
260 connman_error("Host not set; cannot enable VPN");
263 option = connman_provider_get_string(provider, "VPNC.IPSec.ID");
264 if (option == NULL) {
265 connman_error("Group not set; cannot enable VPN");
269 connman_task_add_argument(task, "--non-inter", NULL);
270 connman_task_add_argument(task, "--no-detach", NULL);
272 connman_task_add_argument(task, "--ifname", if_name);
273 connman_task_add_argument(task, "--ifmode", "tun");
275 connman_task_add_argument(task, "--script",
276 SCRIPTDIR "/openconnect-script");
278 option = connman_provider_get_string(provider, "VPNC.Debug");
280 connman_task_add_argument(task, "--debug", option);
282 connman_task_add_argument(task, "-", NULL);
284 err = connman_task_run(task, vpn_died, provider,
287 connman_error("vpnc failed to start");
291 err = vc_write_config_data(provider, fd);
298 static int vc_error_code(int exit_code)
302 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
304 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
306 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
310 static struct vpn_driver vpn_driver = {
312 .connect = vc_connect,
313 .error_code = vc_error_code,
317 static int vpnc_init(void)
319 connection = connman_dbus_get_connection();
321 return vpn_register("vpnc", &vpn_driver, VPNC);
324 static void vpnc_exit(void)
326 vpn_unregister("vpnc");
328 dbus_connection_unref(connection);
331 CONNMAN_PLUGIN_DEFINE(vpnc, "vpnc plugin", VERSION,
332 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpnc_init, vpnc_exit)