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;
58 connman_bool_t cm_save;
60 { "Host", "IPSec gateway", NULL, OPT_STRING, TRUE },
61 { "VPNC.IPSec.ID", "IPSec ID", NULL, OPT_STRING, TRUE },
62 { "VPNC.IPSec.Secret", "IPSec secret", NULL, OPT_STRING, FALSE },
63 { "VPNC.Xauth.Username", "Xauth username", NULL, OPT_STRING, FALSE },
64 { "VPNC.Xauth.Password", "Xauth password", NULL, OPT_STRING, FALSE },
65 { "VPNC.IKE.Authmode", "IKE Authmode", NULL, OPT_STRING, TRUE },
66 { "VPNC.IKE.DHGroup", "IKE DH Group", NULL, OPT_STRING, TRUE },
67 { "VPNC.PFS", "Perfect Forward Secrecy", NULL, OPT_STRING, TRUE },
68 { "VPNC.Domain", "Domain", NULL, OPT_STRING, TRUE },
69 { "VPNC.Vendor", "Vendor", NULL, OPT_STRING, TRUE },
70 { "VPNC.LocalPort", "Local Port", "0", OPT_STRING, TRUE, },
71 { "VPNC.CiscoPort","Cisco UDP Encapsulation Port", "0", OPT_STRING,
73 { "VPNC.AppVersion", "Application Version", NULL, OPT_STRING, TRUE },
74 { "VPNC.NATTMode", "NAT Traversal Mode", "cisco-udp", OPT_STRING,
76 { "VPNC.DPDTimeout", "DPD idle timeout (our side)", NULL, OPT_STRING,
78 { "VPNC.SingleDES", "Enable Single DES", NULL, OPT_BOOLEAN, TRUE },
79 { "VPNC.NoEncryption", "Enable no encryption", NULL, OPT_BOOLEAN,
83 static int vc_notify(DBusMessage *msg, struct connman_provider *provider)
85 DBusMessageIter iter, dict;
86 char *address = NULL, *netmask = NULL, *gateway = NULL;
87 struct connman_ipaddress *ipaddress;
88 const char *reason, *key, *value;
90 dbus_message_iter_init(msg, &iter);
92 dbus_message_iter_get_basic(&iter, &reason);
93 dbus_message_iter_next(&iter);
96 connman_error("No provider found");
97 return VPN_STATE_FAILURE;
100 if (strcmp(reason, "connect"))
101 return VPN_STATE_DISCONNECT;
103 dbus_message_iter_recurse(&iter, &dict);
105 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
106 DBusMessageIter entry;
108 dbus_message_iter_recurse(&dict, &entry);
109 dbus_message_iter_get_basic(&entry, &key);
110 dbus_message_iter_next(&entry);
111 dbus_message_iter_get_basic(&entry, &value);
113 DBG("%s = %s", key, value);
115 if (!strcmp(key, "VPNGATEWAY"))
116 gateway = g_strdup(value);
118 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
119 address = g_strdup(value);
121 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
122 netmask = g_strdup(value);
124 if (!strcmp(key, "INTERNAL_IP4_DNS"))
125 connman_provider_set_nameservers(provider, value);
127 if (!strcmp(key, "CISCO_DEF_DOMAIN"))
128 connman_provider_set_domain(provider, value);
130 if (g_str_has_prefix(key, "CISCO_SPLIT_INC") == TRUE ||
131 g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC") == TRUE)
132 connman_provider_append_route(provider, key, value);
134 dbus_message_iter_next(&dict);
138 ipaddress = connman_ipaddress_alloc(AF_INET);
139 if (ipaddress == NULL) {
144 return VPN_STATE_FAILURE;
147 connman_ipaddress_set_ipv4(ipaddress, address, netmask, gateway);
148 connman_provider_set_ipaddress(provider, ipaddress);
153 connman_ipaddress_free(ipaddress);
155 return VPN_STATE_CONNECT;
158 static ssize_t full_write(int fd, const void *buf, size_t len)
163 byte_write = write(fd, buf, len);
164 if (byte_write < 0) {
165 connman_error("failed to write config to vpnc: %s\n",
176 static ssize_t write_option(int fd, const char *key, const char *value)
181 if (key != NULL && value != NULL) {
182 buf = g_strdup_printf("%s %s\n", key, value);
183 ret = full_write(fd, buf, strlen(buf));
191 static ssize_t write_bool_option(int fd, const char *key, const char *value)
196 if (key != NULL && value != NULL) {
197 if (strcmp(value, "yes") == 0) {
198 buf = g_strdup_printf("%s\n", key);
199 ret = full_write(fd, buf, strlen(buf));
208 static int vc_write_config_data(struct connman_provider *provider, int fd)
213 for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
214 opt_s = connman_provider_get_string(provider,
215 vpnc_options[i].cm_opt);
217 opt_s= vpnc_options[i].vpnc_default;
222 if (vpnc_options[i].type == OPT_STRING) {
224 vpnc_options[i].vpnc_opt, opt_s) < 0)
226 } else if (vpnc_options[i].type == OPT_BOOLEAN) {
227 if (write_bool_option(fd,
228 vpnc_options[i].vpnc_opt, opt_s) < 0)
237 static int vc_save(struct connman_provider *provider, GKeyFile *keyfile)
242 for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
243 if (strncmp(vpnc_options[i].cm_opt, "VPNC.", 5) == 0) {
245 if (vpnc_options[i].cm_save == FALSE)
248 option = connman_provider_get_string(provider,
249 vpnc_options[i].cm_opt);
253 g_key_file_set_string(keyfile,
254 connman_provider_get_save_group(provider),
255 vpnc_options[i].cm_opt, option);
261 static int vc_connect(struct connman_provider *provider,
262 struct connman_task *task, const char *if_name)
267 option = connman_provider_get_string(provider, "Host");
268 if (option == NULL) {
269 connman_error("Host not set; cannot enable VPN");
272 option = connman_provider_get_string(provider, "VPNC.IPSec.ID");
273 if (option == NULL) {
274 connman_error("Group not set; cannot enable VPN");
278 connman_task_add_argument(task, "--non-inter", NULL);
279 connman_task_add_argument(task, "--no-detach", NULL);
281 connman_task_add_argument(task, "--ifname", if_name);
282 connman_task_add_argument(task, "--ifmode", "tun");
284 connman_task_add_argument(task, "--script",
285 SCRIPTDIR "/openconnect-script");
287 option = connman_provider_get_string(provider, "VPNC.Debug");
289 connman_task_add_argument(task, "--debug", option);
291 connman_task_add_argument(task, "-", NULL);
293 err = connman_task_run(task, vpn_died, provider,
296 connman_error("vpnc failed to start");
300 err = vc_write_config_data(provider, fd);
307 static int vc_error_code(int exit_code)
311 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
313 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
315 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
319 static struct vpn_driver vpn_driver = {
321 .connect = vc_connect,
322 .error_code = vc_error_code,
326 static int vpnc_init(void)
328 connection = connman_dbus_get_connection();
330 return vpn_register("vpnc", &vpn_driver, VPNC);
333 static void vpnc_exit(void)
335 vpn_unregister("vpnc");
337 dbus_connection_unref(connection);
340 CONNMAN_PLUGIN_DEFINE(vpnc, "vpnc plugin", VERSION,
341 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpnc_init, vpnc_exit)