2 * Test for the MIT Kerberos shared module API.
4 * Written by Russ Allbery <eagle@eyrie.org>
5 * Copyright 2017, 2020 Russ Allbery <eagle@eyrie.org>
6 * Copyright 2010, 2013, 2014
7 * The Board of Trustees of the Leland Stanford Junior University
9 * See LICENSE for licensing terms.
13 #include <portable/kadmin.h>
14 #include <portable/krb5.h>
15 #include <portable/system.h>
19 #ifdef HAVE_KRB5_PWQUAL_PLUGIN_H
20 # include <krb5/pwqual_plugin.h>
23 #include <tests/tap/basic.h>
24 #include <tests/tap/kerberos.h>
25 #include <tests/tap/process.h>
26 #include <tests/tap/string.h>
27 #include <util/macros.h>
30 * The password test data, generated from the JSON source. Defines arrays
31 * named *_tests, where * is the file name without the ".c" suffix.
33 #include <tests/data/passwords/cdb.c>
34 #include <tests/data/passwords/classes.c>
35 #include <tests/data/passwords/cracklib.c>
36 #include <tests/data/passwords/length.c>
37 #include <tests/data/passwords/letter.c>
38 #include <tests/data/passwords/principal.c>
39 #include <tests/data/passwords/sqlite.c>
42 #ifndef HAVE_KRB5_PWQUAL_PLUGIN_H
44 * If we're not building with MIT Kerberos, we can't run this test and much of
45 * the test won't even compile. Replace this test with a small program that
46 * just calls skip_all.
51 skip_all("not built against MIT libraries");
57 /* The public symbol that we load and call to get the vtable. */
58 typedef krb5_error_code pwqual_strength_initvt(krb5_context, int, int,
63 * Loads the Heimdal password change plugin and tests that its metadata is
64 * correct. Returns a pointer to the kadm5_pw_policy_verifier struct or bails
65 * on failure to load the plugin. Stores the handle from dlopen in its second
66 * argument for a later clean shutdown.
68 static krb5_pwqual_vtable
69 load_plugin(krb5_context ctx, void **handle)
73 krb5_pwqual_vtable vtable = NULL;
74 krb5_error_code (*init)(krb5_context, int, int, krb5_plugin_vtable);
76 /* Load the module. */
77 path = test_file_path("../plugin/.libs/strength.so");
79 bail("cannot find plugin");
80 *handle = dlopen(path, RTLD_NOW);
82 bail("cannot dlopen %s: %s", path, dlerror());
83 test_file_path_free(path);
85 /* Find the entry point function. */
86 init = dlsym(*handle, "pwqual_strength_initvt");
88 bail("cannot get pwqual_strength_initvt symbol: %s", dlerror());
90 /* Test for correct results when requesting the wrong API version. */
91 code = init(ctx, 2, 0, (krb5_plugin_vtable) vtable);
92 is_int(code, KRB5_PLUGIN_VER_NOTSUPP,
93 "Correct status for bad major API version");
95 /* Call that function properly to get the vtable. */
96 vtable = bmalloc(sizeof(*vtable));
97 code = init(ctx, 1, 1, (krb5_plugin_vtable) vtable);
99 bail_krb5(ctx, code, "cannot obtain module vtable");
101 /* Check that all of the vtable entries are present. */
102 if (vtable->open == NULL || vtable->check == NULL || vtable->close == NULL)
103 bail("missing function in module vtable");
105 /* Verify the metadata. */
106 is_string("krb5-strength", vtable->name, "Module name");
108 /* Return the vtable. */
114 * Given a Kerberos context, the dispatch table, the module data, and a test
115 * case, call out to the password strength checking module and check the
119 is_password_test(krb5_context ctx, const krb5_pwqual_vtable vtable,
120 krb5_pwqual_moddata data, const struct password_test *test)
122 krb5_principal princ;
123 krb5_error_code code;
126 /* Translate the principal into a krb5_principal. */
127 code = krb5_parse_name(ctx, test->principal, &princ);
129 bail_krb5(ctx, code, "cannot parse principal %s", test->principal);
131 /* Call the verifier. */
132 code = vtable->check(ctx, data, test->password, NULL, princ, NULL);
134 /* Check the results against the test data. */
135 is_int(test->code, code, "%s (status)", test->name);
137 is_string(test->error, NULL, "%s (error)", test->name);
139 error = krb5_get_error_message(ctx, code);
140 is_string(test->error, error, "%s (error)", test->name);
141 krb5_free_error_message(ctx, error);
144 /* Free the parsed principal. */
145 krb5_free_principal(ctx, princ);
152 char *path, *dictionary, *krb5_config, *krb5_config_empty, *tmpdir;
153 char *setup_argv[12];
157 krb5_pwqual_vtable vtable;
158 krb5_pwqual_moddata data;
159 krb5_error_code code;
163 * Calculate how many tests we have. There are two tests for the module
164 * metadata, seven more tests for initializing the plugin, and two tests
167 * We run all the CrackLib tests twice, once with an explicit dictionary
168 * path and once from krb5.conf configuration. We run the principal tests
169 * with CrackLib, CDB, and SQLite configurations.
171 count = 2 * ARRAY_SIZE(cracklib_tests);
172 count += 2 * ARRAY_SIZE(length_tests);
173 count += ARRAY_SIZE(cdb_tests);
174 count += ARRAY_SIZE(sqlite_tests);
175 count += ARRAY_SIZE(classes_tests);
176 count += ARRAY_SIZE(letter_tests);
177 count += 3 * ARRAY_SIZE(principal_tests);
178 plan(2 + 8 + count * 2);
180 /* Start with the krb5.conf that contains no dictionary configuration. */
181 path = test_file_path("data/krb5.conf");
183 bail("cannot find data/krb5.conf in the test suite");
184 basprintf(&krb5_config_empty, "KRB5_CONFIG=%s", path);
185 putenv(krb5_config_empty);
187 /* Obtain a Kerberos context with that krb5.conf file. */
188 code = krb5_init_context(&ctx);
190 bail_krb5(ctx, code, "cannot initialize Kerberos context");
192 /* Load the plugin. */
193 vtable = load_plugin(ctx, &handle);
195 /* Initialize the plugin with a CrackLib dictionary. */
196 build = getenv("BUILD");
198 bail("BUILD not set in the environment");
199 basprintf(&dictionary, "%s/data/dictionary", build);
200 code = vtable->open(ctx, dictionary, &data);
201 is_int(0, code, "Plugin initialization (explicit dictionary)");
203 bail("cannot continue after plugin initialization failure");
205 /* Run the principal tests. */
206 for (i = 0; i < ARRAY_SIZE(principal_tests); i++)
207 is_password_test(ctx, vtable, data, &principal_tests[i]);
209 # ifdef HAVE_CRACKLIB
210 /* Run the CrackLib tests if CrackLib is available, otherwise skip them. */
211 for (i = 0; i < ARRAY_SIZE(cracklib_tests); i++)
212 is_password_test(ctx, vtable, data, &cracklib_tests[i]);
214 count = ARRAY_SIZE(cracklib_tests);
215 skip_block(count * 2, "not built with CrackLib support");
218 /* Close that initialization of the plugin and destroy that context. */
219 vtable->close(ctx, data);
220 krb5_free_context(ctx);
223 /* Set up our krb5.conf with a base configuration. */
224 tmpdir = test_tmpdir();
225 setup_argv[0] = test_file_path("data/make-krb5-conf");
226 if (setup_argv[0] == NULL)
227 bail("cannot find data/make-krb5-conf in the test suite");
228 setup_argv[1] = path;
229 setup_argv[2] = tmpdir;
230 setup_argv[3] = NULL;
232 /* Point KRB5_CONFIG at the newly-generated krb5.conf file. */
233 basprintf(&krb5_config, "KRB5_CONFIG=%s/krb5.conf", tmpdir);
235 free(krb5_config_empty);
237 # ifdef HAVE_CRACKLIB
239 /* Add CrackLib configuration. */
240 setup_argv[3] = (char *) "password_dictionary";
241 setup_argv[4] = dictionary;
242 setup_argv[5] = NULL;
243 run_setup((const char **) setup_argv);
245 /* Obtain a new Kerberos context with that krb5.conf file. */
246 krb5_free_context(ctx);
247 code = krb5_init_context(&ctx);
249 bail_krb5(ctx, code, "cannot initialize Kerberos context");
251 /* Run all of the tests again. No need to re-run principal tests. */
252 code = vtable->open(ctx, NULL, &data);
253 is_int(0, code, "Plugin initialization (krb5.conf dictionary)");
255 bail("cannot continue after plugin initialization failure");
256 for (i = 0; i < ARRAY_SIZE(cracklib_tests); i++)
257 is_password_test(ctx, vtable, data, &cracklib_tests[i]);
258 vtable->close(ctx, data);
261 * Add length restrictions and a maximum length for CrackLib. This should
262 * reject passwords as too short, but let through a password that's
263 * actually in the CrackLib dictionary.
265 setup_argv[5] = (char *) "minimum_length";
266 setup_argv[6] = (char *) "12";
267 setup_argv[7] = (char *) "cracklib_maxlen";
268 setup_argv[8] = (char *) "11";
269 setup_argv[9] = NULL;
270 run_setup((const char **) setup_argv);
272 /* Obtain a new Kerberos context with that krb5.conf file. */
273 krb5_free_context(ctx);
274 code = krb5_init_context(&ctx);
276 bail_krb5(ctx, code, "cannot initialize Kerberos context");
278 /* Run all of the length tests. */
279 code = vtable->open(ctx, NULL, &data);
280 is_int(0, code, "Plugin initialization (length)");
282 bail("cannot continue after plugin initialization failure");
283 for (i = 0; i < ARRAY_SIZE(length_tests); i++)
284 is_password_test(ctx, vtable, data, &length_tests[i]);
285 vtable->close(ctx, data);
289 /* Otherwise mark the CrackLib tests as skipped. */
290 count = ARRAY_SIZE(cracklib_tests) + ARRAY_SIZE(length_tests);
291 skip_block(count * 2 + 1, "not built with CrackLib support");
293 # endif /* !HAVE_CRACKLIB */
295 /* Switch to simple character class configuration in krb5.conf. */
296 setup_argv[3] = (char *) "minimum_different";
297 setup_argv[4] = (char *) "8";
298 setup_argv[5] = (char *) "require_ascii_printable";
299 setup_argv[6] = (char *) "true";
300 setup_argv[7] = (char *) "require_non_letter";
301 setup_argv[8] = (char *) "true";
302 setup_argv[9] = NULL;
303 run_setup((const char **) setup_argv);
305 /* Obtain a new Kerberos context with that krb5.conf file. */
306 krb5_free_context(ctx);
307 code = krb5_init_context(&ctx);
309 bail_krb5(ctx, code, "cannot initialize Kerberos context");
311 /* Run all the simple character class tests. */
312 code = vtable->open(ctx, NULL, &data);
313 is_int(0, code, "Plugin initialization (simple character class)");
315 bail("cannot continue after plugin initialization failure");
316 for (i = 0; i < ARRAY_SIZE(letter_tests); i++)
317 is_password_test(ctx, vtable, data, &letter_tests[i]);
318 vtable->close(ctx, data);
320 /* Add complex character class configuration to krb5.conf. */
321 setup_argv[3] = (char *) "require_classes";
322 setup_argv[4] = (char *) "8-19:lower,upper 8-15:digit 8-11:symbol 24-24:3";
323 setup_argv[5] = NULL;
324 run_setup((const char **) setup_argv);
326 /* Obtain a new Kerberos context with that krb5.conf file. */
327 krb5_free_context(ctx);
328 code = krb5_init_context(&ctx);
330 bail_krb5(ctx, code, "cannot initialize Kerberos context");
332 /* Run all the complex character class tests. */
333 code = vtable->open(ctx, NULL, &data);
334 is_int(0, code, "Plugin initialization (complex character class)");
336 bail_krb5(ctx, code, "plugin initialization failure");
337 for (i = 0; i < ARRAY_SIZE(classes_tests); i++)
338 is_password_test(ctx, vtable, data, &classes_tests[i]);
339 vtable->close(ctx, data);
341 /* Re-run the length restriction checks with no dictionary at all. */
342 setup_argv[3] = (char *) "minimum_length";
343 setup_argv[4] = (char *) "12";
344 setup_argv[5] = NULL;
345 run_setup((const char **) setup_argv);
347 /* Obtain a new Kerberos context with that krb5.conf file. */
348 krb5_free_context(ctx);
349 code = krb5_init_context(&ctx);
351 bail_krb5(ctx, code, "cannot initialize Kerberos context");
353 /* Run all of the length tests. */
354 code = vtable->open(ctx, NULL, &data);
355 is_int(0, code, "Plugin initialization (length)");
357 bail("cannot continue after plugin initialization failure");
358 for (i = 0; i < ARRAY_SIZE(length_tests); i++)
359 is_password_test(ctx, vtable, data, &length_tests[i]);
360 vtable->close(ctx, data);
364 /* If built with CDB, set up krb5.conf to use a CDB dictionary instead. */
365 test_file_path_free(dictionary);
366 dictionary = test_file_path("data/wordlist.cdb");
367 if (dictionary == NULL)
368 bail("cannot find data/wordlist.cdb in the test suite");
369 setup_argv[3] = (char *) "password_dictionary_cdb";
370 setup_argv[4] = dictionary;
371 setup_argv[5] = NULL;
372 run_setup((const char **) setup_argv);
374 /* Obtain a new Kerberos context with that krb5.conf file. */
375 krb5_free_context(ctx);
376 code = krb5_init_context(&ctx);
378 bail_krb5(ctx, code, "cannot initialize Kerberos context");
380 /* Run the CDB and principal tests. */
381 code = vtable->open(ctx, NULL, &data);
382 is_int(0, code, "Plugin initialization (CDB dictionary)");
384 bail("cannot continue after plugin initialization failure");
385 for (i = 0; i < ARRAY_SIZE(cdb_tests); i++)
386 is_password_test(ctx, vtable, data, &cdb_tests[i]);
387 for (i = 0; i < ARRAY_SIZE(principal_tests); i++)
388 is_password_test(ctx, vtable, data, &principal_tests[i]);
389 vtable->close(ctx, data);
391 # else /* !HAVE_CDB */
393 /* Otherwise, mark the CDB tests as skipped. */
394 count = ARRAY_SIZE(cdb_tests) + ARRAY_SIZE(principal_tests);
395 skip_block(count * 2 + 1, "not built with CDB support");
397 # endif /* !HAVE_CDB */
402 * If built with SQLite, set up krb5.conf to use a SQLite dictionary
405 test_file_path_free(dictionary);
406 dictionary = test_file_path("data/wordlist.sqlite");
407 if (dictionary == NULL)
408 bail("cannot find data/wordlist.sqlite in the test suite");
409 setup_argv[3] = (char *) "password_dictionary_sqlite";
410 setup_argv[4] = dictionary;
411 setup_argv[5] = NULL;
412 run_setup((const char **) setup_argv);
413 test_file_path_free(setup_argv[0]);
414 test_file_path_free(path);
416 /* Obtain a new Kerberos context with that krb5.conf file. */
417 krb5_free_context(ctx);
418 code = krb5_init_context(&ctx);
420 bail_krb5(ctx, code, "cannot initialize Kerberos context");
422 /* Run the SQLite and principal tests. */
423 code = vtable->open(ctx, NULL, &data);
424 is_int(0, code, "Plugin initialization (SQLite dictionary)");
426 bail("cannot continue after plugin initialization failure");
427 for (i = 0; i < ARRAY_SIZE(sqlite_tests); i++)
428 is_password_test(ctx, vtable, data, &sqlite_tests[i]);
429 for (i = 0; i < ARRAY_SIZE(principal_tests); i++)
430 is_password_test(ctx, vtable, data, &principal_tests[i]);
431 vtable->close(ctx, data);
433 # else /* !HAVE_SQLITE */
435 /* Otherwise, mark the SQLite tests as skipped. */
436 count = ARRAY_SIZE(sqlite_tests) + ARRAY_SIZE(principal_tests);
437 skip_block(count * 2 + 1, "not built with SQLite support");
439 # endif /* !HAVE_SQLITE */
441 /* Manually clean up after the results of make-krb5-conf. */
442 basprintf(&path, "%s/krb5.conf", tmpdir);
445 test_tmpdir_free(tmpdir);
447 /* Close down the module. */
448 if (dlclose(handle) != 0)
449 bail("cannot close plugin: %s", dlerror());
451 /* Keep valgrind clean by freeing all memory. */
452 test_file_path_free(dictionary);
453 krb5_free_context(ctx);
455 putenv((char *) "KRB5_CONFIG=");
460 #endif /* HAVE_KRB5_PWQUAL_PLUGIN_H */