2 * Check a SQLite database for a password within edit distance one.
4 * This file implements yet another variation on dictionary lookups.
5 * Passwords are checked against a SQLite database (generally created with the
6 * krb5-strength-wordlist utility) that holds words and reversed words, and
7 * all passwords within edit distance one of a word in the database are
10 * To find passwords within edit distance one, this algorithm checks, for each
11 * dictionary word, whether the length of longest common prefix plus the
12 * length of the longest common suffix between that word and the password is
13 * within 1 of the length of the password. It will be one less if a letter
14 * has been removed or replaced, and equal if the password is an exact match.
16 * To do this, the SQLite database contains one row for each dictionary word,
17 * containing both the word and the reversed version of the word. The
18 * password is divided into two components, a prefix and a suffix. It is
19 * checked against all dictionary words that fall lexicographically between
20 * the prefix and the prefix with its last character incremented, and then
21 * against all words where the word reversed falls lexicographically between
22 * the suffix reversed and the suffix reversed with its last character
25 * If the password matches a dictionary word, the edit must either be in the
26 * first half of the password or the last half of the password. If in the
27 * first half, the word it will match will fall in the prefix range. If in
28 * the last half, the word it will match will fall in the suffix range.
30 * Written by Russ Allbery <eagle@eyrie.org>
31 * Based on work by David Mazières
33 * The Board of Trustees of the Leland Stanford Junior University
35 * See LICENSE for licensing terms.
39 #include <portable/kadmin.h>
40 #include <portable/krb5.h>
41 #include <portable/system.h>
47 #include <plugin/internal.h>
48 #include <util/macros.h>
51 * The prefix and suffix SQLite query. Finds all candidate words in range of
52 * the prefix or suffix. The prefix query should get bind variables for the
53 * prefix and the prefix with the last character incremented; the suffix query
54 * gets the same, but the suffix should be reversed.
56 #define PREFIX_QUERY \
57 "SELECT password, drowssap FROM passwords WHERE password BETWEEN ? AND ?;"
58 #define SUFFIX_QUERY \
59 "SELECT password, drowssap FROM passwords WHERE drowssap BETWEEN ? AND ?;"
63 * Stub for strength_init_sqlite if not built with SQLite support.
67 strength_init_sqlite(krb5_context ctx, krb5_pwqual_moddata data UNUSED)
71 /* Get CDB dictionary path from krb5.conf. */
72 strength_config_string(ctx, "password_dictionary_sqlite", &path);
74 /* If it was set, report an error, since we don't have CDB support. */
78 krb5_set_error_message(ctx, KADM5_BAD_SERVER_PARAMS, "SQLite dictionary"
79 " requested but not built with SQLite support");
80 return KADM5_BAD_SERVER_PARAMS;
85 /* Skip the rest of this file if SQLite is not available. */
89 * Report a SQLite error. Takes the SQLite error code and the Kerberos
90 * context, stores the resulting error in the Kerberos context, and returns
91 * the generic KADM5_FAILURE code, since there doesn't appear to be anything
94 static krb5_error_code
95 error_sqlite(krb5_context ctx, int status, const char *format, ...)
102 errstr = sqlite3_errstr(status);
103 va_start(args, format);
104 length = vasprintf(&message, format, args);
107 return strength_error_system(ctx, "cannot allocate memory");
108 krb5_set_error_message(ctx, KADM5_FAILURE, "%s: %s", message, errstr);
110 return KADM5_FAILURE;
115 * Given a string, returns a reversed version of that string in newly
116 * allocated memory. The caller is responsible for freeing. Returns NULL on
117 * memory allocation failure.
120 reverse_string(const char *string)
125 length = strlen(string);
126 reversed = malloc(length + 1);
127 if (reversed == NULL)
129 reversed[length] = '\0';
130 for (i = 0; i < length; i++)
131 reversed[length - i - 1] = string[i];
137 * Given two strings, return the length of their common prefix, not counting
138 * the nul character that terminates either string.
141 common_prefix_length(const char *a, const char *b)
145 for (i = 0; a[i] == b[i] && a[i] != '\0' && b[i] != '\0'; i++)
152 * Given the length of the password, the password, the reversed password, and
153 * an executed SQLite statement that contains the word and reversed word as
154 * the first two column texts, determine whether this password is a match
155 * within edit distance one.
157 * It will be a match if the length of the common prefix of the passwod and
158 * word plus the length of the common prefix of the reversed password and the
159 * reversed word (which is the length of the common suffix) is within 1 of the
160 * length of the password.
163 match(size_t length, const char *password, const char *drowssap,
166 const char *word, *drow;
167 size_t prefix_length, suffix_length;
169 word = (const char *) sqlite3_column_text(query, 0);
170 drow = (const char *) sqlite3_column_text(query, 1);
171 prefix_length = common_prefix_length(password, word);
172 if (prefix_length == length)
174 suffix_length = common_prefix_length(drowssap, drow);
175 return (length - prefix_length - suffix_length <= 1);
180 * Initialize the SQLite dictionary. Opens the database and compiles the two
181 * queries that we'll use. Returns 0 on success, non-zero on failure (and
182 * sets the error in the Kerberos context).
185 strength_init_sqlite(krb5_context ctx, krb5_pwqual_moddata data)
190 /* Get SQLite dictionary path from krb5.conf. */
191 strength_config_string(ctx, "password_dictionary_sqlite", &path);
193 /* If there is no configured dictionary, nothing to do. */
197 /* Open the database. */
198 status = sqlite3_open_v2(path, &data->sqlite, SQLITE_OPEN_READONLY, NULL);
200 return error_sqlite(ctx, status, "cannot open dictionary %s", path);
202 /* Precompile the queries we'll use. */
203 status = sqlite3_prepare_v2(data->sqlite, PREFIX_QUERY, -1,
204 &data->prefix_query, NULL);
206 return error_sqlite(ctx, status, "cannot prepare prefix query");
207 status = sqlite3_prepare_v2(data->sqlite, SUFFIX_QUERY, -1,
208 &data->suffix_query, NULL);
210 return error_sqlite(ctx, status, "cannot prepare suffix query");
212 /* Finished. Return success. */
218 * Given a password, look for a word in the database within edit distance one.
219 * The full algorithm used here is described in the comment at the start of
220 * this file. Returns a Kerberos status code, which will be KADM5_PASS_Q_DICT
221 * if the password was found in the dictionary.
224 strength_check_sqlite(krb5_context ctx, krb5_pwqual_moddata data,
225 const char *password)
227 krb5_error_code code;
228 size_t length, prefix_length, suffix_length;
230 char *drowssap = NULL;
234 /* If we have no dictionary, there is nothing to do. */
235 if (data->sqlite == NULL)
239 * Determine the length of the prefix and suffix into which we'll divide
240 * the string. Passwords shorter than two characters cannot be
241 * meaningfully checked using this method and cause boundary condition
244 length = strlen(password);
247 prefix_length = length / 2;
248 suffix_length = length - prefix_length;
250 /* Obtain the reversed password, used for suffix checks. */
251 drowssap = reverse_string(password);
252 if (drowssap == NULL)
253 return strength_error_system(ctx, "cannot allocate memory");
255 /* Set up the query for prefix matching. */
256 prefix = strdup(password);
257 if (prefix == NULL) {
258 code = strength_error_system(ctx, "cannot allocate memory");
261 status = sqlite3_bind_text(data->prefix_query, 1, password, prefix_length,
263 if (status != SQLITE_OK) {
264 code = error_sqlite(ctx, status, "cannot bind prefix start");
267 prefix[prefix_length - 1]++;
268 status = sqlite3_bind_text(data->prefix_query, 2, prefix, prefix_length,
270 if (status != SQLITE_OK) {
271 code = error_sqlite(ctx, status, "cannot bind prefix end");
276 * Do prefix matching. Get the set of all database entries starting with
277 * the same prefix and, for each, check whether our password matches that
278 * entry within edit distance one.
280 while ((status = sqlite3_step(data->prefix_query)) == SQLITE_ROW)
281 if (match(length, password, drowssap, data->prefix_query)) {
285 if (status != SQLITE_DONE && status != SQLITE_ROW) {
286 code = error_sqlite(ctx, status, "error searching by password prefix");
289 status = sqlite3_reset(data->prefix_query);
290 if (status != SQLITE_OK) {
291 code = error_sqlite(ctx, status, "error resetting prefix query");
297 /* Set up the query for suffix matching. */
298 status = sqlite3_bind_text(data->suffix_query, 1, drowssap, suffix_length,
300 if (status != SQLITE_OK) {
301 code = error_sqlite(ctx, status, "cannot bind suffix start");
304 drowssap[prefix_length - 1]++;
305 status = sqlite3_bind_text(data->suffix_query, 2, drowssap, suffix_length,
307 drowssap[prefix_length - 1]--;
308 if (status != SQLITE_OK) {
309 code = error_sqlite(ctx, status, "cannot bind suffix end");
314 * Do suffix matching. Get the set of all database entries starting with
315 * the same prefix and, for each, check whether our password matches that
316 * entry within edit distance one.
318 while ((status = sqlite3_step(data->suffix_query)) == SQLITE_ROW)
319 if (match(length, password, drowssap, data->suffix_query)) {
323 if (status != SQLITE_DONE && status != SQLITE_ROW) {
324 code = error_sqlite(ctx, status, "error searching by password suffix");
327 status = sqlite3_reset(data->suffix_query);
328 if (status != SQLITE_OK) {
329 code = error_sqlite(ctx, status, "error resetting suffix query");
335 /* No match. Clean up and return success. */
336 memset(prefix, 0, length);
337 memset(drowssap, 0, length);
343 /* We found the password in the dictionary. */
344 code = strength_error_dict(ctx, ERROR_DICT);
347 memset(prefix, 0, length);
348 memset(drowssap, 0, length);
356 * Free internal SQLite state and close the SQLite database.
359 strength_close_sqlite(krb5_context ctx UNUSED, krb5_pwqual_moddata data)
361 if (data->prefix_query != NULL)
362 sqlite3_finalize(data->prefix_query);
363 if (data->suffix_query != NULL)
364 sqlite3_finalize(data->suffix_query);
365 if (data->sqlite != NULL)
366 sqlite3_close_v2(data->sqlite);
369 #endif /* HAVE_CDB */