]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/plugin/mit-t.c
9857bb9e5bac050d19fe7ca9118fcbe203a67210
[kerberos/krb5-strength.git] / tests / plugin / mit-t.c
1 /*
2  * Test for the MIT Kerberos shared module API.
3  *
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
8  *
9  * See LICENSE for licensing terms.
10  */
11
12 #include <config.h>
13 #include <portable/kadmin.h>
14 #include <portable/krb5.h>
15 #include <portable/system.h>
16
17 #include <dlfcn.h>
18 #include <errno.h>
19 #ifdef HAVE_KRB5_PWQUAL_PLUGIN_H
20 #    include <krb5/pwqual_plugin.h>
21 #endif
22
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>
28
29 /*
30  * The password test data, generated from the JSON source.  Defines arrays
31  * named *_tests, where * is the file name without the ".c" suffix.
32  */
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>
40
41
42 #ifndef HAVE_KRB5_PWQUAL_PLUGIN_H
43 /*
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.
47  */
48 int
49 main(void)
50 {
51     skip_all("not built against MIT libraries");
52     return 0;
53 }
54
55 #else
56
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,
59                                                krb5_plugin_vtable);
60
61
62 /*
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.
67  */
68 static krb5_pwqual_vtable
69 load_plugin(krb5_context ctx, void **handle)
70 {
71     char *path;
72     krb5_error_code code;
73     krb5_pwqual_vtable vtable = NULL;
74     krb5_error_code (*init)(krb5_context, int, int, krb5_plugin_vtable);
75
76     /* Load the module. */
77     path = test_file_path("../plugin/.libs/strength.so");
78     if (path == NULL)
79         bail("cannot find plugin");
80     *handle = dlopen(path, RTLD_NOW);
81     if (*handle == NULL)
82         bail("cannot dlopen %s: %s", path, dlerror());
83     test_file_path_free(path);
84
85     /* Find the entry point function. */
86     init = dlsym(*handle, "pwqual_strength_initvt");
87     if (init == NULL)
88         bail("cannot get pwqual_strength_initvt symbol: %s", dlerror());
89
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");
94
95     /* Call that function properly to get the vtable. */
96     vtable = bmalloc(sizeof(*vtable));
97     code = init(ctx, 1, 1, (krb5_plugin_vtable) vtable);
98     if (code != 0)
99         bail_krb5(ctx, code, "cannot obtain module vtable");
100
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");
104
105     /* Verify the metadata. */
106     is_string("krb5-strength", vtable->name, "Module name");
107
108     /* Return the vtable. */
109     return vtable;
110 }
111
112
113 /*
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
116  * results.
117  */
118 static void
119 is_password_test(krb5_context ctx, const krb5_pwqual_vtable vtable,
120                  krb5_pwqual_moddata data, const struct password_test *test)
121 {
122     krb5_principal princ;
123     krb5_error_code code;
124     const char *error;
125
126     /* Translate the principal into a krb5_principal. */
127     code = krb5_parse_name(ctx, test->principal, &princ);
128     if (code != 0)
129         bail_krb5(ctx, code, "cannot parse principal %s", test->principal);
130
131     /* Call the verifier. */
132     code = vtable->check(ctx, data, test->password, NULL, princ, NULL);
133
134     /* Check the results against the test data. */
135     is_int(test->code, code, "%s (status)", test->name);
136     if (code == 0)
137         is_string(test->error, NULL, "%s (error)", test->name);
138     else {
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);
142     }
143
144     /* Free the parsed principal. */
145     krb5_free_principal(ctx, princ);
146 }
147
148
149 int
150 main(void)
151 {
152     char *path, *dictionary, *krb5_config, *krb5_config_empty, *tmpdir;
153     char *setup_argv[12];
154     const char *build;
155     size_t i, count;
156     krb5_context ctx;
157     krb5_pwqual_vtable vtable;
158     krb5_pwqual_moddata data;
159     krb5_error_code code;
160     void *handle;
161
162     /*
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
165      * per password test.
166      *
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.
170      */
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);
179
180     /* Start with the krb5.conf that contains no dictionary configuration. */
181     path = test_file_path("data/krb5.conf");
182     if (path == NULL)
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);
186
187     /* Obtain a Kerberos context with that krb5.conf file. */
188     code = krb5_init_context(&ctx);
189     if (code != 0)
190         bail_krb5(ctx, code, "cannot initialize Kerberos context");
191
192     /* Load the plugin. */
193     vtable = load_plugin(ctx, &handle);
194
195     /* Initialize the plugin with a CrackLib dictionary. */
196     build = getenv("BUILD");
197     if (build == NULL)
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)");
202     if (code != 0)
203         bail("cannot continue after plugin initialization failure");
204
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]);
208
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]);
213 #    else
214     count = ARRAY_SIZE(cracklib_tests);
215     skip_block(count * 2, "not built with CrackLib support");
216 #    endif
217
218     /* Close that initialization of the plugin and destroy that context. */
219     vtable->close(ctx, data);
220     krb5_free_context(ctx);
221     ctx = NULL;
222
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;
231
232     /* Point KRB5_CONFIG at the newly-generated krb5.conf file. */
233     basprintf(&krb5_config, "KRB5_CONFIG=%s/krb5.conf", tmpdir);
234     putenv(krb5_config);
235     free(krb5_config_empty);
236
237 #    ifdef HAVE_CRACKLIB
238
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);
244
245     /* Obtain a new Kerberos context with that krb5.conf file. */
246     krb5_free_context(ctx);
247     code = krb5_init_context(&ctx);
248     if (code != 0)
249         bail_krb5(ctx, code, "cannot initialize Kerberos context");
250
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)");
254     if (code != 0)
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);
259
260     /*
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.
264      */
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);
271
272     /* Obtain a new Kerberos context with that krb5.conf file. */
273     krb5_free_context(ctx);
274     code = krb5_init_context(&ctx);
275     if (code != 0)
276         bail_krb5(ctx, code, "cannot initialize Kerberos context");
277
278     /* Run all of the length tests. */
279     code = vtable->open(ctx, NULL, &data);
280     is_int(0, code, "Plugin initialization (length)");
281     if (code != 0)
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);
286
287 #    else
288
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");
292
293 #    endif /* !HAVE_CRACKLIB */
294
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);
304
305     /* Obtain a new Kerberos context with that krb5.conf file. */
306     krb5_free_context(ctx);
307     code = krb5_init_context(&ctx);
308     if (code != 0)
309         bail_krb5(ctx, code, "cannot initialize Kerberos context");
310
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)");
314     if (code != 0)
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);
319
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);
325
326     /* Obtain a new Kerberos context with that krb5.conf file. */
327     krb5_free_context(ctx);
328     code = krb5_init_context(&ctx);
329     if (code != 0)
330         bail_krb5(ctx, code, "cannot initialize Kerberos context");
331
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)");
335     if (code != 0)
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);
340
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);
346
347     /* Obtain a new Kerberos context with that krb5.conf file. */
348     krb5_free_context(ctx);
349     code = krb5_init_context(&ctx);
350     if (code != 0)
351         bail_krb5(ctx, code, "cannot initialize Kerberos context");
352
353     /* Run all of the length tests. */
354     code = vtable->open(ctx, NULL, &data);
355     is_int(0, code, "Plugin initialization (length)");
356     if (code != 0)
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);
361
362 #    ifdef HAVE_CDB
363
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);
373
374     /* Obtain a new Kerberos context with that krb5.conf file. */
375     krb5_free_context(ctx);
376     code = krb5_init_context(&ctx);
377     if (code != 0)
378         bail_krb5(ctx, code, "cannot initialize Kerberos context");
379
380     /* Run the CDB and principal tests. */
381     code = vtable->open(ctx, NULL, &data);
382     is_int(0, code, "Plugin initialization (CDB dictionary)");
383     if (code != 0)
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);
390
391 #    else /* !HAVE_CDB */
392
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");
396
397 #    endif /* !HAVE_CDB */
398
399 #    ifdef HAVE_SQLITE
400
401     /*
402      * If built with SQLite, set up krb5.conf to use a SQLite dictionary
403      * instead.
404      */
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);
415
416     /* Obtain a new Kerberos context with that krb5.conf file. */
417     krb5_free_context(ctx);
418     code = krb5_init_context(&ctx);
419     if (code != 0)
420         bail_krb5(ctx, code, "cannot initialize Kerberos context");
421
422     /* Run the SQLite and principal tests. */
423     code = vtable->open(ctx, NULL, &data);
424     is_int(0, code, "Plugin initialization (SQLite dictionary)");
425     if (code != 0)
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);
432
433 #    else /* !HAVE_SQLITE */
434
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");
438
439 #    endif /* !HAVE_SQLITE */
440
441     /* Manually clean up after the results of make-krb5-conf. */
442     basprintf(&path, "%s/krb5.conf", tmpdir);
443     unlink(path);
444     free(path);
445     test_tmpdir_free(tmpdir);
446
447     /* Close down the module. */
448     if (dlclose(handle) != 0)
449         bail("cannot close plugin: %s", dlerror());
450
451     /* Keep valgrind clean by freeing all memory. */
452     test_file_path_free(dictionary);
453     krb5_free_context(ctx);
454     free(vtable);
455     putenv((char *) "KRB5_CONFIG=");
456     free(krb5_config);
457     return 0;
458 }
459
460 #endif /* HAVE_KRB5_PWQUAL_PLUGIN_H */