[network-manager-openvpn/dcbw/need-secrets: 4/5] auth-dialog: better splitting of standard vs. external-ui-mode logic
- From: Dan Williams <dcbw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [network-manager-openvpn/dcbw/need-secrets: 4/5] auth-dialog: better splitting of standard vs. external-ui-mode logic
- Date: Wed, 17 Jul 2013 19:38:50 +0000 (UTC)
commit 533386d84cb175174f129d120e7f7415196b11cb
Author: Dan Williams <dcbw redhat com>
Date: Tue Jul 16 15:12:35 2013 -0500
auth-dialog: better splitting of standard vs. external-ui-mode logic
Use function pointers and populate them with the right functions for
each mode instead of interleaving confusing code everywhere.
auth-dialog/main.c | 160 ++++++++++++++++++++++++++++----------------
nm-openvpn-service.name.in | 1 +
2 files changed, 103 insertions(+), 58 deletions(-)
---
diff --git a/auth-dialog/main.c b/auth-dialog/main.c
index b457a79..3b71f36 100644
--- a/auth-dialog/main.c
+++ b/auth-dialog/main.c
@@ -90,6 +90,17 @@ keyring_lookup_secret (const char *uuid, const char *secret_name)
return secret;
}
+/*****************************************************************/
+
+typedef void (*NoSecretsRequiredFunc) (void);
+typedef void (*FinishFunc) (gboolean need_password,
+ const char *new_password,
+ gboolean need_certpass,
+ const char *new_certpass);
+
+/*****************************************************************/
+/* External UI mode stuff */
+
static void
keyfile_add_entry_info (GKeyFile *keyfile,
const gchar *key,
@@ -117,6 +128,73 @@ keyfile_print_stdout (GKeyFile *keyfile)
g_free (data);
}
+static void
+eui_no_secrets_required (void)
+{
+ GKeyFile *keyfile;
+
+ keyfile = g_key_file_new ();
+
+ g_key_file_set_integer (keyfile, UI_KEYFILE_GROUP, "Version", 2);
+ keyfile_add_entry_info (keyfile, NM_OPENVPN_KEY_NOSECRET, "true", "", TRUE, FALSE);
+ keyfile_print_stdout (keyfile);
+ g_key_file_unref (keyfile);
+}
+
+/*****************************************************************/
+
+static void
+std_no_secrets_required (void)
+{
+ printf ("%s\n%s\n\n\n", NM_OPENVPN_KEY_NOSECRET, "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);
+}
+
+static void
+std_finish (gboolean need_password,
+ const char *new_password,
+ gboolean need_certpass,
+ const char *new_certpass)
+{
+ if (need_password && new_password)
+ printf ("%s\n%s\n", NM_OPENVPN_KEY_PASSWORD, new_password);
+ if (need_certpass && new_certpass)
+ printf ("%s\n%s\n", NM_OPENVPN_KEY_CERTPASS, new_certpass);
+ printf ("\n\n");
+
+ /* for good measure, flush stdout since Kansas is going Bye-Bye */
+ fflush (stdout);
+
+ /* Wait for quit signal */
+ wait_for_quit ();
+}
+
+/*****************************************************************/
+
static gboolean
get_secrets (const char *vpn_name,
const char *vpn_uuid,
@@ -299,31 +377,6 @@ get_passwords_required (GHashTable *data,
return NULL;
}
-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[])
{
@@ -339,6 +392,10 @@ main (int argc, char *argv[])
gboolean external_ui_mode = FALSE;
NMSettingSecretFlags pw_flags = NM_SETTING_SECRET_FLAG_NONE;
NMSettingSecretFlags cp_flags = NM_SETTING_SECRET_FLAG_NONE;
+
+ FinishFunc finish_func = NULL;
+ NoSecretsRequiredFunc no_secrets_required_func = NULL;
+
GOptionContext *context;
GOptionEntry entries[] = {
{ "reprompt", 'r', 0, G_OPTION_ARG_NONE, &retry, "Reprompt for passwords", NULL},
@@ -378,6 +435,13 @@ main (int argc, char *argv[])
return 1;
}
+ if (external_ui_mode) {
+ no_secrets_required_func = eui_no_secrets_required;
+ } else {
+ no_secrets_required_func = std_no_secrets_required;
+ finish_func = std_finish;
+ }
+
/* Determine which passwords are actually required, either from hints or
* from looking at the VPN configuration.
*/
@@ -385,23 +449,9 @@ main (int argc, char *argv[])
/* Exit early if we don't need any passwords */
if (!need_password && !need_certpass) {
- if (external_ui_mode) {
- GKeyFile *keyfile;
-
- keyfile = g_key_file_new ();
-
- g_key_file_set_integer (keyfile, UI_KEYFILE_GROUP, "Version", 2);
- keyfile_add_entry_info (keyfile, NM_OPENVPN_KEY_NOSECRET, "true", "", TRUE, FALSE);
- keyfile_print_stdout (keyfile);
-
- g_key_file_unref (keyfile);
- } else {
- /* The older protocol */
- printf ("%s\n%s\n\n\n", NM_OPENVPN_KEY_NOSECRET, "true");
- }
-
- g_free (prompt);
- return 0;
+ if (no_secrets_required_func)
+ no_secrets_required_func ();
+ goto done;
}
nm_vpn_plugin_utils_get_secret_flags (data, NM_OPENVPN_KEY_PASSWORD, &pw_flags);
@@ -421,25 +471,19 @@ main (int argc, char *argv[])
&new_certpass))
return 1; /* canceled */
- if (!external_ui_mode) {
- if (need_password && new_password)
- printf ("%s\n%s\n", NM_OPENVPN_KEY_PASSWORD, new_password);
- if (need_certpass && new_certpass)
- printf ("%s\n%s\n", NM_OPENVPN_KEY_CERTPASS, new_certpass);
- printf ("\n\n");
-
- if (new_password)
- g_free (new_password);
- if (new_certpass)
- g_free (new_certpass);
+ if (finish_func)
+ finish_func (need_password, new_password, need_certpass, new_certpass);
- /* for good measure, flush stdout since Kansas is going Bye-Bye */
- fflush (stdout);
-
- /* Wait for quit signal */
- wait_for_quit ();
+ if (new_password) {
+ memset (new_password, 0, strlen (new_password));
+ g_free (new_password);
+ }
+ if (new_certpass) {
+ memset (new_certpass, 0, strlen (new_certpass));
+ g_free (new_certpass);
}
+done:
if (data)
g_hash_table_unref (data);
if (secrets)
diff --git a/nm-openvpn-service.name.in b/nm-openvpn-service.name.in
index 2ee066a..f4cce87 100644
--- a/nm-openvpn-service.name.in
+++ b/nm-openvpn-service.name.in
@@ -7,4 +7,5 @@ program= LIBEXECDIR@/nm-openvpn-service
auth-dialog= LIBEXECDIR@/nm-openvpn-auth-dialog
properties= PLUGINDIR@/libnm-openvpn-properties
supports-external-ui-mode=true
+supports-hints=true
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]