[network-manager-openvpn/bg/inline-cert-fixes-bgo760746: 2/3] import: handle line splitting and utf8 conversion inside do_import()



commit da5628677409b3139756707ff29d2709c7e307e1
Author: Beniamino Galvani <bgalvani redhat com>
Date:   Mon Jan 18 16:36:33 2016 +0100

    import: handle line splitting and utf8 conversion inside do_import()
    
    The text parsing logic belongs to do_import(). No functional change.

 properties/import-export.c |   38 ++++++++++++++++++++++++++++++++++++--
 properties/import-export.h |    2 +-
 properties/nm-openvpn.c    |   32 +-------------------------------
 3 files changed, 38 insertions(+), 34 deletions(-)
---
diff --git a/properties/import-export.c b/properties/import-export.c
index 7a77584..d6e54d5 100644
--- a/properties/import-export.c
+++ b/properties/import-export.c
@@ -50,6 +50,7 @@
 
 #define OPENVPN_EDITOR_PLUGIN_ERROR                     NM_SETTING_VPN_ERROR
 #define OPENVPN_EDITOR_PLUGIN_ERROR_FILE_NOT_OPENVPN    NM_SETTING_VPN_ERROR_UNKNOWN
+#define OPENVPN_EDITOR_PLUGIN_ERROR_FILE_NOT_READABLE   NM_SETTING_VPN_ERROR_UNKNOWN
 
 #else /* !NM_OPENVPN_OLD */
 
@@ -57,6 +58,8 @@
 
 #define OPENVPN_EDITOR_PLUGIN_ERROR                     NM_CONNECTION_ERROR
 #define OPENVPN_EDITOR_PLUGIN_ERROR_FILE_NOT_OPENVPN    NM_CONNECTION_ERROR_FAILED
+#define OPENVPN_EDITOR_PLUGIN_ERROR_FILE_NOT_READABLE   NM_CONNECTION_ERROR_FAILED
+
 #endif
 
 #include "import-export.h"
@@ -479,19 +482,20 @@ parse_ip (const char *str, const char *line, guint32 *out_ip)
 }
 
 NMConnection *
-do_import (const char *path, char **lines, GError **error)
+do_import (const char *path, const char *contents, GError **error)
 {
        NMConnection *connection = NULL;
        NMSettingConnection *s_con;
        NMSettingIPConfig *s_ip4;
        NMSettingVpn *s_vpn;
        char *last_dot;
-       char **line;
+       char **line, **lines = NULL;
        gboolean have_client = FALSE, have_remote = FALSE;
        gboolean have_pass = FALSE, have_sk = FALSE;
        const char *ctype = NULL;
        char *basename;
        char *default_path, *tmp, *tmp2;
+       char *new_contents = NULL;
        gboolean http_proxy = FALSE, socks_proxy = FALSE, proxy_set = FALSE;
        int nitems;
 
@@ -524,6 +528,31 @@ do_import (const char *path, char **lines, GError **error)
                *last_dot = '\0';
        g_object_set (s_con, NM_SETTING_CONNECTION_ID, basename, NULL);
 
+       if (!g_utf8_validate (contents, -1, NULL)) {
+               GError *conv_error = NULL;
+
+               new_contents = g_locale_to_utf8 (contents, -1, NULL, NULL, &conv_error);
+               if (conv_error) {
+                       /* ignore the error, we tried at least. */
+                       g_error_free (conv_error);
+                       g_free (new_contents);
+               } else {
+                       g_assert (new_contents);
+                       contents = new_contents;  /* update contents with the UTF-8 safe text */
+               }
+       }
+
+       lines = g_strsplit_set (contents, "\r\n", 0);
+       if (g_strv_length (lines) <= 1) {
+               g_set_error_literal (error,
+                                    OPENVPN_EDITOR_PLUGIN_ERROR,
+                                    OPENVPN_EDITOR_PLUGIN_ERROR_FILE_NOT_READABLE,
+                                    _("not a valid OpenVPN configuration file"));
+               g_object_unref (connection);
+               connection = NULL;
+               goto out;
+       }
+
        for (line = lines; *line; line++) {
                char *comment, **items = NULL, *leftover = NULL;
 
@@ -1125,6 +1154,7 @@ route_fail:
                }
        }
 
+out:
        g_free (default_path);
        g_free (basename);
 
@@ -1133,6 +1163,10 @@ route_fail:
        else if (s_vpn)
                g_object_unref (s_vpn);
 
+       g_free (new_contents);
+       if (lines)
+               g_strfreev (lines);
+
        return connection;
 }
 
diff --git a/properties/import-export.h b/properties/import-export.h
index 0293288..0668353 100644
--- a/properties/import-export.h
+++ b/properties/import-export.h
@@ -30,7 +30,7 @@
 #include <NetworkManager.h>
 #endif
 
-NMConnection *do_import (const char *path, char **lines, GError **error);
+NMConnection *do_import (const char *path, const char *contents, GError **error);
 
 gboolean do_export (const char *path, NMConnection *connection, GError **error);
 
diff --git a/properties/nm-openvpn.c b/properties/nm-openvpn.c
index 7fe16d4..68ce85f 100644
--- a/properties/nm-openvpn.c
+++ b/properties/nm-openvpn.c
@@ -48,7 +48,6 @@
 #define OPENVPN_EDITOR_PLUGIN_ERROR_INVALID_PROPERTY    NM_SETTING_VPN_ERROR_INVALID_PROPERTY
 #define OPENVPN_EDITOR_PLUGIN_ERROR_MISSING_PROPERTY    NM_SETTING_VPN_ERROR_MISSING_PROPERTY
 #define OPENVPN_EDITOR_PLUGIN_ERROR_FILE_NOT_OPENVPN    NM_SETTING_VPN_ERROR_UNKNOWN
-#define OPENVPN_EDITOR_PLUGIN_ERROR_FILE_NOT_READABLE   NM_SETTING_VPN_ERROR_UNKNOWN
 
 #else /* !NM_OPENVPN_OLD */
 
@@ -58,7 +57,6 @@
 #define OPENVPN_EDITOR_PLUGIN_ERROR_INVALID_PROPERTY    NM_CONNECTION_ERROR_INVALID_PROPERTY
 #define OPENVPN_EDITOR_PLUGIN_ERROR_MISSING_PROPERTY    NM_CONNECTION_ERROR_MISSING_PROPERTY
 #define OPENVPN_EDITOR_PLUGIN_ERROR_FILE_NOT_OPENVPN    NM_CONNECTION_ERROR_FAILED
-#define OPENVPN_EDITOR_PLUGIN_ERROR_FILE_NOT_READABLE   NM_CONNECTION_ERROR_FAILED
 #endif
 
 #include "../src/nm-openvpn-service-defines.h"
@@ -637,7 +635,6 @@ import (NMVpnEditorPlugin *iface, const char *path, GError **error)
 {
        NMConnection *connection = NULL;
        char *contents = NULL;
-       char **lines = NULL;
        char *ext;
 
        ext = strrchr (path, '.');
@@ -656,36 +653,9 @@ import (NMVpnEditorPlugin *iface, const char *path, GError **error)
        if (!g_file_get_contents (path, &contents, NULL, error))
                return NULL;
 
-       if (!g_utf8_validate (contents, -1, NULL)) {
-               char *tmp;
-               GError *conv_error = NULL;
-
-               tmp = g_locale_to_utf8 (contents, -1, NULL, NULL, &conv_error);
-               if (conv_error) {
-                       /* ignore the error, we tried at least. */
-                       g_error_free (conv_error);
-                       g_free (tmp);
-               } else {
-                       g_assert (tmp);
-                       g_free (contents);
-                       contents = tmp;  /* update contents with the UTF-8 safe text */
-               }
-       }
-
-       lines = g_strsplit_set (contents, "\r\n", 0);
-       if (g_strv_length (lines) <= 1) {
-               g_set_error_literal (error,
-                                    OPENVPN_EDITOR_PLUGIN_ERROR,
-                                    OPENVPN_EDITOR_PLUGIN_ERROR_FILE_NOT_READABLE,
-                                    _("not a valid OpenVPN configuration file"));
-               goto out;
-       }
-
-       connection = do_import (path, lines, error);
+       connection = do_import (path, contents, error);
 
 out:
-       if (lines)
-               g_strfreev (lines);
        g_free (contents);
        return connection;
 }


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