[network-manager-openvpn/NM_0_8] properties: export HTTP proxy authfile too



commit 339620b379c67900cfc7f62a75773915382a905f
Author: Dan Williams <dcbw redhat com>
Date:   Tue Apr 5 22:59:43 2011 -0500

    properties: export HTTP proxy authfile too

 properties/import-export.c            |   31 ++++++++++++++++++++++++++++++-
 properties/tests/test-import-export.c |   20 ++++++--------------
 2 files changed, 36 insertions(+), 15 deletions(-)
---
diff --git a/properties/import-export.c b/properties/import-export.c
index 44dcb88..7ac27b0 100644
--- a/properties/import-export.c
+++ b/properties/import-export.c
@@ -694,6 +694,8 @@ do_export (const char *path, NMConnection *connection, GError **error)
 	const char *proxy_server = NULL;
 	const char *proxy_port = NULL;
 	const char *proxy_retry = NULL;
+	const char *proxy_username = NULL;
+	const char *proxy_password = NULL;
 
 	s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
 	g_assert (s_con);
@@ -879,12 +881,39 @@ do_export (const char *path, NMConnection *connection, GError **error)
 		proxy_server = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_PROXY_SERVER);
 		proxy_port = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_PROXY_PORT);
 		proxy_retry = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_PROXY_RETRY);
+		proxy_username = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_HTTP_PROXY_USERNAME);
+		proxy_password = nm_setting_vpn_get_secret (s_vpn, NM_OPENVPN_KEY_HTTP_PROXY_PASSWORD);
+
 		if (!strcmp (proxy_type, "http") && proxy_server && proxy_port) {
+			char *authfile, *authcontents, *base, *dirname;
+
 			if (!proxy_port)
 				proxy_port = "8080";
-			fprintf (f, "http-proxy %s %s\n", proxy_server, proxy_port);
+
+			/* If there's a username, need to write an authfile */
+			base = g_path_get_basename (path);
+			dirname = g_path_get_dirname (path);
+			authfile = g_strdup_printf ("%s/%s-httpauthfile", dirname, base);
+			g_free (base);
+			g_free (dirname);
+
+			fprintf (f, "http-proxy %s %s%s%s\n",
+			         proxy_server,
+			         proxy_port,
+			         proxy_username ? " " : "",
+			         proxy_username ? authfile : "");
 			if (proxy_retry && !strcmp (proxy_retry, "yes"))
 				fprintf (f, "http-proxy-retry\n");
+
+			/* Write out the authfile */
+			if (proxy_username) {
+				authcontents = g_strdup_printf ("%s\n%s\n",
+				                                proxy_username,
+				                                proxy_password ? proxy_password : "");
+				g_file_set_contents (authfile, authcontents, -1, NULL);
+				g_free (authcontents);
+			}
+			g_free (authfile);
 		} else if (!strcmp (proxy_type, "socks") && proxy_server && proxy_port) {
 			if (!proxy_port)
 				proxy_port = "1080";
diff --git a/properties/tests/test-import-export.c b/properties/tests/test-import-export.c
index 395c873..5bdb212 100644
--- a/properties/tests/test-import-export.c
+++ b/properties/tests/test-import-export.c
@@ -799,8 +799,6 @@ test_proxy_http_export (NMVpnPluginUiInterface *plugin, const char *dir, const c
 	char *path;
 	gboolean success;
 	GError *error = NULL;
-	int ret;
-	NMSettingVPN *s_vpn;
 
 	connection = get_basic_connection ("proxy-http-export", plugin, dir, "proxy-http.ovpn");
 	ASSERT (connection != NULL, "proxy-http-export", "failed to import connection");
@@ -817,25 +815,19 @@ test_proxy_http_export (NMVpnPluginUiInterface *plugin, const char *dir, const c
 	/* Now re-import it and compare the connections to ensure they are the same */
 	reimported = get_basic_connection ("proxy-http-export", plugin, tmpdir, PROXY_HTTP_EXPORTED_NAME);
 	(void) unlink (path);
+	g_free (path);
 	ASSERT (reimported != NULL, "proxy-http-export", "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);
-
-	/* Also clear the HTTP Proxy username.  We don't export that either since it
-	 * goes into a separate authfile.
-	 */
-	s_vpn = NM_SETTING_VPN (nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN));
-	nm_setting_vpn_remove_data_item (s_vpn, NM_OPENVPN_KEY_HTTP_PROXY_USERNAME);
-
 	ASSERT (nm_connection_compare (connection, reimported, NM_SETTING_COMPARE_FLAG_EXACT) == TRUE,
 	        "proxy-http-export", "original and reimported connection differ");
 
+	/* Unlink the proxy authfile */
+	path = g_strdup_printf ("%s/%s-httpauthfile", tmpdir, PROXY_HTTP_EXPORTED_NAME);
+	(void) unlink (path);
+	g_free (path);
+
 	g_object_unref (reimported);
 	g_object_unref (connection);
-	g_free (path);
 }
 
 static void



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