]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/plugin/heimdal-t.c
Declare fast forward from 3.1-2
[kerberos/krb5-strength.git] / tests / plugin / heimdal-t.c
1 /*
2  * Test for the Heimdal shared module API.
3  *
4  * Written by Russ Allbery <eagle@eyrie.org>
5  * Copyright 2009, 2013-2014
6  *     The Board of Trustees of the Leland Stanford Junior University
7  *
8  * SPDX-License-Identifier: MIT
9  */
10
11 #include <config.h>
12 #include <portable/krb5.h>
13 #include <portable/system.h>
14
15 #include <dlfcn.h>
16 #include <errno.h>
17 #ifdef HAVE_KADM5_KADM5_PWCHECK_H
18 #    include <kadm5/kadm5-pwcheck.h>
19 #endif
20
21 #include <tests/tap/basic.h>
22 #include <tests/tap/kerberos.h>
23 #include <tests/tap/process.h>
24 #include <tests/tap/string.h>
25 #include <util/macros.h>
26
27 /*
28  * The password test data, generated from the JSON source.  Defines arrays
29  * named *_tests, where * is the file name without the ".c" suffix.
30  */
31 #include <tests/data/passwords/cdb.c>
32 #include <tests/data/passwords/classes.c>
33 #include <tests/data/passwords/cracklib.c>
34 #include <tests/data/passwords/length.c>
35 #include <tests/data/passwords/letter.c>
36 #include <tests/data/passwords/principal.c>
37 #include <tests/data/passwords/sqlite.c>
38
39
40 #ifndef HAVE_KADM5_KADM5_PWCHECK_H
41 /*
42  * If we're not building with Heimdal, we can't run this test and much of the
43  * test won't even compile.  Replace this test with a small program that just
44  * calls skip_all.
45  */
46 int
47 main(void)
48 {
49     skip_all("not built against Heimdal libraries");
50     return 0;
51 }
52
53 #else
54
55 /*
56  * Loads the Heimdal password change plugin and tests that its metadata is
57  * correct.  Returns a pointer to the kadm5_pw_policy_verifier struct or bails
58  * on failure to load the plugin.  Stores the handle in the last argument so
59  * that the caller can free the handle at the end of the test suite.
60  */
61 static struct kadm5_pw_policy_verifier *
62 load_plugin(void **handle)
63 {
64     char *path;
65     struct kadm5_pw_policy_verifier *verifier;
66
67     /* Load the module. */
68     path = test_file_path("../plugin/.libs/strength.so");
69     if (path == NULL)
70         bail("cannot find plugin");
71     *handle = dlopen(path, RTLD_NOW);
72     if (*handle == NULL)
73         bail("cannot dlopen %s: %s", path, dlerror());
74     test_file_path_free(path);
75
76     /* Find the dispatch table and do a basic sanity check. */
77     verifier = dlsym(*handle, "kadm5_password_verifier");
78     if (verifier == NULL)
79         bail("cannot get kadm5_password_verifier symbol: %s", dlerror());
80     if (verifier->funcs == NULL || verifier->funcs[0].func == NULL)
81         bail("no verifier functions in module");
82
83     /* Verify the metadata. */
84     is_string("krb5-strength", verifier->name, "Module name");
85     is_string("Russ Allbery", verifier->vendor, "Module vendor");
86     is_int(KADM5_PASSWD_VERSION_V1, verifier->version, "Module version");
87     is_string("krb5-strength", verifier->funcs[0].name,
88               "Module function name");
89     ok(verifier->funcs[1].name == NULL, "Only one function in module");
90
91     /* Return the dispatch table. */
92     return verifier;
93 }
94
95
96 /*
97  * Given the dispatch table and a test case, call out to the password strength
98  * checking module and check the results.
99  */
100 static void
101 is_password_test(const struct kadm5_pw_policy_verifier *verifier,
102                  const struct password_test *test)
103 {
104     krb5_context ctx;
105     krb5_principal princ;
106     krb5_error_code code;
107     krb5_data password;
108     int result;
109     char error[BUFSIZ] = "";
110
111     /* Obtain a Kerberos context to use for parsing principal names. */
112     code = krb5_init_context(&ctx);
113     if (code != 0)
114         bail_krb5(ctx, code, "cannot initialize Kerberos context");
115
116     /* Translate the test data into the form that the verifier expects. */
117     code = krb5_parse_name(ctx, test->principal, &princ);
118     if (code != 0)
119         bail_krb5(ctx, code, "cannot parse principal %s", test->principal);
120     password.data = (char *) test->password;
121     password.length = strlen(test->password);
122
123     /* Call the verifier. */
124     result = (verifier->funcs[0].func)(ctx, princ, &password, NULL, error,
125                                        sizeof(error));
126
127     /* Heimdal only returns 0 or 1, so translate the expected code. */
128     is_int(test->code == 0 ? 0 : 1, result, "%s (status)", test->name);
129     is_string(test->error == NULL ? "" : test->error, error, "%s (error)",
130               test->name);
131
132     /* Free data structures. */
133     krb5_free_principal(ctx, princ);
134     krb5_free_context(ctx);
135 }
136
137
138 int
139 main(void)
140 {
141     char *path, *krb5_config, *krb5_config_empty, *tmpdir;
142     char *setup_argv[12];
143     size_t i, count;
144     struct kadm5_pw_policy_verifier *verifier;
145     void *handle;
146
147     /*
148      * Calculate how many tests we have.  There are five tests for the module
149      * metadata and two tests per password test.  We run the principal tests
150      * three times, once each with CrackLib, CDB, and SQLite.
151      */
152     count = ARRAY_SIZE(cracklib_tests);
153     count += 2 * ARRAY_SIZE(length_tests);
154     count += ARRAY_SIZE(cdb_tests);
155     count += ARRAY_SIZE(sqlite_tests);
156     count += ARRAY_SIZE(classes_tests);
157     count += ARRAY_SIZE(letter_tests);
158     count += ARRAY_SIZE(principal_tests) * 3;
159     plan(5 + count * 2);
160
161     /* Start with the krb5.conf that contains no dictionary configuration. */
162     path = test_file_path("data/krb5.conf");
163     if (path == NULL)
164         bail("cannot find data/krb5.conf in the test suite");
165     basprintf(&krb5_config_empty, "KRB5_CONFIG=%s", path);
166     putenv(krb5_config_empty);
167
168     /* Load the plugin. */
169     verifier = load_plugin(&handle);
170
171     /* Set up our krb5.conf with a basic configuration. */
172     setup_argv[0] = test_file_path("data/make-krb5-conf");
173     if (setup_argv[0] == NULL)
174         bail("cannot find data/make-krb5-conf in the test suite");
175     tmpdir = test_tmpdir();
176     setup_argv[1] = path;
177     setup_argv[2] = tmpdir;
178     setup_argv[3] = NULL;
179     run_setup((const char **) setup_argv);
180
181     /* Point KRB5_CONFIG at the newly-generated krb5.conf file. */
182     basprintf(&krb5_config, "KRB5_CONFIG=%s/krb5.conf", tmpdir);
183     putenv(krb5_config);
184     free(krb5_config_empty);
185
186     /* Run principal tests. */
187     for (i = 0; i < ARRAY_SIZE(principal_tests); i++)
188         is_password_test(verifier, &principal_tests[i]);
189
190 #    ifdef HAVE_CRACKLIB
191
192     /* Add CrackLib tests. */
193     setup_argv[3] = (char *) "password_dictionary";
194     basprintf(&setup_argv[4], "%s/data/dictionary", getenv("BUILD"));
195     setup_argv[5] = NULL;
196     run_setup((const char **) setup_argv);
197
198     /* Now, run all of the tests. */
199     for (i = 0; i < ARRAY_SIZE(cracklib_tests); i++) {
200 #        ifdef HAVE_SYSTEM_CRACKLIB
201         if (cracklib_tests[i].skip_for_system_cracklib) {
202             skip_block(2, "not built with embedded CrackLib");
203             continue;
204         }
205 #        endif
206         is_password_test(verifier, &cracklib_tests[i]);
207     }
208
209     /*
210      * Add length restrictions and a maximum length for CrackLib.  This should
211      * reject passwords as too short, but let through a password that's
212      * actually in the CrackLib dictionary.
213      */
214     setup_argv[5] = (char *) "minimum_length";
215     setup_argv[6] = (char *) "12";
216     setup_argv[7] = (char *) "cracklib_maxlen";
217     setup_argv[8] = (char *) "11";
218     setup_argv[9] = NULL;
219     run_setup((const char **) setup_argv);
220
221     /* Run the length tests. */
222     for (i = 0; i < ARRAY_SIZE(length_tests); i++)
223         is_password_test(verifier, &length_tests[i]);
224
225     /* Free the memory allocated for the CrackLib test. */
226     free(setup_argv[4]);
227
228 #    else
229
230     /* Otherwise, mark the CrackLib tests as skipped. */
231     count = ARRAY_SIZE(cracklib_tests) + ARRAY_SIZE(length_tests);
232     skip_block(count * 2, "not built with CDB support");
233
234 #    endif /* !HAVE_CRACKLIB */
235
236     /* Add simple character class restrictions. */
237     setup_argv[3] = (char *) "minimum_different";
238     setup_argv[4] = (char *) "8";
239     setup_argv[5] = (char *) "require_ascii_printable";
240     setup_argv[6] = (char *) "true";
241     setup_argv[7] = (char *) "require_non_letter";
242     setup_argv[8] = (char *) "true";
243     setup_argv[9] = NULL;
244     run_setup((const char **) setup_argv);
245
246     /* Run the simple character class tests. */
247     for (i = 0; i < ARRAY_SIZE(letter_tests); i++)
248         is_password_test(verifier, &letter_tests[i]);
249
250     /* Add complex character class restrictions and remove the dictionary. */
251     setup_argv[3] = (char *) "require_classes";
252     setup_argv[4] = (char *) "8-19:lower,upper 8-15:digit 8-11:symbol 24-24:3";
253     setup_argv[5] = NULL;
254     run_setup((const char **) setup_argv);
255
256     /* Run the simple character class tests. */
257     for (i = 0; i < ARRAY_SIZE(classes_tests); i++)
258         is_password_test(verifier, &classes_tests[i]);
259
260     /* Try the length checks again with no dictionary at all. */
261     setup_argv[3] = (char *) "minimum_length";
262     setup_argv[4] = (char *) "12";
263     setup_argv[5] = NULL;
264     run_setup((const char **) setup_argv);
265
266     /* Run the length tests. */
267     for (i = 0; i < ARRAY_SIZE(length_tests); i++)
268         is_password_test(verifier, &length_tests[i]);
269
270 #    ifdef HAVE_CDB
271
272     /* If built with CDB, set up krb5.conf to use a CDB dictionary instead. */
273     setup_argv[3] = (char *) "password_dictionary_cdb";
274     setup_argv[4] = test_file_path("data/wordlist.cdb");
275     if (setup_argv[4] == NULL)
276         bail("cannot find data/wordlist.cdb in the test suite");
277     setup_argv[5] = NULL;
278     run_setup((const char **) setup_argv);
279     test_file_path_free(setup_argv[4]);
280
281     /* Run the CDB tests. */
282     for (i = 0; i < ARRAY_SIZE(cdb_tests); i++)
283         is_password_test(verifier, &cdb_tests[i]);
284     for (i = 0; i < ARRAY_SIZE(principal_tests); i++)
285         is_password_test(verifier, &principal_tests[i]);
286
287 #    else /* !HAVE_CDB */
288
289     /* Otherwise, mark the CDB tests as skipped. */
290     count = ARRAY_SIZE(cdb_tests) + ARRAY_SIZE(principal_tests);
291     skip_block(count * 2, "not built with CDB support");
292
293 #    endif /* !HAVE_CDB */
294
295 #    ifdef HAVE_SQLITE
296
297     /*
298      * If built with SQLite, set up krb5.conf to use a SQLite dictionary
299      * instead.
300      */
301     setup_argv[3] = (char *) "password_dictionary_sqlite";
302     setup_argv[4] = test_file_path("data/wordlist.sqlite");
303     if (setup_argv[4] == NULL)
304         bail("cannot find data/wordlist.sqlite in the test suite");
305     setup_argv[5] = NULL;
306     run_setup((const char **) setup_argv);
307     test_file_path_free(setup_argv[0]);
308     test_file_path_free(setup_argv[4]);
309     test_file_path_free(path);
310
311     /* Run the SQLite tests. */
312     for (i = 0; i < ARRAY_SIZE(sqlite_tests); i++)
313         is_password_test(verifier, &sqlite_tests[i]);
314     for (i = 0; i < ARRAY_SIZE(principal_tests); i++)
315         is_password_test(verifier, &principal_tests[i]);
316
317 #    else /* !HAVE_SQLITE */
318
319     /* Otherwise, mark the SQLite tests as skipped. */
320     count = ARRAY_SIZE(sqlite_tests) + ARRAY_SIZE(principal_tests);
321     skip_block(count * 2, "not built with SQLite support");
322
323 #    endif /* !HAVE_SQLITE */
324
325     /* Manually clean up after the results of make-krb5-conf. */
326     basprintf(&path, "%s/krb5.conf", tmpdir);
327     unlink(path);
328     free(path);
329     test_tmpdir_free(tmpdir);
330
331     /* Close down the module. */
332     if (dlclose(handle) != 0)
333         bail("cannot close plugin: %s", dlerror());
334
335     /* Keep valgrind clean by freeing environmental memory. */
336     putenv((char *) "KRB5_CONFIG=");
337     free(krb5_config);
338     return 0;
339 }
340
341 #endif /* HAVE_KADM5_KADM5_PWCHECK_H */