[network-manager-pptp] core: port everything to NM 0.9



commit 0d85d95a3f9feac662c15456e7ebf5208d9e4452
Author: Dan Williams <dcbw redhat com>
Date:   Tue Feb 22 12:03:30 2011 -0600

    core: port everything to NM 0.9

 auth-dialog/Makefile.am |    5 +-
 auth-dialog/main.c      |  146 ++++++++++++++++++++++++++++++++++-------------
 configure.ac            |    8 ---
 properties/Makefile.am  |    7 +--
 properties/nm-pptp.c    |   45 ++++++---------
 src/nm-pptp-service.c   |   22 +++++--
 6 files changed, 141 insertions(+), 92 deletions(-)
---
diff --git a/auth-dialog/Makefile.am b/auth-dialog/Makefile.am
index ec1593d..565370f 100644
--- a/auth-dialog/Makefile.am
+++ b/auth-dialog/Makefile.am
@@ -6,15 +6,13 @@ nm_pptp_auth_dialog_CPPFLAGS =			\
 	$(NM_CFLAGS)		\
 	$(GTHREAD_CFLAGS)			\
 	$(GTK_CFLAGS)				\
-	$(GCONF_CFLAGS)				\
 	$(GNOMEKEYRING_CFLAGS)			\
 	-DICONDIR=\""$(datadir)/pixmaps"\"	\
 	-DUIDIR=\""$(uidir)"\"			\
 	-DBINDIR=\""$(bindir)"\"		\
 	-DG_DISABLE_DEPRECATED			\
 	-DGDK_DISABLE_DEPRECATED		\
-	-DGNOME_DISABLE_DEPRECATED		\
-	-DGNOMELOCALEDIR=\"$(datadir)/locale\"	\
+	-DGTK_DISABLE_DEPRECATED		\
 	-DVERSION=\"$(VERSION)\"
 
 nm_pptp_auth_dialog_SOURCES =			\
@@ -24,7 +22,6 @@ nm_pptp_auth_dialog_SOURCES =			\
 
 nm_pptp_auth_dialog_LDADD =			\
 	$(GTK_LIBS)				\
-	$(GCONF_LIBS)				\
 	$(top_builddir)/common-gnome/libnm-pptp-common-gnome.la
 
 CLEANFILES = *~
diff --git a/auth-dialog/main.c b/auth-dialog/main.c
index 7b4e9a9..da7436e 100644
--- a/auth-dialog/main.c
+++ b/auth-dialog/main.c
@@ -26,39 +26,69 @@
 
 #include <string.h>
 #include <stdlib.h>
+#include <errno.h>
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
 #include <nm-setting-vpn.h>
+#include <nm-vpn-plugin-utils.h>
 
 #include "src/nm-pptp-service.h"
 #include "common-gnome/keyring-helpers.h"
 #include "gnome-two-password-dialog.h"
 
-#define KEYRING_UUID_TAG "connection-uuid"
-#define KEYRING_SN_TAG "setting-name"
-#define KEYRING_SK_TAG "setting-key"
-
 static gboolean
 get_secrets (const char *vpn_uuid,
              const char *vpn_name,
-             const char *vpn_service,
              gboolean retry,
-             char **password)
+             gboolean allow_interaction,
+             const char *in_pw,
+             char **out_pw,
+             NMSettingSecretFlags pw_flags)
 {
 	GnomeTwoPasswordDialog *dialog;
 	gboolean is_session = TRUE;
-	char *prompt;
+	char *prompt, *pw = NULL;
 
 	g_return_val_if_fail (vpn_uuid != NULL, FALSE);
 	g_return_val_if_fail (vpn_name != NULL, FALSE);
-	g_return_val_if_fail (password != NULL, FALSE);
-	g_return_val_if_fail (*password == NULL, FALSE);
+	g_return_val_if_fail (out_pw != NULL, FALSE);
+	g_return_val_if_fail (*out_pw == NULL, FALSE);
+
+	/* Get the existing secret, if any */
+	if (   !(pw_flags & NM_SETTING_SECRET_FLAG_NOT_SAVED)
+	    && !(pw_flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED)) {
+		if (in_pw)
+			pw = gnome_keyring_memory_strdup (in_pw);
+		else
+			pw = keyring_helpers_lookup_secret (vpn_uuid, NM_PPTP_KEY_PASSWORD, &is_session);
+	}
+
+	/* Don't ask if the passwords is unused */
+	if (pw_flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED) {
+		gnome_keyring_memory_free (pw);
+		return TRUE;
+	}
 
-	*password = keyring_helpers_lookup_secret (vpn_uuid, NM_PPTP_KEY_PASSWORD, &is_session);
-	if (!retry && *password)
+	if (!retry) {
+		/* Don't ask the user if we don't need a new password (ie, !retry),
+		 * we have an existing PW, and the password is saved.
+		 */
+		if (pw && !(pw_flags & NM_SETTING_SECRET_FLAG_NOT_SAVED)) {
+			*out_pw = pw;
+			return TRUE;
+		}
+	}
+
+	/* If interaction isn't allowed, just return existing secrets */
+	if (allow_interaction == FALSE) {
+		*out_pw = pw;
 		return TRUE;
+	}
 
+	/* Otherwise, we have no saved password, or the password flags indicated
+	 * that the password should never be saved.
+	 */
 	prompt = g_strdup_printf (_("You need to authenticate to access the Virtual Private Network '%s'."), vpn_name);
 	dialog = GNOME_TWO_PASSWORD_DIALOG (gnome_two_password_dialog_new (_("Authenticate VPN"), prompt, NULL, NULL, FALSE));
 	g_free (prompt);
@@ -70,7 +100,7 @@ get_secrets (const char *vpn_uuid,
 	gnome_two_password_dialog_set_show_password_secondary (dialog, FALSE);
 
 	/* If nothing was found in the keyring, default to not remembering any secrets */
-	if (*password) {
+	if (pw) {
 		/* Otherwise set default remember based on which keyring the secrets were found in */
 		if (is_session)
 			gnome_two_password_dialog_set_remember (dialog, GNOME_TWO_PASSWORD_DIALOG_REMEMBER_SESSION);
@@ -79,12 +109,9 @@ get_secrets (const char *vpn_uuid,
 	} else
 		gnome_two_password_dialog_set_remember (dialog, GNOME_TWO_PASSWORD_DIALOG_REMEMBER_NOTHING);
 
-	/* if retrying, pre-fill dialog with the password */
-	if (*password) {
-		gnome_two_password_dialog_set_password (dialog, *password);
-		g_free (*password);
-		*password = NULL;
-	}
+	/* pre-fill dialog with the password */
+	if (pw && !(pw_flags & NM_SETTING_SECRET_FLAG_NOT_SAVED))
+		gnome_two_password_dialog_set_password (dialog, pw);
 
 	gtk_widget_show (GTK_WIDGET (dialog));
 
@@ -92,7 +119,7 @@ get_secrets (const char *vpn_uuid,
 		const char *keyring = NULL;
 		gboolean save = FALSE;
 
-		*password = gnome_two_password_dialog_get_password (dialog);
+		*out_pw = gnome_two_password_dialog_get_password (dialog);
 
 		switch (gnome_two_password_dialog_get_remember (dialog)) {
 		case GNOME_TWO_PASSWORD_DIALOG_REMEMBER_SESSION:
@@ -105,10 +132,12 @@ get_secrets (const char *vpn_uuid,
 			break;
 		}
 
-		if (save) {
-			if (*password) {
-				keyring_helpers_save_secret (vpn_uuid, vpn_name, keyring,
-					   	NM_PPTP_KEY_PASSWORD, *password);
+		if (save && (pw_flags & NM_SETTING_SECRET_FLAG_AGENT_OWNED)) {
+		    if (*out_pw && !(pw_flags & NM_SETTING_SECRET_FLAG_NOT_SAVED))
+				keyring_helpers_save_secret (vpn_uuid, vpn_name, keyring, NM_PPTP_KEY_PASSWORD, *out_pw);
+			else {
+				/* Clear the password from the keyring */
+				keyring_helpers_delete_secret (vpn_uuid, NM_PPTP_KEY_PASSWORD);
 			}
 		}
 	}
@@ -119,22 +148,45 @@ get_secrets (const char *vpn_uuid,
 	return TRUE;
 }
 
+static void
+wait_for_quit (void)
+{
+	GString *str;
+	char c;
+	ssize_t n;
+	time_t start;
+
+	str = g_string_sized_new (10);
+	start = time (NULL);
+	do {
+		errno = 0;
+		n = read (0, &c, 1);
+		if (n == 0 || (n < 0 && errno == EAGAIN))
+			g_usleep (G_USEC_PER_SEC / 10);
+		else if (n == 1) {
+			g_string_append_c (str, c);
+			if (strstr (str->str, "QUIT") || (str->len > 10))
+				break;
+		} else
+			break;
+	} while (time (NULL) < start + 20);
+	g_string_free (str, TRUE);
+}
+
 int 
 main (int argc, char *argv[])
 {
-	gboolean retry = FALSE;
-	gchar *vpn_name = NULL;
-	gchar *vpn_uuid = NULL;
-	gchar *vpn_service = NULL;
-	char *password = NULL;
-	char buf[1];
-	int ret;
+	gboolean retry = FALSE, allow_interaction = FALSE;
+	char *vpn_name = NULL, *vpn_uuid = NULL, *vpn_service = NULL, *password = NULL;
+	GHashTable *data = NULL, *secrets = NULL;
+	NMSettingSecretFlags pw_flags = NM_SETTING_SECRET_FLAG_NONE;
 	GOptionContext *context;
 	GOptionEntry entries[] = {
 			{ "reprompt", 'r', 0, G_OPTION_ARG_NONE, &retry, "Reprompt for passwords", NULL},
 			{ "uuid", 'u', 0, G_OPTION_ARG_STRING, &vpn_uuid, "UUID of VPN connection", NULL},
 			{ "name", 'n', 0, G_OPTION_ARG_STRING, &vpn_name, "Name of VPN connection", NULL},
 			{ "service", 's', 0, G_OPTION_ARG_STRING, &vpn_service, "VPN service type", NULL},
+			{ "allow-interaction", 'i', 0, G_OPTION_ARG_NONE, &allow_interaction, "Allow user interaction", NULL},
 			{ NULL }
 		};
 
@@ -149,19 +201,29 @@ main (int argc, char *argv[])
 	g_option_context_parse (context, &argc, &argv, NULL);
 	g_option_context_free (context);
 
-
-	if (vpn_uuid == NULL || vpn_name == NULL || vpn_service == NULL) {
-		fprintf (stderr, "Have to supply UUID, name, and service\n");
-		return EXIT_FAILURE;
+	if (!vpn_uuid || !vpn_service || !vpn_name) {
+		fprintf (stderr, "A connection UUID, name, and VPN plugin service name are required.\n");
+		return 1;
 	}
 
 	if (strcmp (vpn_service, NM_DBUS_SERVICE_PPTP) != 0) {
 		fprintf (stderr, "This dialog only works with the '%s' service\n", NM_DBUS_SERVICE_PPTP);
-		return EXIT_FAILURE;
+		return 1;
+	}
+
+	if (!nm_vpn_plugin_utils_read_vpn_details (0, &data, &secrets)) {
+		fprintf (stderr, "Failed to read '%s' (%s) data and secrets from stdin.\n",
+		         vpn_name, vpn_uuid);
+		return 1;
 	}
 
-	if (!get_secrets (vpn_uuid, vpn_name, vpn_service, retry, &password))
-		return EXIT_FAILURE;
+	nm_vpn_plugin_utils_get_secret_flags (secrets, NM_PPTP_KEY_PASSWORD, &pw_flags);
+
+	if (!get_secrets (vpn_uuid, vpn_name, retry, allow_interaction,
+	                  g_hash_table_lookup (secrets, NM_PPTP_KEY_PASSWORD),
+	                  &password,
+	                  pw_flags))
+		return 1;
 
 	/* dump the passwords to stdout */
 	printf ("%s\n%s\n", NM_PPTP_KEY_PASSWORD, password);
@@ -175,8 +237,12 @@ main (int argc, char *argv[])
 	/* for good measure, flush stdout since Kansas is going Bye-Bye */
 	fflush (stdout);
 
-	/* wait for data on stdin  */
-	ret = fread (buf, sizeof (char), sizeof (buf), stdin);
+	/* Wait for quit signal */
+	wait_for_quit ();
 
-	return EXIT_SUCCESS;
+	if (data)
+		g_hash_table_unref (data);
+	if (secrets)
+		g_hash_table_unref (secrets);
+	return 0;
 }
diff --git a/configure.ac b/configure.ac
index e737413..0fc88b7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -91,14 +91,6 @@ if test x"$with_gnome" != xno; then
 	AC_SUBST(GTK_CFLAGS)
 	AC_SUBST(GTK_LIBS)
 
-	PKG_CHECK_MODULES(GDK_PIXBUF, gdk-pixbuf-2.0)
-	AC_SUBST(GDK_PIXBUF_CFLAGS)
-	AC_SUBST(GDK_PIXBUF_LIBS)
-
-	PKG_CHECK_MODULES(GCONF, gconf-2.0)
-	AC_SUBST(GCONF_CFLAGS)
-	AC_SUBST(GCONF_LIBS)
-
 	PKG_CHECK_MODULES(GNOMEKEYRING, gnome-keyring-1)
 	AC_SUBST(GNOMEKEYRING_CFLAGS)
 	AC_SUBST(GNOMEKEYRING_LIBS)
diff --git a/properties/Makefile.am b/properties/Makefile.am
index 61a33ba..1086b40 100644
--- a/properties/Makefile.am
+++ b/properties/Makefile.am
@@ -14,22 +14,17 @@ ui_DATA = nm-pptp-dialog.ui
 
 libnm_pptp_properties_la_CFLAGS =                       \
         $(GTK_CFLAGS)                                   \
-        $(GCONF_CFLAGS)                                 \
-        $(LIBGNOMEUI_CFLAGS)                            \
         $(GNOMEKEYRING_CFLAGS)                          \
         $(NM_CFLAGS)                        \
         -DICONDIR=\""$(datadir)/pixmaps"\"              \
         -DUIDIR=\""$(uidir)"\"                          \
         -DG_DISABLE_DEPRECATED                          \
         -DGDK_DISABLE_DEPRECATED                        \
-        -DGNOME_DISABLE_DEPRECATED                      \
-        -DGNOMELOCALEDIR=\"$(datadir)/locale\"          \
+	-DGTK_DISABLE_DEPRECATED                        \
         -DVERSION=\"$(VERSION)\"
 
 libnm_pptp_properties_la_LIBADD =       \
         $(GTK_LIBS)                     \
-        $(GCONF_LIBS)                   \
-        $(LIBGNOMEUI_LIBS)              \
         $(top_builddir)/common-gnome/libnm-pptp-common-gnome.la	\
         $(NM_LIBS)
 
diff --git a/properties/nm-pptp.c b/properties/nm-pptp.c
index 69483d1..5f609ba 100644
--- a/properties/nm-pptp.c
+++ b/properties/nm-pptp.c
@@ -231,6 +231,8 @@ fill_password (GtkBuilder *builder,
 {
 	GtkWidget *widget = NULL;
 	gchar *password = NULL;
+	NMSettingVPN *s_vpn;
+	gboolean unused;
 
 	widget = GTK_WIDGET (gtk_builder_get_object (builder, widget_name));
 	g_assert (widget);
@@ -238,27 +240,18 @@ fill_password (GtkBuilder *builder,
 	if (!connection)
 		return widget;
 
-	password = NULL;
-
-	if (nm_connection_get_scope (connection) == NM_CONNECTION_SCOPE_SYSTEM) {
-		NMSettingVPN *s_vpn;
-
-		s_vpn = (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
-		if (s_vpn) {
-			const gchar *tmp = NULL;
+	/* Try the connection first */
+	s_vpn = (NMSettingVPN *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
+	if (s_vpn) {
+		const gchar *tmp = NULL;
 
-			tmp = nm_setting_vpn_get_secret (s_vpn, password_type);
-			if (tmp)
-				password = gnome_keyring_memory_strdup (tmp);
-		}
-	} else {
-		NMSettingConnection *s_con = NULL;
-		gboolean unused;
-		const char *uuid;
+		tmp = nm_setting_vpn_get_secret (s_vpn, password_type);
+		if (tmp)
+			password = gnome_keyring_memory_strdup (tmp);
+	}
 
-		s_con = NM_SETTING_CONNECTION (nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION));
-		uuid = nm_setting_connection_get_uuid (s_con);
-		password = keyring_helpers_lookup_secret (uuid,
+	if (!password) {
+		password = keyring_helpers_lookup_secret (nm_connection_get_uuid (connection),
 		                                          password_type,
 		                                          &unused);
 	}
@@ -418,26 +411,24 @@ save_secrets (NMVpnPluginUiWidgetInterface *iface,
 	PptpPluginUiWidget *self = PPTP_PLUGIN_UI_WIDGET (iface);
 	PptpPluginUiWidgetPrivate *priv = PPTP_PLUGIN_UI_WIDGET_GET_PRIVATE (self);
 	GnomeKeyringResult ret;
-	NMSettingConnection *s_con;
 	GtkWidget *widget;
 	const char *str, *uuid, *id;
+	NMSettingSecretFlags flags = NM_SETTING_SECRET_FLAG_NONE;
 
-	s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
-	if (!s_con) {
+	id = nm_connection_get_id (connection);
+	uuid = nm_connection_get_uuid (connection);
+	if (!id || !uuid) {
 		g_set_error (error,
 		             PPTP_PLUGIN_UI_ERROR,
 		             PPTP_PLUGIN_UI_ERROR_INVALID_CONNECTION,
-		             "missing 'connection' setting");
+		             "missing ID or UUID");
 		return FALSE;
 	}
 
-	id = nm_setting_connection_get_id (s_con);
-	uuid = nm_setting_connection_get_uuid (s_con);
-
     widget = GTK_WIDGET (gtk_builder_get_object (priv->builder, "user_password_entry"));
     g_assert (widget);
     str = gtk_entry_get_text (GTK_ENTRY (widget));
-    if (str && strlen (str)) {
+    if (str && strlen (str) && (flags & NM_SETTING_SECRET_FLAG_AGENT_OWNED)) {
         ret = keyring_helpers_save_secret (uuid, id, NULL, NM_PPTP_KEY_PASSWORD, str);
         if (ret != GNOME_KEYRING_RESULT_OK)
             g_warning ("%s: failed to save user password to keyring.", __func__);
diff --git a/src/nm-pptp-service.c b/src/nm-pptp-service.c
index 24d4ed4..39a0269 100644
--- a/src/nm-pptp-service.c
+++ b/src/nm-pptp-service.c
@@ -1110,19 +1110,27 @@ real_need_secrets (NMVPNPlugin *plugin,
                    char **setting_name,
                    GError **error)
 {
-	NMSettingVPN *s_vpn;
+	NMSetting *s_vpn;
+	NMSettingSecretFlags flags = NM_SETTING_SECRET_FLAG_NONE;
 
 	g_return_val_if_fail (NM_IS_VPN_PLUGIN (plugin), FALSE);
 	g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
 
-	s_vpn = NM_SETTING_VPN (nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN));
+	s_vpn = nm_connection_get_setting (connection, NM_TYPE_SETTING_VPN);
 
-	if (!nm_setting_vpn_get_secret (s_vpn, NM_PPTP_KEY_PASSWORD)) {
-		*setting_name = NM_SETTING_VPN_SETTING_NAME;
-		return TRUE;
-	}
+	nm_setting_get_secret_flags (NM_SETTING (s_vpn), NM_PPTP_KEY_PASSWORD, &flags, NULL);
 
-	return FALSE;
+	/* Don't need the password if it's not required */
+	if (flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED)
+		return FALSE;
+
+	/* Don't need the password if we already have one */
+	if (nm_setting_vpn_get_secret (NM_SETTING_VPN (s_vpn), NM_PPTP_KEY_PASSWORD))
+		return FALSE;
+
+	/* Otherwise we need a password */
+	*setting_name = NM_SETTING_VPN_SETTING_NAME;
+	return TRUE;
 }
 
 static gboolean



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