2 * The general entry points for password strength checking.
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.
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, 2014
13 * The Board of Trustees of the Leland Stanford Junior University
15 * See LICENSE for licensing terms.
19 #include <portable/krb5.h>
20 #include <portable/system.h>
24 #include <plugin/internal.h>
25 #include <util/macros.h>
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.
34 * The dictionary file should not include the trailing .pwd extension.
35 * Currently, we don't cope with a NULL dictionary path.
38 strength_init(krb5_context ctx, const char *dictionary,
39 krb5_pwqual_moddata *moddata)
41 krb5_pwqual_moddata data = NULL;
44 /* Allocate our internal data. */
45 data = calloc(1, sizeof(*data));
47 return strength_error_system(ctx, "cannot allocate memory");
50 /* Get minimum length and character information from krb5.conf. */
51 strength_config_number(ctx, "minimum_different", &data->minimum_different);
52 strength_config_number(ctx, "minimum_length", &data->minimum_length);
54 /* Get simple character class restrictions from krb5.conf. */
55 strength_config_boolean(ctx, "require_ascii_printable", &data->ascii);
56 strength_config_boolean(ctx, "require_non_letter", &data->nonletter);
58 /* Get complex character class restrictions from krb5.conf. */
59 code = strength_config_classes(ctx, "require_classes", &data->rules);
64 * Try to initialize CDB and CrackLib dictionaries. Both functions handle
65 * their own configuration parsing and will do nothing if the
66 * corresponding dictionary is not configured.
68 code = strength_init_cracklib(ctx, data, dictionary);
71 code = strength_init_cdb(ctx, data);
75 /* Initialized. Set moddata and return. */
81 strength_close(ctx, data);
88 * Check if a password contains only printable ASCII characters.
91 only_printable_ascii(const char *password)
95 for (p = password; *p != '\0'; p++)
96 if (!isascii((unsigned char) *p) || !isprint((unsigned char) *p))
103 * Check if a password contains only letters and spaces.
106 only_alpha_space(const char *password)
110 for (p = password; *p != '\0'; p++)
111 if (!isalpha((unsigned char) *p) && *p != ' ')
118 * Check if a password has a sufficient number of unique characters. Takes
119 * the password and the required number of characters.
122 has_minimum_different(const char *password, long minimum)
127 /* Special cases for passwords of length 0 and a minimum <= 1. */
128 if (password == NULL || password[0] == '\0')
134 * Count the number of unique characters by incrementing the count if each
135 * subsequent character is not found in the previous password characters.
136 * This algorithm is O(n^2), but passwords are short enough it shouldn't
140 for (p = password + 1; *p != '\0'; p++)
141 if (memchr(password, *p, p - password) == NULL) {
143 if (unique >= (size_t) minimum)
151 * Check a given password. Takes a Kerberos context, our module data, the
152 * password, the principal the password is for, and a buffer and buffer length
153 * into which to put any failure message.
156 strength_check(krb5_context ctx UNUSED, krb5_pwqual_moddata data,
157 const char *principal, const char *password)
159 krb5_error_code code;
161 /* Check minimum length first, since that's easy. */
162 if ((long) strlen(password) < data->minimum_length)
163 return strength_error_tooshort(ctx, ERROR_SHORT);
166 * If desired, check whether the password contains non-ASCII or
167 * non-printable ASCII characters.
169 if (data->ascii && !only_printable_ascii(password))
170 return strength_error_generic(ctx, ERROR_ASCII);
173 * If desired, ensure the password has a non-letter (and non-space)
174 * character. This requires that people using phrases at least include a
175 * digit or punctuation to make phrase dictionary attacks or dictionary
176 * attacks via combinations of words harder.
178 if (data->nonletter && only_alpha_space(password))
179 return strength_error_class(ctx, ERROR_LETTER);
181 /* If desired, check for enough unique characters. */
182 if (data->minimum_different > 0)
183 if (!has_minimum_different(password, data->minimum_different))
184 return strength_error_class(ctx, ERROR_MINDIFF);
187 * If desired, check that the password satisfies character class
190 code = strength_check_classes(ctx, data, password);
194 /* Check if the password is based on the principal in some way. */
195 code = strength_check_principal(ctx, data, principal, password);
199 /* Check the password against CDB and CrackLib if configured. */
200 code = strength_check_cracklib(ctx, data, password);
203 code = strength_check_cdb(ctx, data, password);
207 /* Success. Password accepted. */
213 * Cleanly shut down the password strength plugin. The only thing we have to
214 * do is free the memory allocated for our internal data.
217 strength_close(krb5_context ctx UNUSED, krb5_pwqual_moddata data)
219 struct class_rule *last, *tmp;
223 strength_close_cdb(ctx, data);
225 while (last != NULL) {
230 free(data->dictionary);