[gnome-initial-setup] account: Import newer pw-utils from g-c-c master



commit bc9f70c8860aa8c552cab3cebc9d02a7a7145e24
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Sun Nov 25 21:33:44 2012 -0500

    account: Import newer pw-utils from g-c-c master
    
    This will be used to build a strength bar for password quality

 gnome-initial-setup/pages/account/Makefile.am      |    4 +-
 .../pages/account/gis-account-page.c               |    2 +-
 .../pages/account/pw-utils-pwquality.c             |  118 --------------
 gnome-initial-setup/pages/account/pw-utils.c       |  171 ++++++++------------
 gnome-initial-setup/pages/account/pw-utils.h       |   14 +-
 5 files changed, 76 insertions(+), 233 deletions(-)
---
diff --git a/gnome-initial-setup/pages/account/Makefile.am b/gnome-initial-setup/pages/account/Makefile.am
index 05d034b..b1f2ad7 100644
--- a/gnome-initial-setup/pages/account/Makefile.am
+++ b/gnome-initial-setup/pages/account/Makefile.am
@@ -1,8 +1,6 @@
 
 NULL =
 
-pw_utils_sources = pw-utils-pwquality.c pw-utils.h
-
 noinst_LTLIBRARIES = libgisaccount.la
 
 BUILT_SOURCES =
@@ -18,11 +16,11 @@ um-realm-generated.h: $(srcdir)/org.freedesktop.realmd.xml
 BUILT_SOURCES += um-realm-generated.c um-realm-generated.h
 
 libgisaccount_la_SOURCES =				\
-	$(pw_utils_sources)				\
 	$(BUILT_SOURCES)				\
 	gis-account-page.c gis-account-page.h		\
 	um-realm-manager.c um-realm-manager.h		\
 	um-utils.c um-utils.h				\
+	pw-utils.c pw-utils.h				\
 	$(NULL)
 
 libgisaccount_la_CFLAGS = $(INITIAL_SETUP_CFLAGS) -I "$(srcdir)/../.."
diff --git a/gnome-initial-setup/pages/account/gis-account-page.c b/gnome-initial-setup/pages/account/gis-account-page.c
index 518b168..0162311 100644
--- a/gnome-initial-setup/pages/account/gis-account-page.c
+++ b/gnome-initial-setup/pages/account/gis-account-page.c
@@ -296,7 +296,7 @@ update_password_entries (GisAccountPage *page)
   verify = gtk_entry_get_text (GTK_ENTRY (confirm_entry));
   username = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (username_combo));
 
-  strength = pw_strength (password, NULL, username, &hint, &long_hint);
+  strength = pw_strength (password, NULL, username, &hint, &long_hint, NULL);
 
   if (strength == 0.0) {
     priv->valid_password = FALSE;
diff --git a/gnome-initial-setup/pages/account/pw-utils.c b/gnome-initial-setup/pages/account/pw-utils.c
index cdafa23..f859ba1 100644
--- a/gnome-initial-setup/pages/account/pw-utils.c
+++ b/gnome-initial-setup/pages/account/pw-utils.c
@@ -2,10 +2,10 @@
  *
  * Copyright 2012  Red Hat, Inc,
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -14,8 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  *
  * Written by: Matthias Clasen <mclasen redhat com>
  */
@@ -27,138 +26,102 @@
 #include <glib.h>
 #include <glib/gi18n.h>
 
-#include <sys/types.h>
-#include <sys/wait.h>
+#include <pwquality.h>
 
 
-#define MIN_PASSWORD_LEN 6
+static pwquality_settings_t *
+get_pwq (void)
+{
+        static pwquality_settings_t *settings;
+
+        if (settings == NULL) {
+                gchar *err = NULL;
+                settings = pwquality_default_settings ();
+                if (pwquality_read_config (settings, NULL, (gpointer)&err) < 0) {
+                        g_error ("failed to read pwquality configuration: %s\n", err);
+                }
+        }
+
+        return settings;
+}
 
 gint
 pw_min_length (void)
 {
-        return MIN_PASSWORD_LEN;
+        gint value = 0;
+
+        if (pwquality_get_int_value (get_pwq (), PWQ_SETTING_MIN_LENGTH, &value) < 0) {
+                g_error ("Failed to read pwquality setting\n" );
+        }
+
+        return value;
 }
 
 gchar *
 pw_generate (void)
 {
-        static gchar **generated = NULL;
-        static gint next;
-
-        gint min_len, max_len;
-        gchar *output, *err, *cmdline, *p;
-        gint status;
-        GError *error;
-        gchar *ret;
+        gchar *res;
+        gint rv;
 
-        if (generated && generated[next]) {
-                return g_strdup (generated[next++]);
-        }
+        rv = pwquality_generate (get_pwq (), 0, &res);
 
-        g_strfreev (generated);
-        generated = NULL;
-        next = 0;
-
-        ret = NULL;
-
-        min_len = 6;
-        max_len = 12;
-        cmdline = g_strdup_printf ("apg -n 10 -M SNC -m %d -x %d", min_len, max_len);
-        error = NULL;
-        output = NULL;
-        err = NULL;
-        if (!g_spawn_command_line_sync (cmdline, &output, &err, &status, &error)) {
-                g_warning ("Failed to run apg: %s", error->message);
-                g_error_free (error);
-        } else if (WEXITSTATUS (status) == 0) {
-                p = output;
-                if (*p == '\n')
-                        p++;
-                if (p[strlen(p) - 1] == '\n')
-                        p[strlen(p) - 1] = '\0';
-                generated = g_strsplit (p, "\n", -1);
-                next = 0;
-
-                ret = g_strdup (generated[next++]);
-        } else {
-                g_warning ("agp returned an error: %s", err);
+        if (rv < 0) {
+                g_error ("Password generation failed: %s\n",
+                         pwquality_strerror (NULL, 0, rv, NULL));
+                return NULL;
         }
 
-        g_free (cmdline);
-        g_free (output);
-        g_free (err);
-
-        return ret;
+        return res;
 }
 
-/* This code is based on the Master Password dialog in Firefox
- * (pref-masterpass.js)
- * Original code triple-licensed under the MPL, GPL, and LGPL
- * so is license-compatible with this file
- */
 gdouble
 pw_strength (const gchar  *password,
              const gchar  *old_password,
              const gchar  *username,
              const gchar **hint,
-             const gchar **long_hint)
+             const gchar **long_hint,
+             gint         *strength_level)
 {
-        gint length;
-        gint upper, lower, digit, misc;
-        gint i;
-        gdouble strength;
-
-        length = strlen (password);
-        upper = 0;
-        lower = 0;
-        digit = 0;
-        misc = 0;
-
-        if (length < MIN_PASSWORD_LEN) {
+        gint rv, level = 0;
+        gdouble strength = 0.0;
+        void *auxerror;
+
+        rv = pwquality_check (get_pwq (),
+                              password, old_password, username,
+                              &auxerror);
+
+        if (rv == PWQ_ERROR_MIN_LENGTH) {
                 *hint = C_("Password strength", "Too short");
-                return 0.0;
+                *long_hint = pwquality_strerror (NULL, 0, rv, auxerror);
+                goto out;
         }
-
-        for (i = 0; i < length ; i++) {
-                if (g_ascii_isdigit (password[i]))
-                        digit++;
-                else if (g_ascii_islower (password[i]))
-                        lower++;
-                else if (g_ascii_isupper (password[i]))
-                        upper++;
-                else
-                        misc++;
+        else if (rv < 0) {
+                *hint = C_("Password strength", "Not good enough");
+                *long_hint = pwquality_strerror (NULL, 0, rv, auxerror);
+                goto out;
         }
 
-        if (length > 5)
-                length = 5;
-
-        if (digit > 3)
-                digit = 3;
-
-        if (upper > 3)
-                upper = 3;
-
-        if (misc > 3)
-                misc = 3;
+        strength = CLAMP (0.01 * rv, 0.0, 1.0);
 
-        strength = ((length * 0.1) - 0.2) +
-                    (digit * 0.1) +
-                    (misc * 0.15) +
-                    (upper * 0.1);
-
-        strength = CLAMP (strength, 0.0, 1.0);
-
-        if (strength < 0.50)
+        if (strength < 0.50) {
+                level = 1;
                 *hint = C_("Password strength", "Weak");
-        else if (strength < 0.75)
+        } else if (strength < 0.75) {
+                level = 2;
                 *hint = C_("Password strength", "Fair");
-        else if (strength < 0.90)
+        } else if (strength < 0.90) {
+                level = 3;
                 *hint = C_("Password strength", "Good");
-        else
+        } else {
+                level = 4;
                 *hint = C_("Password strength", "Strong");
+        }
 
         *long_hint = NULL;
 
+ out:
+        if (strength_level)
+                *strength_level = level;
+
         return strength;
 }
diff --git a/gnome-initial-setup/pages/account/pw-utils.h b/gnome-initial-setup/pages/account/pw-utils.h
index fde331e..92214ef 100644
--- a/gnome-initial-setup/pages/account/pw-utils.h
+++ b/gnome-initial-setup/pages/account/pw-utils.h
@@ -2,10 +2,10 @@
  *
  * Copyright 2012  Red Hat, Inc,
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -14,8 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  *
  * Written by: Matthias Clasen <mclasen redhat com>
  */
@@ -28,4 +27,5 @@ gdouble  pw_strength   (const gchar  *password,
                         const gchar  *old_password,
                         const gchar  *username,
                         const gchar **hint,
-                        const gchar **long_hints);
+                        const gchar **long_hints,
+                        gint         *strength_level);



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