[PATCH 1/3] Add vpnc support.

Mohamed Abbas mabbas at linux.intel.com
Sat Jan 15 16:34:00 PST 2011


Add vpnc vpn support.
---
 plugins/vpnc.c        |  349 +++++++++++++++++++++++++++++++++++++++++++++++++
 scripts/vpnc-script.c |  134 +++++++++++++++++++
 2 files changed, 483 insertions(+), 0 deletions(-)
 create mode 100644 plugins/vpnc.c
 create mode 100644 scripts/vpnc-script.c

diff --git a/plugins/vpnc.c b/plugins/vpnc.c
new file mode 100644
index 0000000..1255a90
--- /dev/null
+++ b/plugins/vpnc.c
@@ -0,0 +1,349 @@
+/*
+ *
+ *  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
+
+static DBusConnection *connection;
+
+struct vc_route {
+	char *host;
+	char *netmask;
+};
+
+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);
+
+	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 void write_option(int fd, const char *key, const char *value)
+{
+	gchar *buf;
+	int byte_write;
+
+	if (key != NULL && value != NULL) {
+		buf = g_strdup_printf("%s %s\n", key, value);
+		byte_write = write(fd, buf, strlen(buf));
+
+		g_free(buf);
+	}
+}
+
+static void write_bool_option(int fd, const char *key, const char *value)
+{
+	gchar *buf;
+	int byte_write;
+
+	if (key != NULL && value != NULL) {
+		if (strcmp(value, "yes") == 0) {
+			buf = g_strdup_printf("%s\n", key);
+			byte_write = write(fd, buf, strlen(buf));
+
+			g_free(buf);
+		}
+	}
+}
+
+static void vc_write_config_data(struct connman_provider *provider, int fd)
+{
+	const char *value;
+
+	value = connman_provider_get_string(provider, "Host");
+	write_option(fd, "IPSec gateway", value);
+
+	write_option(fd, "Cisco UDP Encapsulation Port", "0");
+
+	value = connman_provider_get_string(provider, "VPNC.Group");
+	write_option(fd, "IPSec ID", value);
+
+	value = connman_provider_get_string(provider, "VPNC.User");
+	write_option(fd, "Xauth username", value);
+
+	value = connman_provider_get_string(provider, "VPNC.Secret");
+	write_option(fd, "IPSec secret", value);
+	value = connman_provider_get_string(provider, "VPNC.UserPassword");
+	write_option(fd, "Xauth password", value);
+
+	value = connman_provider_get_string(provider, "VPNC.SingleDES");
+	write_bool_option(fd, "Enable Single DES", value);
+
+	value = connman_provider_get_string(provider, "VPNC.NoEncryption");
+	write_bool_option(fd, "Enable no encryption", value);
+
+	value = connman_provider_get_string(provider, "VPNC.DHGroup");
+	write_option(fd, "IKE DH Group", value);
+
+	value = connman_provider_get_string(provider, "VPNC.NATMode");
+	if (value == NULL || strlen(value) == 0)
+		write_option(fd, "NAT Traversal Mode", "cisco-udp");
+	else
+		write_option(fd, "NAT Traversal Mode", value);
+
+	value = connman_provider_get_string(provider, "VPNC.LocalPort");
+	if (value == NULL)
+		write_option(fd, "Local Port", "0");
+	else
+		write_option(fd, "Local Port", value);
+
+	value = connman_provider_get_string(provider, "VPNC.DPDIdleTimeout");
+	write_option(fd, "DPD idle timeout (our side)", value);
+}
+
+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.Group");
+        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 "/vpnc-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;
+	}
+
+	vc_write_config_data(provider, fd);
+
+	close(fd);
+ 
+	return 0;
+}
+
+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/vpnc-script.c b/scripts/vpnc-script.c
new file mode 100644
index 0000000..509fc95
--- /dev/null
+++ b/scripts/vpnc-script.c
@@ -0,0 +1,134 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007-2010  Intel Corporation. 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <dbus/dbus.h>
+
+extern char **environ;
+
+static void append(DBusMessageIter *dict, const char *pattern)
+{
+	DBusMessageIter entry;
+	const char *key, *value;
+	char *delim;
+
+	delim = strchr(pattern, '=');
+	*delim = '\0';
+
+	key = pattern;
+	value = delim + 1;
+
+	/* We clean the environment before invoking openconnect, but
+	   might as well still filter out the few things that get
+	   added that we're not interested in */
+	if (!strcmp(key, "PWD") || !strcmp(key, "_") ||
+	    !strcmp(key, "SHLVL") || !strcmp(key, "connman_busname") ||
+	    !strcmp(key, "connman_network"))
+		return;
+
+	dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
+							NULL, &entry);
+
+	dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
+
+	dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
+
+	dbus_message_iter_close_container(dict, &entry);
+}
+
+int main(int argc, char *argv[])
+{
+	DBusConnection *conn;
+	DBusError error;
+	DBusMessage *msg;
+	DBusMessageIter iter, dict;
+	char **envp, *busname, *reason, *interface, *path;
+
+	busname = getenv("CONNMAN_BUSNAME");
+	interface = getenv("CONNMAN_INTERFACE");
+	path = getenv("CONNMAN_PATH");
+
+	reason = getenv("reason");
+
+	if (!busname || !interface || !path || !reason) {
+		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);
+	if (conn == NULL) {
+		if (dbus_error_is_set(&error) == TRUE) {
+			fprintf(stderr, "%s\n", error.message);
+			dbus_error_free(&error);
+		} else
+			fprintf(stderr, "Failed to get on system bus\n");
+		return 0;
+	}
+
+	msg = dbus_message_new_method_call(busname, path,
+						interface, "notify");
+	if (msg == NULL) {
+		dbus_connection_unref(conn);
+		fprintf(stderr, "Failed to allocate method call\n");
+		return 0;
+	}
+
+	dbus_message_set_no_reply(msg, TRUE);
+
+	dbus_message_append_args(msg,
+				 DBUS_TYPE_STRING, &reason,
+				 DBUS_TYPE_INVALID);
+
+	dbus_message_iter_init_append(msg, &iter);
+
+	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+			DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+			DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
+			DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
+
+	for (envp = environ; envp && *envp; envp++)
+		append(&dict, *envp);
+
+	dbus_message_iter_close_container(&iter, &dict);
+
+	if (dbus_connection_send(conn, msg, NULL) == FALSE)
+		fprintf(stderr, "Failed to send message\n");
+
+	dbus_connection_flush(conn);
+
+	dbus_message_unref(msg);
+
+	dbus_connection_unref(conn);
+
+	return 0;
+}
-- 
1.7.3.4




More information about the connman mailing list