[PATCH 1/3 v3] Add vpnc support.

Mohamed Abbas mabbas at linux.intel.com
Tue Jan 18 03:28:50 PST 2011


Initial.
---
 plugins/vpnc.c               |  391 ++++++++++++++++++++++++++++++++++++++++++
 scripts/openconnect-script.c |    4 +
 2 files changed, 395 insertions(+), 0 deletions(-)
 create mode 100644 plugins/vpnc.c

diff --git a/plugins/vpnc.c b/plugins/vpnc.c
new file mode 100644
index 0000000..f0c47b9
--- /dev/null
+++ b/plugins/vpnc.c
@@ -0,0 +1,391 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <net/if.h>
+
+#include <glib.h>
+
+#define CONNMAN_API_SUBJECT_TO_CHANGE
+#include <connman/plugin.h>
+#include <connman/provider.h>
+#include <connman/log.h>
+#include <connman/task.h>
+#include <connman/dbus.h>
+
+#include "vpn.h"
+
+#define VPNC_ROUTE_TYPE_NONE	0
+#define VPNC_ROUTE_TYPE_MASK	1
+#define VPNC_ROUTE_TYPE_ADDR	2
+
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
+
+static DBusConnection *connection;
+
+struct vc_route {
+	char *host;
+	char *netmask;
+};
+
+enum {
+	OPTION_STRING = 1,
+	OPTION_BOOLEAN = 2,
+};
+
+struct {
+	const char *cm_opt;
+	const char *vpnc_opt;
+	int type;
+} vpnc_options[] = {
+	{ "Host", "IPSec gateway", OPTION_STRING },
+	{ "VPNC.IPSec.ID", "IPSec ID", OPTION_STRING },
+	{ "VPNC.IPSec.Secret", "IPSec secret", OPTION_STRING },
+	{ "VPNC.Xauth.Username", "Xauth username", OPTION_STRING },
+	{ "VPNC.Xauth.Password", "Xauth password", OPTION_STRING },
+	{ "VPNC.IKE.Authmode", "IKE Authmode", OPTION_STRING },
+	{ "VPNC.IKE.DHGroup", "IKE DH Group", OPTION_STRING },
+	{ "VPNC.PFS", "Perfect Forward Secrecy", OPTION_STRING },
+	{ "VPNC.Domain", "Domain", OPTION_STRING },
+	{ "VPNC.Vendor", "Vendor", OPTION_STRING },
+	{ "VPNC.LocalPort", "Local Port", OPTION_STRING },
+	{ "VPNC.AppVersion", "Application Version", OPTION_STRING },
+	{ "VPNC.NATTMode", "NAT Traversal Mode", OPTION_STRING },
+	{ "VPNC.DPDIdleTimeout", "DPD idle timeout (our side)", OPTION_STRING },
+	{ "VPNC.SingleDES", "Enable Single DES", OPTION_BOOLEAN },
+	{ "VPNC.NoEncryption", "Enable no encryption", OPTION_BOOLEAN },
+};
+
+static void destroy_route(gpointer user_data)
+{
+	struct vc_route *route = user_data;
+
+	g_free(route->host);
+	g_free(route->netmask);
+	g_free(route);
+}
+
+static void vc_provider_append_routes(gpointer key, gpointer value,
+					gpointer user_data)
+{
+	struct vc_route *route = value;
+	struct connman_provider *provider = user_data;
+
+	connman_provider_append_route(provider, route->host, route->netmask,
+					NULL);
+}
+
+static struct vc_route *vc_route_lookup(const char *key, const char *prefix_key,
+					int *field_type, GHashTable *routes)
+{
+	unsigned long idx;
+	const char *start;
+	char *end;
+	struct vc_route *route;
+
+	if (g_str_has_prefix(key, prefix_key) == FALSE)
+		return NULL;
+
+	start = key + strlen(prefix_key);
+	idx = g_ascii_strtoull(start, &end, 10);
+
+	if (idx == 0 && start == end) {
+		connman_error("string conversion failed %s", start);
+		return NULL;
+	}
+
+	route = g_hash_table_lookup(routes, GINT_TO_POINTER(idx));
+	if (route == NULL) {
+		route = g_try_new0(struct vc_route, 1);
+		if (route == NULL) {
+			connman_error("out of memory");
+			return NULL;
+		}
+
+		g_hash_table_replace(routes, GINT_TO_POINTER(idx),
+						route);
+	}
+
+	if (strncmp(end, "_ADDR", 5) == 0)
+		*field_type = VPNC_ROUTE_TYPE_ADDR;
+	else if (strncmp(end, "_MASK", 5) == 0)
+		*field_type = VPNC_ROUTE_TYPE_MASK;
+
+	return  route;
+}
+
+static void vc_append_route(const char *key,
+				const char *value, GHashTable *routes)
+{
+	struct vc_route *route;
+	int type;
+
+	/*
+	 * VPNC pushes routing tupples (host, mask,masklen) as several
+	 * environment values, e.g.
+	 *
+	 * CISCO_SPLIT_INC_1_ADDR = 10.242.2.13
+	 * CISCO_SPLIT_INC_1_MASK = 255.255.0.0
+	 *
+	 * The hash table is used to group the separate environment
+
+	 * complete even when VPNC pushes the information in a
+	 * wrong order (unlikely).
+	 */
+
+	type = VPNC_ROUTE_TYPE_NONE;
+
+	route = vc_route_lookup(key, "CISCO_SPLIT_INC_", &type, routes);
+	if (route != NULL) {
+		if (type == VPNC_ROUTE_TYPE_ADDR) {
+			route->host = g_strdup(value);
+			return;
+		} else if (type == VPNC_ROUTE_TYPE_MASK) {
+			route->netmask = g_strdup(value);
+			return;
+		}
+	}
+}
+
+static int vc_notify(DBusMessage *msg, struct connman_provider *provider)
+{
+	DBusMessageIter iter, dict;
+	const char *reason, *key, *value;
+	GHashTable *routes;
+
+	dbus_message_iter_init(msg, &iter);
+
+	dbus_message_iter_get_basic(&iter, &reason);
+	dbus_message_iter_next(&iter);
+
+	if (!provider) {
+		connman_error("No provider found");
+		return VPN_STATE_FAILURE;
+	}
+
+	if (strcmp(reason, "connect"))
+		return VPN_STATE_DISCONNECT;
+
+	dbus_message_iter_recurse(&iter, &dict);
+
+	routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+					NULL, destroy_route);
+
+	while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
+		DBusMessageIter entry;
+
+		dbus_message_iter_recurse(&dict, &entry);
+		dbus_message_iter_get_basic(&entry, &key);
+		dbus_message_iter_next(&entry);
+		dbus_message_iter_get_basic(&entry, &value);
+
+		DBG("%s = %s", key, value);
+
+		if (!strcmp(key, "VPNGATEWAY"))
+			connman_provider_set_string(provider, "Gateway", value);
+
+		if (!strcmp(key, "INTERNAL_IP4_ADDRESS"))
+			connman_provider_set_string(provider, "Address", value);
+
+		if (!strcmp(key, "INTERNAL_IP4_NETMASK"))
+			connman_provider_set_string(provider, "Netmask", value);
+
+		if (!strcmp(key, "INTERNAL_IP4_DNS"))
+			connman_provider_set_string(provider, "DNS", value);
+
+		if (!strcmp(key, "CISCO_DEF_DOMAIN"))
+			connman_provider_set_string(provider, "Domain", value);
+
+		vc_append_route(key, value, routes);
+
+		dbus_message_iter_next(&dict);
+	}
+
+	g_hash_table_foreach(routes, vc_provider_append_routes, provider);
+
+	g_hash_table_destroy(routes);
+
+	return VPN_STATE_CONNECT;
+}
+
+static ssize_t full_write(int fd, const void *buf, size_t len)
+{
+	ssize_t byte_write;
+
+	while (len) {
+		byte_write = write(fd, buf, len);
+		if (byte_write < 0) {
+			connman_error("failed to write config to vpnc: %s\n",
+					strerror(errno));
+			return byte_write;
+		}
+		len -= byte_write;
+		buf += byte_write;
+	}
+
+	return 0;
+}
+
+static ssize_t write_option(int fd, const char *key, const char *value)
+{
+	gchar *buf;
+	ssize_t ret = 0;
+
+	if (key != NULL && value != NULL) {
+		buf = g_strdup_printf("%s %s\n", key, value);
+		ret = full_write(fd, buf, strlen(buf));
+
+		g_free(buf);
+	}
+
+	return ret;
+}
+
+static ssize_t write_bool_option(int fd, const char *key, const char *value)
+{
+	gchar *buf;
+	ssize_t ret = 0;
+
+	if (key != NULL && value != NULL) {
+		if (strcmp(value, "yes") == 0) {
+			buf = g_strdup_printf("%s\n", key);
+			ret = full_write(fd, buf, strlen(buf));
+
+			g_free(buf);
+		}
+	}
+
+	return ret;
+}
+
+static int vc_write_config_data(struct connman_provider *provider, int fd)
+{
+	const char *opt_s;
+	int i;
+
+	for (i = 0; i < (int)ARRAY_SIZE(vpnc_options); i++) {
+		opt_s = connman_provider_get_string(provider,
+					vpnc_options[i].cm_opt);
+		if (!opt_s)
+			continue;
+
+		if (vpnc_options[i].type == OPTION_STRING) {
+			if (write_option(fd,
+					vpnc_options[i].vpnc_opt, opt_s) < 0)
+				return -EIO;
+		} else if (vpnc_options[i].type == OPTION_BOOLEAN) {
+			if (write_bool_option(fd,
+					vpnc_options[i].vpnc_opt, opt_s) < 0)
+				return -EIO;
+		}
+
+	}
+
+	if (write_option(fd, "Cisco UDP Encapsulation Port", "0") < 0)
+		return -EIO;
+
+	opt_s = connman_provider_get_string(provider, "VPNC.NATTMode");
+	if (opt_s == NULL || strlen(opt_s) == 0)
+		if (write_option(fd, "NAT Traversal Mode", "cisco-udp") < 0)
+			return -EIO;
+
+	opt_s = connman_provider_get_string(provider, "VPNC.LocalPort");
+	if (opt_s == NULL)
+		if (write_option(fd, "Local Port", "0") < 0)
+			return -EIO;
+
+	return 0;
+}
+
+static int vc_connect(struct connman_provider *provider,
+		struct connman_task *task, const char *if_name)
+{
+	const char *option;
+	int err, fd;
+
+	option = connman_provider_get_string(provider, "Host");
+	if (option == NULL) {
+		connman_error("Host not set; cannot enable VPN");
+		return -EINVAL;
+	}
+	option = connman_provider_get_string(provider, "VPNC.IPSec.ID");
+	if (option == NULL) {
+		connman_error("Group not set; cannot enable VPN");
+		return -EINVAL;
+	}
+
+	connman_task_add_argument(task, "--non-inter", NULL);
+	connman_task_add_argument(task, "--no-detach", NULL);
+
+	connman_task_add_argument(task, "--ifname", if_name);
+	connman_task_add_argument(task, "--ifmode", "tun");
+
+	connman_task_add_argument(task, "--script",
+				SCRIPTDIR "/openconnect-script");
+
+	option = connman_provider_get_string(provider, "VPNC.Debug");
+	if (option != NULL)
+		connman_task_add_argument(task, "--debug", option);
+
+	connman_task_add_argument(task, "-", NULL);
+
+	err = connman_task_run(task, vpn_died, provider,
+				&fd, NULL, NULL);
+	if (err < 0) {
+		connman_error("vpnc failed to start");
+		return -EIO;
+	}
+
+	err = vc_write_config_data(provider, fd);
+
+	close(fd);
+ 
+	return err;
+}
+
+static struct vpn_driver vpn_driver = {
+	.notify		= vc_notify,
+	.connect	= vc_connect,
+};
+
+static int vpnc_init(void)
+{
+	connection = connman_dbus_get_connection();
+
+	return vpn_register("vpnc", &vpn_driver, VPNC);
+}
+
+static void vpnc_exit(void)
+{
+	vpn_unregister("vpnc");
+
+	dbus_connection_unref(connection);
+}
+
+CONNMAN_PLUGIN_DEFINE(vpnc, "vpnc plugin", VERSION,
+	CONNMAN_PLUGIN_PRIORITY_DEFAULT, vpnc_init, vpnc_exit)
diff --git a/scripts/openconnect-script.c b/scripts/openconnect-script.c
index 565bda7..509fc95 100644
--- a/scripts/openconnect-script.c
+++ b/scripts/openconnect-script.c
@@ -79,6 +79,10 @@ int main(int argc, char *argv[])
 		fprintf(stderr, "Required environment variables not set\n");
 		return 1;
 	}
+
+	if (strcmp(reason, "pre-init") == 0)
+		return 0;
+
 	dbus_error_init(&error);
 
 	conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
-- 
1.7.3.4




More information about the connman mailing list