[network-manager-libreswan/lr/import-export: 7/7] properties: add connection import capability



commit 973eee41bd7b247e5e347c226879a96bd892e2ba
Author: Lubomir Rintel <lkundrak v3 sk>
Date:   Mon Dec 7 13:38:49 2015 +0100

    properties: add connection import capability

 common/utils.c            |   43 +++++++++++++++++++++++++++++++++++++++++++
 common/utils.h            |    3 +++
 properties/nm-libreswan.c |   25 +++++++++++++++++++++++--
 3 files changed, 69 insertions(+), 2 deletions(-)
---
diff --git a/common/utils.c b/common/utils.c
index 9e68a41..53c3878 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -29,6 +29,7 @@
 #ifdef NM_LIBRESWAN_OLD
 #define NM_VPN_LIBNM_COMPAT
 #include <nm-connection.h>
+#define nm_simple_connection_new nm_connection_new
 #endif
 
 #include "nm-libreswan-service.h"
@@ -36,6 +37,48 @@
 
 gboolean debug = FALSE;
 
+NMConnection *
+nm_libreswan_config_read (gint fd)
+{
+       NMConnection *connection;
+       NMSettingConnection *s_con;
+       NMSettingVpn *s_vpn;
+       GIOChannel *chan;
+       gchar *str;
+
+       connection = nm_simple_connection_new ();
+       s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());
+       s_vpn = NM_SETTING_VPN (nm_setting_vpn_new ());
+       nm_connection_add_setting (connection, NM_SETTING (s_con));
+       nm_connection_add_setting (connection, NM_SETTING (s_vpn));
+       g_object_set (s_vpn, NM_SETTING_VPN_SERVICE_TYPE, NM_VPN_SERVICE_TYPE_LIBRESWAN, NULL);
+
+       chan = g_io_channel_unix_new (fd);
+       while (g_io_channel_read_line (chan, &str, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {
+               g_strstrip (str);
+               if (g_str_has_prefix (str, "conn "))
+                       g_object_set (s_con, NM_SETTING_CONNECTION_ID, &str[5], NULL);
+               else if (g_str_has_prefix (str, "leftid=@"))
+                       nm_setting_vpn_add_data_item (s_vpn, NM_LIBRESWAN_LEFTID, &str[8]);
+               else if (g_str_has_prefix (str, "leftxauthusername="))
+                       nm_setting_vpn_add_data_item (s_vpn, NM_LIBRESWAN_LEFTXAUTHUSER, &str[18]);
+               else if (g_str_has_prefix (str, "right="))
+                       nm_setting_vpn_add_data_item (s_vpn, NM_LIBRESWAN_RIGHT, &str[6]);
+               else if (g_str_has_prefix (str, "ike=") && strcmp (str, "ike=aes-sha1"))
+                       nm_setting_vpn_add_data_item (s_vpn, NM_LIBRESWAN_IKE, &str[4]);
+               else if (g_str_has_prefix (str, "esp=") && strcmp (str, "esp=aes-sha1;modp1024"))
+                       nm_setting_vpn_add_data_item (s_vpn, NM_LIBRESWAN_ESP, &str[4]);
+               else if (g_str_has_prefix (str, "cisco-unity=yes"))
+                       nm_setting_vpn_add_data_item (s_vpn, NM_LIBRESWAN_VENDOR, "Cisco");
+               else if (debug)
+                       g_print ("Ignored line: '%s'", str);
+               g_free (str);
+       }
+       g_io_channel_unref (chan);
+
+       return connection;
+}
+
 void
 nm_libreswan_config_write (gint fd,
                            NMConnection *connection,
diff --git a/common/utils.h b/common/utils.h
index ee9b23e..155b1c9 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -42,6 +42,9 @@ write_config_option (int fd, const char *format, ...)
        va_end (args);
 }
 
+NMConnection *
+nm_libreswan_config_read (gint fd);
+
 void
 nm_libreswan_config_write (gint fd,
                            NMConnection *connection,
diff --git a/properties/nm-libreswan.c b/properties/nm-libreswan.c
index 1e0525f..5634d53 100644
--- a/properties/nm-libreswan.c
+++ b/properties/nm-libreswan.c
@@ -657,6 +657,27 @@ libreswan_editor_interface_init (NMVpnEditorInterface *iface_class)
        iface_class->update_connection = update_connection;
 }
 
+static NMConnection *
+import_from_file (NMVpnEditorPlugin *self,
+                  const char *path,
+                  GError **error)
+{
+       NMConnection *connection;
+       int fd;
+
+       fd = g_open (path, O_RDONLY, 0777);
+       if (fd == -1) {
+               g_set_error (error, LIBRESWAN_EDITOR_PLUGIN_ERROR, 0,
+                            _("Can't open file '%s': %s"), path, g_strerror (errno));
+               return FALSE;
+       }
+
+       connection = nm_libreswan_config_read (fd);
+       g_close (fd, NULL);
+
+       return connection;
+}
+
 static gboolean
 export_to_file (NMVpnEditorPlugin *self,
                 const char *path,
@@ -685,7 +706,7 @@ export_to_file (NMVpnEditorPlugin *self,
 static guint32
 get_capabilities (NMVpnEditorPlugin *iface)
 {
-       return NM_VPN_EDITOR_PLUGIN_CAPABILITY_EXPORT;
+       return NM_VPN_EDITOR_PLUGIN_CAPABILITY_EXPORT | NM_VPN_EDITOR_PLUGIN_CAPABILITY_IMPORT;
 }
 
 static NMVpnEditor *
@@ -746,7 +767,7 @@ libreswan_editor_plugin_interface_init (NMVpnEditorPluginInterface *iface_class)
        iface_class->get_editor = get_editor;
        iface_class->get_capabilities = get_capabilities;
 
-       iface_class->import_from_file = NULL;
+       iface_class->import_from_file = import_from_file;
        iface_class->export_to_file = export_to_file;
 
        iface_class->get_suggested_filename = NULL;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]