[network-manager-openvpn/NETWORKMANAGER_0_7] import/export: handle 'port' and 'rport' correctly (bgo #604329) (lp:443174)



commit 8499b8da3ccb630c946e72f0348cc9c58d88ef89
Author: Dan Williams <dcbw redhat com>
Date:   Mon Jan 18 00:43:42 2010 -0800

    import/export: handle 'port' and 'rport' correctly (bgo #604329) (lp:443174)

 properties/import-export.c            |   56 ++++++++++++++++++---
 properties/tests/conf/Makefile.am     |    4 +-
 properties/tests/conf/password.conf   |    1 +
 properties/tests/conf/port.ovpn       |   24 +++++++++
 properties/tests/conf/rport.ovpn      |   24 +++++++++
 properties/tests/test-import-export.c |   85 +++++++++++++++++++++++++++++++++
 6 files changed, 185 insertions(+), 9 deletions(-)
---
diff --git a/properties/import-export.c b/properties/import-export.c
index 298c5f1..267bfca 100644
--- a/properties/import-export.c
+++ b/properties/import-export.c
@@ -60,6 +60,8 @@
 #define AUTH_TAG "auth "
 #define RENEG_SEC_TAG "reneg-sec"
 #define TLS_REMOTE_TAG "tls-remote"
+#define PORT_TAG "port"
+#define RPORT_TAG "rport"
 
 
 static char *
@@ -177,6 +179,20 @@ handle_direction (const char *tag, const char *key, char *leftover, NMSettingVPN
 		g_warning ("%s: unknown %s direction '%s'", __func__, tag, leftover);
 }
 
+static char *
+parse_port (const char *str, const char *line)
+{
+	glong port;
+
+	errno = 0;
+	port = strtol (str, NULL, 10);
+	if ((errno == 0) && (port > 0) && (port < 65536))
+		return g_strdup_printf ("%d", (gint) port);
+
+	g_warning ("%s: invalid remote port in option '%s'", __func__, line);
+	return NULL;
+}
+
 NMConnection *
 do_import (const char *path, char **lines, GError **error)
 {
@@ -282,16 +298,13 @@ do_import (const char *path, char **lines, GError **error)
 				have_remote = TRUE;
 
 				if (g_strv_length (items) >= 2) {
-					glong port;
+					char *tmp;
 
-					errno = 0;
-					port = strtol (items[1], NULL, 10);
-					if ((errno == 0) && (port > 0) && (port < 65536)) {
-						char *tmp = g_strdup_printf ("%d", (guint32) port);
+					tmp = parse_port (items[1], *line);
+					if (tmp) {
 						nm_setting_vpn_add_data_item (s_vpn, NM_OPENVPN_KEY_PORT, tmp);
 						g_free (tmp);
-					} else
-						g_warning ("%s: invalid remote port in option '%s'", __func__, *line);
+					}
 				}
 			}
 			g_strfreev (items);
@@ -301,6 +314,30 @@ do_import (const char *path, char **lines, GError **error)
 			continue;
 		}
 
+		if (   !strncmp (*line, PORT_TAG, strlen (PORT_TAG))
+		    || !strncmp (*line, RPORT_TAG, strlen (RPORT_TAG))) {
+			char *tmp;
+
+			/* Port specified in 'remote' always takes precedence */
+			if (nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_PORT))
+				continue;
+
+			if (!strncmp (*line, PORT_TAG, strlen (PORT_TAG)))
+				items = get_args (*line + strlen (PORT_TAG));
+			else if (!strncmp (*line, RPORT_TAG, strlen (RPORT_TAG)))
+				items = get_args (*line + strlen (RPORT_TAG));
+			else
+				g_assert_not_reached ();
+
+			if (g_strv_length (items) >= 1) {
+				tmp = parse_port (items[0], *line);
+				if (tmp) {
+					nm_setting_vpn_add_data_item (s_vpn, NM_OPENVPN_KEY_PORT, tmp);
+					g_free (tmp);
+				}
+			}
+		}
+
 		if (handle_path_item (*line, CA_TAG, NM_OPENVPN_KEY_CA, s_vpn, default_path, NULL))
 			continue;
 
@@ -560,7 +597,10 @@ do_export (const char *path, NMConnection *connection, GError **error)
 	/* Advanced values end */
 
 	fprintf (f, "client\n");
-	fprintf (f, "remote %s %s\n", gateway, port ? port : "");
+	fprintf (f, "remote %s%s%s\n",
+	         gateway,
+	         port ? " " : "",
+	         port ? port : "");
 
 	if (cacert)
 		fprintf (f, "ca %s\n", cacert);
diff --git a/properties/tests/conf/Makefile.am b/properties/tests/conf/Makefile.am
index b291d05..9512a3e 100644
--- a/properties/tests/conf/Makefile.am
+++ b/properties/tests/conf/Makefile.am
@@ -3,6 +3,8 @@ EXTRA_DIST = \
 	tls.ovpn \
 	iso885915.ovpn \
 	static.key \
-	static.ovpn
+	static.ovpn \
+	port.ovpn \
+	rport.ovpn
 
 
diff --git a/properties/tests/conf/password.conf b/properties/tests/conf/password.conf
index bcf4946..c924a4f 100644
--- a/properties/tests/conf/password.conf
+++ b/properties/tests/conf/password.conf
@@ -4,6 +4,7 @@ dev tun
 proto udp
 topology subnet
 
+rport 2352
 remote test.server.com 443
 nobind
 persist-key
diff --git a/properties/tests/conf/port.ovpn b/properties/tests/conf/port.ovpn
new file mode 100644
index 0000000..3fbbf5c
--- /dev/null
+++ b/properties/tests/conf/port.ovpn
@@ -0,0 +1,24 @@
+port 2345
+
+remote 173.8.149.245
+resolv-retry infinite
+
+dev tun
+persist-key
+persist-tun
+link-mtu 1400
+proto udp
+nobind
+pull
+tls-client
+
+ca keys/mg8.ca
+cert keys/clee.crt
+key keys/clee.key
+
+tls-auth keys/46.key 1
+tls-remote "/CN=myvpn.company.com"
+
+comp-lzo
+verb 3
+
diff --git a/properties/tests/conf/rport.ovpn b/properties/tests/conf/rport.ovpn
new file mode 100644
index 0000000..4d09b5a
--- /dev/null
+++ b/properties/tests/conf/rport.ovpn
@@ -0,0 +1,24 @@
+rport 6789
+
+remote 173.8.149.245
+resolv-retry infinite
+
+dev tun
+persist-key
+persist-tun
+link-mtu 1400
+proto udp
+nobind
+pull
+tls-client
+
+ca keys/mg8.ca
+cert keys/clee.crt
+key keys/clee.key
+
+tls-auth keys/46.key 1
+tls-remote "/CN=myvpn.company.com"
+
+comp-lzo
+verb 3
+
diff --git a/properties/tests/test-import-export.c b/properties/tests/test-import-export.c
index a1245ed..951c66c 100644
--- a/properties/tests/test-import-export.c
+++ b/properties/tests/test-import-export.c
@@ -493,6 +493,85 @@ test_static_key_export (NMVpnPluginUiInterface *plugin, const char *dir)
 	g_free (path);
 }
 
+static void
+test_port_import (NMVpnPluginUiInterface *plugin,
+                  const char *detail,
+                  const char *dir,
+                  const char *file,
+                  const char *expected_id,
+                  const char *expected_port)
+{
+	NMConnection *connection;
+	NMSettingConnection *s_con;
+	NMSettingVPN *s_vpn;
+
+	connection = get_basic_connection (detail, plugin, dir, file);
+	ASSERT (connection != NULL, detail, "failed to import connection");
+
+	/* Connection setting */
+	s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
+	ASSERT (s_con != NULL,
+	        detail, "missing 'connection' setting");
+
+	ASSERT (strcmp (nm_setting_connection_get_id (s_con), expected_id) == 0,
+	        detail, "unexpected connection ID");
+
+	/* VPN setting */
+	s_vpn = (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
+	ASSERT (s_vpn != NULL,
+	        detail, "missing 'vpn' setting");
+
+	/* Data items */
+	test_item (detail, s_vpn, NM_OPENVPN_KEY_CONNECTION_TYPE, NM_OPENVPN_CONTYPE_TLS);
+	test_item (detail, s_vpn, NM_OPENVPN_KEY_PORT, expected_port);
+
+	g_object_unref (connection);
+}
+
+static void
+test_port_export (NMVpnPluginUiInterface *plugin,
+                  const char *detail,
+                  const char *dir,
+                  const char *file,
+                  const char *exported_name)
+{
+	NMConnection *connection;
+	NMConnection *reimported;
+	char *path;
+	gboolean success;
+	GError *error = NULL;
+	int ret;
+
+	connection = get_basic_connection (detail, plugin, dir, file);
+	ASSERT (connection != NULL, detail, "failed to import connection");
+
+	path = g_build_path ("/", dir, exported_name, NULL);
+	success = nm_vpn_plugin_ui_interface_export (plugin, path, connection, &error);
+	if (!success) {
+		if (!error)
+			FAIL (detail, "export failed with missing error");
+		else
+			FAIL (detail, "export failed: %s", error->message);
+	}
+
+	/* Now re-import it and compare the connections to ensure they are the same */
+	reimported = get_basic_connection (detail, plugin, dir, exported_name);
+	ret = unlink (path);
+	ASSERT (connection != NULL, detail, "failed to re-import connection");
+
+	/* Clear secrets first, since they don't get exported, and thus would
+	 * make the connection comparison below fail.
+	 */
+	remove_secrets (connection);
+
+	ASSERT (nm_connection_compare (connection, reimported, NM_SETTING_COMPARE_FLAG_EXACT) == TRUE,
+	        detail, "original and reimported connection differ");
+
+	g_object_unref (reimported);
+	g_object_unref (connection);
+	g_free (path);
+}
+
 int main (int argc, char **argv)
 {
 	GError *error = NULL;
@@ -527,6 +606,12 @@ int main (int argc, char **argv)
 	test_static_key_import (plugin, argv[1]);
 	test_static_key_export (plugin, argv[1]);
 
+	test_port_import (plugin, "port-import", argv[1], "port.ovpn", "port", "2345");
+	test_port_export (plugin, "port-export", argv[1], "port.ovpn", "port.ovpntest");
+
+	test_port_import (plugin, "rport-import", argv[1], "rport.ovpn", "rport", "6789");
+	test_port_export (plugin, "rport-export", argv[1], "rport.ovpn", "rport.ovpntest");
+
 	g_object_unref (plugin);
 
 	basename = g_path_get_basename (argv[0]);



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