[NetworkManager-fortisslvpn/lr/export-import: 5/5] properties: add import capability



commit 090b481e7a94580660e7ce43b3ae30fc3e992df9
Author: Lubomir Rintel <lkundrak v3 sk>
Date:   Wed Mar 20 23:31:22 2019 +0100

    properties: add import capability

 properties/nm-fortisslvpn-editor-plugin.c | 104 +++++++++++++++++++++++++++++-
 1 file changed, 102 insertions(+), 2 deletions(-)
---
diff --git a/properties/nm-fortisslvpn-editor-plugin.c b/properties/nm-fortisslvpn-editor-plugin.c
index 977d660..4b810d7 100644
--- a/properties/nm-fortisslvpn-editor-plugin.c
+++ b/properties/nm-fortisslvpn-editor-plugin.c
@@ -22,6 +22,7 @@
 #include "nm-default.h"
 
 #include "nm-fortisslvpn-editor-plugin.h"
+#include "nm-fortissl-properties.h"
 
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -59,7 +60,8 @@ G_DEFINE_TYPE_EXTENDED (FortisslvpnEditorPlugin, fortisslvpn_editor_plugin, G_TY
 static guint32
 get_capabilities (NMVpnEditorPlugin *iface)
 {
-       return NM_VPN_EDITOR_PLUGIN_CAPABILITY_EXPORT;
+       return   NM_VPN_EDITOR_PLUGIN_CAPABILITY_IMPORT
+              | NM_VPN_EDITOR_PLUGIN_CAPABILITY_EXPORT;
 }
 
 #ifndef NM_VPN_OLD
@@ -98,6 +100,104 @@ get_editor (NMVpnEditorPlugin *iface, NMConnection *connection, GError **error)
        }
 }
 
+static NMConnection *
+import_from_file (NMVpnEditorPlugin *iface, const char *filename,
+                  GError **error)
+{
+       gs_unref_object GFile *file = NULL;
+       gs_unref_object GFileInputStream *stream = NULL;
+       gs_unref_object GDataInputStream *data = NULL;
+       gs_unref_object NMSettingVpn *s_vpn = NULL;
+       gs_unref_object NMConnection *connection = NULL;
+       gs_free char *basename = NULL;
+       gs_free char *host = NULL;
+       gs_free char *port = NULL;
+       GError *local = NULL;
+       char *line;
+       gchar **words;
+
+       file = g_file_new_for_path (filename);
+       basename = g_file_get_basename (file);
+       stream = g_file_read (file, NULL, error);
+       if (!stream) {
+               g_prefix_error (error, _("Can not open input file: "));
+               return FALSE;
+       }
+
+       s_vpn = g_object_new (NM_TYPE_SETTING_VPN,
+                             NM_SETTING_VPN_SERVICE_TYPE, NM_DBUS_SERVICE_FORTISSLVPN,
+                             NULL);
+
+       data = g_data_input_stream_new (G_INPUT_STREAM (stream));
+       while ((line = g_data_input_stream_read_line (data, NULL, NULL, &local))) {
+               if (local) {
+                       g_propagate_prefixed_error (error, local,
+                                                   _("Error reading input file: "));
+                       return FALSE;
+               }
+               words = g_strsplit (line, "=", 2);
+               g_free (line);
+               if (words[0] && words[1]) {
+                       g_strchomp (words[0]);
+                       g_strchug (words[1]);
+                       if (strcmp (words[0], "host") == 0) {
+                               g_clear_pointer (&host, g_free);
+                               host = g_steal_pointer (&words[1]);
+                       } else if (strcmp (words[0], "port") == 0) {
+                               g_clear_pointer (&port, g_free);
+                               port = g_steal_pointer (&words[1]);
+                       } else if (strcmp (words[0], "username") == 0) {
+                               nm_setting_vpn_add_data_item (s_vpn, NM_FORTISSLVPN_KEY_USER, words[1]);
+                       } else if (strcmp (words[0], "password") == 0) {
+                               nm_setting_vpn_add_secret (s_vpn, NM_FORTISSLVPN_KEY_PASSWORD, words[1]);
+                       } else if (strcmp (words[0], "otp") == 0) {
+                               nm_setting_vpn_add_secret (s_vpn, NM_FORTISSLVPN_KEY_OTP, words[1]);
+                       } else if (strcmp (words[0], "ca-file") == 0) {
+                               nm_setting_vpn_add_data_item (s_vpn, NM_FORTISSLVPN_KEY_CA, words[1]);
+                       } else if (strcmp (words[0], "user-cert") == 0) {
+                               nm_setting_vpn_add_data_item (s_vpn, NM_FORTISSLVPN_KEY_CERT, words[1]);
+                       } else if (strcmp (words[0], "user-key") == 0) {
+                               nm_setting_vpn_add_data_item (s_vpn, NM_FORTISSLVPN_KEY_KEY, words[1]);
+                       } else if (strcmp (words[0], "trusted-cert") == 0) {
+                               nm_setting_vpn_add_data_item (s_vpn, NM_FORTISSLVPN_KEY_TRUSTED_CERT, 
words[1]);
+                       } else if (strcmp (words[0], "realm") == 0) {
+                               nm_setting_vpn_add_data_item (s_vpn, NM_FORTISSLVPN_KEY_REALM, words[1]);
+                       } else if (*words[0] != '#') {
+                               g_set_error (error, NMV_EDITOR_PLUGIN_ERROR,
+                                            NMV_EDITOR_PLUGIN_ERROR_INVALID_PROPERTY,
+                                            _("Unreconigzed token: '%s'"), words[0]);
+                               g_strfreev (words);
+                               return FALSE;
+                       }
+               }
+               g_strfreev (words);
+       }
+
+       if (host) {
+               line = g_strdup_printf ("%s%s%s", host, port ? ":" : "", port ?: "");
+               nm_setting_vpn_add_data_item (s_vpn, NM_FORTISSLVPN_KEY_GATEWAY, line);
+               g_free (line);
+       }
+
+       if (!nm_fortisslvpn_properties_validate (s_vpn, error))
+               return FALSE;
+
+       connection = nm_simple_connection_new ();
+
+       nm_connection_add_setting (connection,
+               g_object_new (NM_TYPE_SETTING_CONNECTION,
+                             NM_SETTING_CONNECTION_ID, basename,
+                             NULL));
+
+       nm_connection_add_setting (connection, g_steal_pointer (&s_vpn));
+       nm_connection_dump (connection);
+
+       if (!nm_connection_normalize (connection, NULL, NULL, error))
+               return FALSE;
+
+       return g_steal_pointer (&connection);
+}
+
 static gboolean
 export_to_file (NMVpnEditorPlugin *iface, const char *filename,
                 NMConnection *connection, GError **error)
@@ -154,7 +254,7 @@ fortisslvpn_editor_plugin_interface_init (NMVpnEditorPluginInterface *iface_clas
        /* interface implementation */
        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]