~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/user-accounts/pw-utils.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright 2012  Red Hat, Inc,
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 * Written by: Matthias Clasen <mclasen@redhat.com>
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include "pw-utils.h"
 
25
 
 
26
#include <glib.h>
 
27
#include <glib/gi18n.h>
 
28
 
 
29
#include <pwquality.h>
 
30
 
 
31
 
 
32
static pwquality_settings_t *
 
33
get_pwq (void)
 
34
{
 
35
        static pwquality_settings_t *settings;
 
36
 
 
37
        if (settings == NULL) {
 
38
                gchar *err = NULL;
 
39
                settings = pwquality_default_settings ();
 
40
                if (pwquality_read_config (settings, NULL, (gpointer)&err) < 0) {
 
41
                        g_error ("failed to read pwquality configuration: %s\n", err);
 
42
                }
 
43
        }
 
44
 
 
45
        return settings;
 
46
}
 
47
 
 
48
gint
 
49
pw_min_length (void)
 
50
{
 
51
        gint value = 0;
 
52
 
 
53
        if (pwquality_get_int_value (get_pwq (), PWQ_SETTING_MIN_LENGTH, &value) < 0) {
 
54
                g_error ("Failed to read pwquality setting\n" );
 
55
        }
 
56
 
 
57
        return value;
 
58
}
 
59
 
 
60
gchar *
 
61
pw_generate (void)
 
62
{
 
63
        gchar *res;
 
64
        gint rv;
 
65
 
 
66
        rv = pwquality_generate (get_pwq (), 0, &res);
 
67
 
 
68
        if (rv < 0) {
 
69
                g_error ("Password generation failed: %s\n",
 
70
                         pwquality_strerror (NULL, 0, rv, NULL));
 
71
                return NULL;
 
72
        }
 
73
 
 
74
        return res;
 
75
}
 
76
 
 
77
gdouble
 
78
pw_strength (const gchar  *password,
 
79
             const gchar  *old_password,
 
80
             const gchar  *username,
 
81
             const gchar **hint,
 
82
             const gchar **long_hint,
 
83
             gint         *strength_level)
 
84
{
 
85
        gint rv, level = 0;
 
86
        gdouble strength = 0.0;
 
87
        void *auxerror;
 
88
 
 
89
        rv = pwquality_check (get_pwq (),
 
90
                              password, old_password, username,
 
91
                              &auxerror);
 
92
 
 
93
        if (rv == PWQ_ERROR_MIN_LENGTH) {
 
94
                *hint = C_("Password strength", "Too short");
 
95
                *long_hint = pwquality_strerror (NULL, 0, rv, auxerror);
 
96
                goto out;
 
97
        }
 
98
        else if (rv < 0) {
 
99
                *hint = C_("Password strength", "Not good enough");
 
100
                *long_hint = pwquality_strerror (NULL, 0, rv, auxerror);
 
101
                goto out;
 
102
        }
 
103
 
 
104
        strength = CLAMP (0.01 * rv, 0.0, 1.0);
 
105
 
 
106
        if (strength < 0.50) {
 
107
                level = 1;
 
108
                *hint = C_("Password strength", "Weak");
 
109
        } else if (strength < 0.75) {
 
110
                level = 2;
 
111
                *hint = C_("Password strength", "Fair");
 
112
        } else if (strength < 0.90) {
 
113
                level = 3;
 
114
                *hint = C_("Password strength", "Good");
 
115
        } else {
 
116
                level = 4;
 
117
                *hint = C_("Password strength", "Strong");
 
118
        }
 
119
 
 
120
        *long_hint = NULL;
 
121
 
 
122
 out:
 
123
        if (strength_level)
 
124
                *strength_level = level;
 
125
 
 
126
        return strength;
 
127
}