From 1b32cafa1ba855523ad2def5c14b361ebe298903 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 22 Nov 2012 17:45:47 +0100 Subject: [PATCH] Pull out function to build password input out of passwords https://bugs.freedesktop.org/show_bug.cgi?id=55041 --- service/realm-command.c | 37 +++++++++++++++++++++++++++++++++++++ service/realm-command.h | 2 ++ service/realm-samba-enroll.c | 36 ++---------------------------------- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/service/realm-command.c b/service/realm-command.c index 38a8d2b..4322fc7 100644 --- a/service/realm-command.c +++ b/service/realm-command.c @@ -626,3 +626,40 @@ realm_command_run_finish (GAsyncResult *result, return command->exit_code; } + +static void +clear_and_free_password (gpointer data) +{ + gchar *password = data; + memset ((char *)password, 0, strlen (password)); + g_free (password); +} + +GBytes * +realm_command_build_password_line (GBytes *password) +{ + GByteArray *array; + gconstpointer data; + gsize length; + guchar *result; + + array = g_byte_array_new (); + data = g_bytes_get_data (password, &length); + g_byte_array_append (array, data, length); + + /* + * We add a new line, which getpass() used inside command expects + */ + g_byte_array_append (array, (guchar *)"\n", 1); + length = array->len; + + /* + * In addition we add null terminator. This is not + * written to 'net' command, but used by clear_and_free_password(). + */ + g_byte_array_append (array, (guchar *)"\0", 1); + + result = g_byte_array_free (array, FALSE); + return g_bytes_new_with_free_func (result, length, + clear_and_free_password, result); +} diff --git a/service/realm-command.h b/service/realm-command.h index 52e268a..86b3703 100644 --- a/service/realm-command.h +++ b/service/realm-command.h @@ -36,6 +36,8 @@ gint realm_command_run_finish (GAsyncResult *re GString **output, GError **error); +GBytes * realm_command_build_password_line (GBytes *password); + G_END_DECLS #endif /* REALM_COMMAND_H */ diff --git a/service/realm-samba-enroll.c b/service/realm-samba-enroll.c index 9040faf..313cb72 100644 --- a/service/realm-samba-enroll.c +++ b/service/realm-samba-enroll.c @@ -42,14 +42,6 @@ typedef struct { } JoinClosure; static void -clear_and_free_password (gpointer data) -{ - gchar *password = data; - memset ((char *)password, 0, strlen (password)); - g_free (password); -} - -static void join_closure_free (gpointer data) { JoinClosure *join = data; @@ -72,37 +64,13 @@ join_closure_init (const gchar *realm, GDBusMethodInvocation *invocation) { JoinClosure *join; - GByteArray *array; - const guchar *data; - guchar *input; - gsize length; join = g_slice_new0 (JoinClosure); join->realm = g_strdup (realm); join->invocation = invocation ? g_object_ref (invocation) : NULL; - if (password) { - array = g_byte_array_new (); - data = g_bytes_get_data (password, &length); - g_byte_array_append (array, data, length); - - /* - * We add a new line, which getpass() used inside net - * command expects - */ - g_byte_array_append (array, (guchar *)"\n", 1); - length = array->len; - - /* - * In addition we add null terminator. This is not - * written to 'net' command, but used by clear_and_free_password(). - */ - g_byte_array_append (array, (guchar *)"\0", 1); - - input = g_byte_array_free (array, FALSE); - join->password_input = g_bytes_new_with_free_func (input, length, - clear_and_free_password, input); - } + if (password) + join->password_input = realm_command_build_password_line (password); join->user_name = g_strdup (user_name); -- 1.8.1