diff options
-rw-r--r-- | plugins/vpnc.c | 277 | ||||
-rw-r--r-- | scripts/openconnect-script.c | 4 |
2 files changed, 281 insertions, 0 deletions
diff --git a/plugins/vpnc.c b/plugins/vpnc.c new file mode 100644 index 00000000..b04ad24f --- /dev/null +++ b/plugins/vpnc.c @@ -0,0 +1,277 @@ +/* + * + * 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 ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) + +static DBusConnection *connection; + +enum { + OPT_STRING = 1, + OPT_BOOLEAN = 2, +}; + +struct { + const char *cm_opt; + const char *vpnc_opt; + const char *vpnc_default; + int type; +} vpnc_options[] = { + { "Host", "IPSec gateway", NULL, OPT_STRING }, + { "VPNC.IPSec.ID", "IPSec ID", NULL, OPT_STRING }, + { "VPNC.IPSec.Secret", "IPSec secret", NULL, OPT_STRING }, + { "VPNC.Xauth.Username", "Xauth username", NULL, OPT_STRING }, + { "VPNC.Xauth.Password", "Xauth password", NULL, OPT_STRING }, + { "VPNC.IKE.Authmode", "IKE Authmode", NULL, OPT_STRING }, + { "VPNC.IKE.DHGroup", "IKE DH Group", NULL, OPT_STRING }, + { "VPNC.PFS", "Perfect Forward Secrecy", NULL, OPT_STRING }, + { "VPNC.Domain", "Domain", NULL, OPT_STRING }, + { "VPNC.Vendor", "Vendor", NULL, OPT_STRING }, + { "VPNC.LocalPort", "Local Port", "0", OPT_STRING }, + { "VPNC.CiscoPort","Cisco UDP Encapsulation Port", "0", OPT_STRING }, + { "VPNC.AppVersion", "Application Version", NULL, OPT_STRING }, + { "VPNC.NATTMode", "NAT Traversal Mode", "cisco-udp", OPT_STRING }, + { "VPNC.DPDTimeout", "DPD idle timeout (our side)", NULL, OPT_STRING }, + { "VPNC.SingleDES", "Enable Single DES", NULL, OPT_BOOLEAN }, + { "VPNC.NoEncryption", "Enable no encryption", NULL, OPT_BOOLEAN }, +}; + +static int vc_notify(DBusMessage *msg, struct connman_provider *provider) +{ + DBusMessageIter iter, dict; + const char *reason, *key, *value; + + 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); + + 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); + + if (g_str_has_prefix(key, "CISCO_SPLIT_INC") == TRUE || + g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC") == TRUE) + connman_provider_append_route(provider, key, value); + + dbus_message_iter_next(&dict); + } + + 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) + opt_s= vpnc_options[i].vpnc_default; + + if(!opt_s) + continue; + + if (vpnc_options[i].type == OPT_STRING) { + if (write_option(fd, + vpnc_options[i].vpnc_opt, opt_s) < 0) + return -EIO; + } else if (vpnc_options[i].type == OPT_BOOLEAN) { + if (write_bool_option(fd, + vpnc_options[i].vpnc_opt, opt_s) < 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 565bda74..509fc958 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); |