[gdm/wip/initial-setup2] Add password checking



commit c1d91239acb37aa6ec3bc3c0150e3198d4e039a7
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat May 19 19:54:21 2012 -0400

    Add password checking

 gui/initial-setup/gdm-initial-setup.c |   14 +++-
 gui/initial-setup/pw-utils.c          |  163 +++++++++++++++++++++++++++++++++
 gui/initial-setup/pw-utils.h          |   30 ++++++
 3 files changed, 205 insertions(+), 2 deletions(-)
---
diff --git a/gui/initial-setup/gdm-initial-setup.c b/gui/initial-setup/gdm-initial-setup.c
index 1850a6f..1d93f09 100644
--- a/gui/initial-setup/gdm-initial-setup.c
+++ b/gui/initial-setup/gdm-initial-setup.c
@@ -22,6 +22,7 @@
 #include "timedated.h"
 #include "um-utils.h"
 #include "um-photo-dialog.h"
+#include "pw-utils.h"
 #include "gdm-greeter-client.h"
 
 #define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
@@ -946,16 +947,25 @@ update_password_entries (SetupData *setup)
 {
         const gchar *password;
         const gchar *verify;
+        const gchar *username;
         GtkWidget *password_entry;
         GtkWidget *confirm_entry;
+        GtkWidget *username_combo;
+        gdouble strength;
+        const gchar *hint;
+        const gchar *long_hint;
 
         password_entry = WID("account-password-entry");
         confirm_entry = WID("account-confirm-entry");
+        username_combo = WID("account-username-combo");
+
         password = gtk_entry_get_text (GTK_ENTRY (password_entry));
         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);
 
-        /* TODO: configurable policies for acceptable passwords */
-        if (strlen (password) < MIN_PASSWORD_LEN) {
+        if (strength == 0.0) {
                 setup->valid_password = FALSE;
         }
         else if (strcmp (password, verify) != 0) {
diff --git a/gui/initial-setup/pw-utils.c b/gui/initial-setup/pw-utils.c
new file mode 100644
index 0000000..ee8acec
--- /dev/null
+++ b/gui/initial-setup/pw-utils.c
@@ -0,0 +1,163 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * 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 3 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
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Written by: Matthias Clasen <mclasen redhat com>
+ */
+
+#include "config.h"
+
+#include "pw-utils.h"
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+
+#define MIN_PASSWORD_LEN 6
+
+gint
+pw_min_length (void)
+{
+        return MIN_PASSWORD_LEN;
+}
+
+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;
+
+        if (generated && generated[next]) {
+                return g_strdup (generated[next++]);
+        }
+
+        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);
+        }
+
+        g_free (cmdline);
+        g_free (output);
+        g_free (err);
+
+        return ret;
+}
+
+/* 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)
+{
+        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) {
+                *hint = C_("Password strength", "Too short");
+                return 0.0;
+        }
+
+        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++;
+        }
+
+        if (length > 5)
+                length = 5;
+
+        if (digit > 3)
+                digit = 3;
+
+        if (upper > 3)
+                upper = 3;
+
+        if (misc > 3)
+                misc = 3;
+
+        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)
+                *hint = C_("Password strength", "Weak");
+        else if (strength < 0.75)
+                *hint = C_("Password strength", "Fair");
+        else if (strength < 0.90)
+                *hint = C_("Password strength", "Good");
+        else
+                *hint = C_("Password strength", "Strong");
+
+        *long_hint = NULL;
+
+        return strength;
+}
diff --git a/gui/initial-setup/pw-utils.h b/gui/initial-setup/pw-utils.h
new file mode 100644
index 0000000..a88ae34
--- /dev/null
+++ b/gui/initial-setup/pw-utils.h
@@ -0,0 +1,30 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * 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 3 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
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Written by: Matthias Clasen <mclasen redhat com>
+ */
+
+#include <glib.h>
+
+gint     pw_min_length (void);
+gchar   *pw_generate   (void);
+gdouble  pw_strength   (const gchar  *password,
+                        const gchar  *old_password,
+                        const gchar  *username,
+                        const gchar **hint,
+                        const gchar **long_hints);



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