]> eyrie.org Git - kerberos/krb5-strength.git/blob - plugin/general.c
Fix various character class check mistakes, add test suite
[kerberos/krb5-strength.git] / plugin / general.c
1 /*
2  * The general entry points for password strength checking.
3  *
4  * Provides the strength_init, strength_check, and strength_close entry points
5  * for doing password strength checking.  These are the only interfaces that
6  * are called by the implementation-specific code, and all other checks are
7  * wrapped up in those interfaces.
8  *
9  * Developed by Derrick Brashear and Ken Hornstein of Sine Nomine Associates,
10  *     on behalf of Stanford University
11  * Extensive modifications by Russ Allbery <eagle@eyrie.org>
12  * Copyright 2006, 2007, 2009, 2012, 2013
13  *     The Board of Trustees of the Leland Stanford Junior University
14  *
15  * See LICENSE for licensing terms.
16  */
17
18 #include <config.h>
19 #include <portable/krb5.h>
20 #include <portable/system.h>
21
22 #include <ctype.h>
23
24 #include <plugin/internal.h>
25 #include <util/macros.h>
26
27
28 /*
29  * Initialize the module.  Ensure that the dictionary file exists and is
30  * readable and store the path in the module context.  Returns 0 on success,
31  * non-zero on failure.  This function returns failure only if it could not
32  * allocate memory or internal Kerberos calls that shouldn't fail do.
33  *
34  * The dictionary file should not include the trailing .pwd extension.
35  * Currently, we don't cope with a NULL dictionary path.
36  */
37 krb5_error_code
38 strength_init(krb5_context ctx, const char *dictionary,
39               krb5_pwqual_moddata *moddata)
40 {
41     krb5_pwqual_moddata data = NULL;
42     krb5_error_code code;
43
44     /* Allocate our internal data. */
45     data = calloc(1, sizeof(*data));
46     if (data == NULL)
47         return strength_error_system(ctx, "cannot allocate memory");
48     data->cdb_fd = -1;
49
50     /* Get minimum length information from krb5.conf. */
51     strength_config_number(ctx, "minimum_length", &data->minimum_length);
52
53     /* Get simple character class restrictions from krb5.conf. */
54     strength_config_boolean(ctx, "require_ascii_printable", &data->ascii);
55     strength_config_boolean(ctx, "require_non_letter", &data->nonletter);
56
57     /* Get complex character class restrictions from krb5.conf. */
58     code = strength_config_classes(ctx, "require_classes", &data->rules);
59     if (code != 0)
60         goto fail;
61
62     /*
63      * Try to initialize CDB and CrackLib dictionaries.  Both functions handle
64      * their own configuration parsing and will do nothing if the
65      * corresponding dictionary is not configured.
66      */
67     code = strength_init_cracklib(ctx, data, dictionary);
68     if (code != 0)
69         goto fail;
70     code = strength_init_cdb(ctx, data);
71     if (code != 0)
72         goto fail;
73
74     /* Initialized.  Set moddata and return. */
75     *moddata = data;
76     return 0;
77
78 fail:
79     if (data != NULL)
80         strength_close(ctx, data);
81     *moddata = NULL;
82     return code;
83 }
84
85
86 /*
87  * Check if a password contains only printable ASCII characters.
88  */
89 static bool
90 only_printable_ascii(const char *password)
91 {
92     const char *p;
93
94     for (p = password; *p != '\0'; p++)
95         if (!isascii((unsigned char) *p) || !isprint((unsigned char) *p))
96             return false;
97     return true;
98 }
99
100
101 /*
102  * Check if a password contains only letters and spaces.
103  */
104 static bool
105 only_alpha_space(const char *password)
106 {
107     const char *p;
108
109     for (p = password; *p != '\0'; p++)
110         if (!isalpha((unsigned char) *p) && *p != ' ')
111             return false;
112     return true;
113 }
114
115
116 /*
117  * Check a given password.  Takes a Kerberos context, our module data, the
118  * password, the principal the password is for, and a buffer and buffer length
119  * into which to put any failure message.
120  */
121 krb5_error_code
122 strength_check(krb5_context ctx UNUSED, krb5_pwqual_moddata data,
123                const char *principal, const char *password)
124 {
125     krb5_error_code code;
126
127     /* Check minimum length first, since that's easy. */
128     if ((long) strlen(password) < data->minimum_length)
129         return strength_error_tooshort(ctx, ERROR_SHORT);
130
131     /*
132      * If desired, check whether the password contains non-ASCII or
133      * non-printable ASCII characters.
134      */
135     if (data->ascii && !only_printable_ascii(password))
136         return strength_error_generic(ctx, ERROR_ASCII);
137
138     /*
139      * If desired, ensure the password has a non-letter (and non-space)
140      * character.  This requires that people using phrases at least include a
141      * digit or punctuation to make phrase dictionary attacks or dictionary
142      * attacks via combinations of words harder.
143      */
144     if (data->nonletter && only_alpha_space(password))
145         return strength_error_class(ctx, ERROR_LETTER);
146
147     /*
148      * If desired, check that the password satisfies character class
149      * restrictions.
150      */
151     code = strength_check_classes(ctx, data, password);
152     if (code != 0)
153         return code;
154
155     /* Check if the password is based on the principal in some way. */
156     code = strength_check_principal(ctx, data, principal, password);
157     if (code != 0)
158         return code;
159
160     /* Check the password against CDB and CrackLib if configured. */
161     code = strength_check_cracklib(ctx, data, password);
162     if (code != 0)
163         return code;
164     code = strength_check_cdb(ctx, data, password);
165     if (code != 0)
166         return code;
167
168     /* Success.  Password accepted. */
169     return 0;
170 }
171
172
173 /*
174  * Cleanly shut down the password strength plugin.  The only thing we have to
175  * do is free the memory allocated for our internal data.
176  */
177 void
178 strength_close(krb5_context ctx UNUSED, krb5_pwqual_moddata data)
179 {
180     if (data != NULL) {
181         strength_close_cdb(ctx, data);
182         free(data->dictionary);
183         free(data);
184     }
185 }