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/dbus.h>
43 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
45 static DBusConnection *connection;
55 const char *vpnc_default;
58 { "Host", "IPSec gateway", NULL, OPT_STRING },
59 { "VPNC.IPSec.ID", "IPSec ID", NULL, OPT_STRING },
60 { "VPNC.IPSec.Secret", "IPSec secret", NULL, OPT_STRING },
61 { "VPNC.Xauth.Username", "Xauth username", NULL, OPT_STRING },
62 { "VPNC.Xauth.Password", "Xauth password", NULL, OPT_STRING },
63 { "VPNC.IKE.Authmode", "IKE Authmode", NULL, OPT_STRING },
64 { "VPNC.IKE.DHGroup", "IKE DH Group", NULL, OPT_STRING },
65 { "VPNC.PFS", "Perfect Forward Secrecy", NULL, OPT_STRING },
66 { "VPNC.Domain", "Domain", NULL, OPT_STRING },
67 { "VPNC.Vendor", "Vendor", NULL, OPT_STRING },
68 { "VPNC.LocalPort", "Local Port", "0", OPT_STRING },
69 { "VPNC.CiscoPort","Cisco UDP Encapsulation Port", "0", OPT_STRING },
70 { "VPNC.AppVersion", "Application Version", NULL, OPT_STRING },
71 { "VPNC.NATTMode", "NAT Traversal Mode", "cisco-udp", OPT_STRING },
72 { "VPNC.DPDTimeout", "DPD idle timeout (our side)", NULL, OPT_STRING },
73 { "VPNC.SingleDES", "Enable Single DES", NULL, OPT_BOOLEAN },
74 { "VPNC.NoEncryption", "Enable no encryption", NULL, OPT_BOOLEAN },
77 static int vc_notify(DBusMessage *msg, struct connman_provider *provider)
79 DBusMessageIter iter, dict;
80 const char *reason, *key, *value;
82 dbus_message_iter_init(msg, &iter);
84 dbus_message_iter_get_basic(&iter, &reason);
85 dbus_message_iter_next(&iter);
88 connman_error("No provider found");
89 return VPN_STATE_FAILURE;
92 if (strcmp(reason, "connect"))
93 return VPN_STATE_DISCONNECT;
95 dbus_message_iter_recurse(&iter, &dict);
97 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
98 DBusMessageIter entry;
100 dbus_message_iter_recurse(&dict, &entry);
101 dbus_message_iter_get_basic(&entry, &key);
102 dbus_message_iter_next(&entry);
103 dbus_message_iter_get_basic(&entry, &value);
105 DBG("%s = %s", key, value);
107 if (!strcmp(key, "VPNGATEWAY"))
108 connman_provider_set_string(provider, "Gateway", value);
110 if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
111 connman_provider_set_string(provider, "Address", value);
113 if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
114 connman_provider_set_string(provider, "Netmask", value);
116 if (!strcmp(key, "INTERNAL_IP4_DNS"))
117 connman_provider_set_string(provider, "DNS", value);
119 if (!strcmp(key, "CISCO_DEF_DOMAIN"))
120 connman_provider_set_string(provider, "Domain", value);
122 if (g_str_has_prefix(key, "CISCO_SPLIT_INC") == TRUE ||
123 g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC") == TRUE)
124 connman_provider_append_route(provider, key, value);
126 dbus_message_iter_next(&dict);
129 return VPN_STATE_CONNECT;
132 static ssize_t full_write(int fd, const void *buf, size_t len)
137 byte_write = write(fd, buf, len);
138 if (byte_write < 0) {
139 connman_error("failed to write config to vpnc: %s\n",
150 static ssize_t write_option(int fd, const char *key, const char *value)
155 if (key != NULL && value != NULL) {
156 buf = g_strdup_printf("%s %s\n", key, value);
157 ret = full_write(fd, buf, strlen(buf));
165 static ssize_t write_bool_option(int fd, const char *key, const char *value)
170 if (key != NULL && value != NULL) {
171 if (strcmp(value, "yes") == 0) {
172 buf = g_strdup_printf("%s\n", key);
173 ret = full_write(fd, buf, strlen(buf));
182 static int vc_write_config_data(struct connman_provider *provider, int fd)
187 for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
188 opt_s = connman_provider_get_string(provider,
189 vpnc_options[i].cm_opt);
191 opt_s= vpnc_options[i].vpnc_default;
196 if (vpnc_options[i].type == OPT_STRING) {
198 vpnc_options[i].vpnc_opt, opt_s) < 0)
200 } else if (vpnc_options[i].type == OPT_BOOLEAN) {
201 if (write_bool_option(fd,
202 vpnc_options[i].vpnc_opt, opt_s) < 0)
211 static int vc_connect(struct connman_provider *provider,
212 struct connman_task *task, const char *if_name)
217 option = connman_provider_get_string(provider, "Host");
218 if (option == NULL) {
219 connman_error("Host not set; cannot enable VPN");
222 option = connman_provider_get_string(provider, "VPNC.IPSec.ID");
223 if (option == NULL) {
224 connman_error("Group not set; cannot enable VPN");
228 connman_task_add_argument(task, "--non-inter", NULL);
229 connman_task_add_argument(task, "--no-detach", NULL);
231 connman_task_add_argument(task, "--ifname", if_name);
232 connman_task_add_argument(task, "--ifmode", "tun");
234 connman_task_add_argument(task, "--script",
235 SCRIPTDIR "/openconnect-script");
237 option = connman_provider_get_string(provider, "VPNC.Debug");
239 connman_task_add_argument(task, "--debug", option);
241 connman_task_add_argument(task, "-", NULL);
243 err = connman_task_run(task, vpn_died, provider,
246 connman_error("vpnc failed to start");
250 err = vc_write_config_data(provider, fd);
257 static int vc_error_code(int exit_code)
261 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
263 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
265 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
269 static struct vpn_driver vpn_driver = {
271 .connect = vc_connect,
272 .error_code = vc_error_code,
275 static int vpnc_init(void)
277 connection = connman_dbus_get_connection();
279 return vpn_register("vpnc", &vpn_driver, VPNC);
282 static void vpnc_exit(void)
284 vpn_unregister("vpnc");
286 dbus_connection_unref(connection);
289 CONNMAN_PLUGIN_DEFINE(vpnc, "vpnc plugin", VERSION,
290 CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpnc_init, vpnc_exit)