File: heimdal-t.c

package info (click to toggle)
krb5-strength 3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,112 kB
  • sloc: ansic: 8,236; sh: 4,977; perl: 1,528; makefile: 181
file content (342 lines) | stat: -rw-r--r-- 11,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Test for the Heimdal shared module API.
 *
 * Written by Russ Allbery <eagle@eyrie.org>
 * Copyright 2023 Russ Allbery <eagle@eyrie.org>
 * Copyright 2009, 2013-2014
 *     The Board of Trustees of the Leland Stanford Junior University
 *
 * SPDX-License-Identifier: MIT
 */

#include <config.h>
#include <portable/krb5.h>
#include <portable/system.h>

#include <dlfcn.h>
#include <errno.h>
#ifdef HAVE_KADM5_KADM5_PWCHECK_H
#    include <kadm5/kadm5-pwcheck.h>
#endif

#include <tests/tap/basic.h>
#include <tests/tap/kerberos.h>
#include <tests/tap/process.h>
#include <tests/tap/string.h>
#include <util/macros.h>

/*
 * The password test data, generated from the JSON source.  Defines arrays
 * named *_tests, where * is the file name without the ".c" suffix.
 */
#include <tests/data/passwords/cdb.c>
#include <tests/data/passwords/classes.c>
#include <tests/data/passwords/cracklib.c>
#include <tests/data/passwords/length.c>
#include <tests/data/passwords/letter.c>
#include <tests/data/passwords/principal.c>
#include <tests/data/passwords/sqlite.c>


#ifndef HAVE_KADM5_KADM5_PWCHECK_H
/*
 * If we're not building with Heimdal, we can't run this test and much of the
 * test won't even compile.  Replace this test with a small program that just
 * calls skip_all.
 */
int
main(void)
{
    skip_all("not built against Heimdal libraries");
    return 0;
}

#else

/*
 * Loads the Heimdal password change plugin and tests that its metadata is
 * correct.  Returns a pointer to the kadm5_pw_policy_verifier struct or bails
 * on failure to load the plugin.  Stores the handle in the last argument so
 * that the caller can free the handle at the end of the test suite.
 */
static struct kadm5_pw_policy_verifier *
load_plugin(void **handle)
{
    char *path;
    struct kadm5_pw_policy_verifier *verifier;

    /* Load the module. */
    path = test_file_path("../plugin/.libs/strength.so");
    if (path == NULL)
        bail("cannot find plugin");
    *handle = dlopen(path, RTLD_NOW);
    if (*handle == NULL)
        bail("cannot dlopen %s: %s", path, dlerror());
    test_file_path_free(path);

    /* Find the dispatch table and do a basic sanity check. */
    verifier = dlsym(*handle, "kadm5_password_verifier");
    if (verifier == NULL)
        bail("cannot get kadm5_password_verifier symbol: %s", dlerror());
    if (verifier->funcs == NULL || verifier->funcs[0].func == NULL)
        bail("no verifier functions in module");

    /* Verify the metadata. */
    is_string("krb5-strength", verifier->name, "Module name");
    is_string("Russ Allbery", verifier->vendor, "Module vendor");
    is_int(KADM5_PASSWD_VERSION_V1, verifier->version, "Module version");
    is_string("krb5-strength", verifier->funcs[0].name,
              "Module function name");
    ok(verifier->funcs[1].name == NULL, "Only one function in module");

    /* Return the dispatch table. */
    return verifier;
}


/*
 * Given the dispatch table and a test case, call out to the password strength
 * checking module and check the results.
 */
static void
is_password_test(const struct kadm5_pw_policy_verifier *verifier,
                 const struct password_test *test)
{
    krb5_context ctx;
    krb5_principal princ;
    krb5_error_code code;
    krb5_data password;
    int result;
    char error[BUFSIZ] = "";

    /* Obtain a Kerberos context to use for parsing principal names. */
    code = krb5_init_context(&ctx);
    if (code != 0)
        bail_krb5(ctx, code, "cannot initialize Kerberos context");

    /* Translate the test data into the form that the verifier expects. */
    code = krb5_parse_name(ctx, test->principal, &princ);
    if (code != 0)
        bail_krb5(ctx, code, "cannot parse principal %s", test->principal);
    password.data = (char *) test->password;
    password.length = strlen(test->password);

    /* Call the verifier. */
    result = (verifier->funcs[0].func)(ctx, princ, &password, NULL, error,
                                       sizeof(error));

    /* Heimdal only returns 0 or 1, so translate the expected code. */
    is_int(test->code == 0 ? 0 : 1, result, "%s (status)", test->name);
    is_string(test->error == NULL ? "" : test->error, error, "%s (error)",
              test->name);

    /* Free data structures. */
    krb5_free_principal(ctx, princ);
    krb5_free_context(ctx);
}


int
main(void)
{
    char *path, *krb5_config, *krb5_config_empty, *tmpdir;
    char *setup_argv[12];
    size_t i, count;
    struct kadm5_pw_policy_verifier *verifier;
    void *handle;

    /*
     * Calculate how many tests we have.  There are five tests for the module
     * metadata and two tests per password test.  We run the principal tests
     * three times, once each with CrackLib, CDB, and SQLite.
     */
    count = ARRAY_SIZE(cracklib_tests);
    count += 2 * ARRAY_SIZE(length_tests);
    count += ARRAY_SIZE(cdb_tests);
    count += ARRAY_SIZE(sqlite_tests);
    count += ARRAY_SIZE(classes_tests);
    count += ARRAY_SIZE(letter_tests);
    count += ARRAY_SIZE(principal_tests) * 3;
    plan(5 + count * 2);

    /* Start with the krb5.conf that contains no dictionary configuration. */
    path = test_file_path("data/krb5.conf");
    if (path == NULL)
        bail("cannot find data/krb5.conf in the test suite");
    basprintf(&krb5_config_empty, "KRB5_CONFIG=%s", path);
    putenv(krb5_config_empty);

    /* Load the plugin. */
    verifier = load_plugin(&handle);

    /* Set up our krb5.conf with a basic configuration. */
    setup_argv[0] = test_file_path("data/make-krb5-conf");
    if (setup_argv[0] == NULL)
        bail("cannot find data/make-krb5-conf in the test suite");
    tmpdir = test_tmpdir();
    setup_argv[1] = path;
    setup_argv[2] = tmpdir;
    setup_argv[3] = NULL;
    run_setup((const char **) setup_argv);

    /* Point KRB5_CONFIG at the newly-generated krb5.conf file. */
    basprintf(&krb5_config, "KRB5_CONFIG=%s/krb5.conf", tmpdir);
    putenv(krb5_config);
    free(krb5_config_empty);

    /* Run principal tests. */
    for (i = 0; i < ARRAY_SIZE(principal_tests); i++)
        is_password_test(verifier, &principal_tests[i]);

#    ifdef HAVE_CRACKLIB

    /* Add CrackLib tests. */
    setup_argv[3] = (char *) "password_dictionary";
    basprintf(&setup_argv[4], "%s/data/dictionary", getenv("BUILD"));
    setup_argv[5] = NULL;
    run_setup((const char **) setup_argv);

    /* Now, run all of the tests. */
    for (i = 0; i < ARRAY_SIZE(cracklib_tests); i++) {
#        ifdef HAVE_SYSTEM_CRACKLIB
        if (cracklib_tests[i].skip_for_system_cracklib) {
            skip_block(2, "not built with embedded CrackLib");
            continue;
        }
#        endif
        is_password_test(verifier, &cracklib_tests[i]);
    }

    /*
     * Add length restrictions and a maximum length for CrackLib.  This should
     * reject passwords as too short, but let through a password that's
     * actually in the CrackLib dictionary.
     */
    setup_argv[5] = (char *) "minimum_length";
    setup_argv[6] = (char *) "12";
    setup_argv[7] = (char *) "cracklib_maxlen";
    setup_argv[8] = (char *) "11";
    setup_argv[9] = NULL;
    run_setup((const char **) setup_argv);

    /* Run the length tests. */
    for (i = 0; i < ARRAY_SIZE(length_tests); i++)
        is_password_test(verifier, &length_tests[i]);

    /* Free the memory allocated for the CrackLib test. */
    free(setup_argv[4]);

#    else

    /* Otherwise, mark the CrackLib tests as skipped. */
    count = ARRAY_SIZE(cracklib_tests) + ARRAY_SIZE(length_tests);
    skip_block(count * 2, "not built with CDB support");

#    endif /* !HAVE_CRACKLIB */

    /* Add simple character class restrictions. */
    setup_argv[3] = (char *) "minimum_different";
    setup_argv[4] = (char *) "8";
    setup_argv[5] = (char *) "require_ascii_printable";
    setup_argv[6] = (char *) "true";
    setup_argv[7] = (char *) "require_non_letter";
    setup_argv[8] = (char *) "true";
    setup_argv[9] = NULL;
    run_setup((const char **) setup_argv);

    /* Run the simple character class tests. */
    for (i = 0; i < ARRAY_SIZE(letter_tests); i++)
        is_password_test(verifier, &letter_tests[i]);

    /* Add complex character class restrictions and remove the dictionary. */
    setup_argv[3] = (char *) "require_classes";
    setup_argv[4] = (char *) "8-19:lower,upper 8-15:digit 8-11:symbol 24-24:3";
    setup_argv[5] = NULL;
    run_setup((const char **) setup_argv);

    /* Run the simple character class tests. */
    for (i = 0; i < ARRAY_SIZE(classes_tests); i++)
        is_password_test(verifier, &classes_tests[i]);

    /* Try the length checks again with no dictionary at all. */
    setup_argv[3] = (char *) "minimum_length";
    setup_argv[4] = (char *) "12";
    setup_argv[5] = NULL;
    run_setup((const char **) setup_argv);

    /* Run the length tests. */
    for (i = 0; i < ARRAY_SIZE(length_tests); i++)
        is_password_test(verifier, &length_tests[i]);

#    ifdef HAVE_CDB

    /* If built with CDB, set up krb5.conf to use a CDB dictionary instead. */
    setup_argv[3] = (char *) "password_dictionary_cdb";
    setup_argv[4] = test_file_path("data/wordlist.cdb");
    if (setup_argv[4] == NULL)
        bail("cannot find data/wordlist.cdb in the test suite");
    setup_argv[5] = NULL;
    run_setup((const char **) setup_argv);
    test_file_path_free(setup_argv[4]);

    /* Run the CDB tests. */
    for (i = 0; i < ARRAY_SIZE(cdb_tests); i++)
        is_password_test(verifier, &cdb_tests[i]);
    for (i = 0; i < ARRAY_SIZE(principal_tests); i++)
        is_password_test(verifier, &principal_tests[i]);

#    else /* !HAVE_CDB */

    /* Otherwise, mark the CDB tests as skipped. */
    count = ARRAY_SIZE(cdb_tests) + ARRAY_SIZE(principal_tests);
    skip_block(count * 2, "not built with CDB support");

#    endif /* !HAVE_CDB */

#    ifdef HAVE_SQLITE3

    /*
     * If built with SQLite, set up krb5.conf to use a SQLite dictionary
     * instead.
     */
    setup_argv[3] = (char *) "password_dictionary_sqlite";
    setup_argv[4] = test_file_path("data/wordlist.sqlite");
    if (setup_argv[4] == NULL)
        bail("cannot find data/wordlist.sqlite in the test suite");
    setup_argv[5] = NULL;
    run_setup((const char **) setup_argv);
    test_file_path_free(setup_argv[0]);
    test_file_path_free(setup_argv[4]);
    test_file_path_free(path);

    /* Run the SQLite tests. */
    for (i = 0; i < ARRAY_SIZE(sqlite_tests); i++)
        is_password_test(verifier, &sqlite_tests[i]);
    for (i = 0; i < ARRAY_SIZE(principal_tests); i++)
        is_password_test(verifier, &principal_tests[i]);

#    else /* !HAVE_SQLITE3 */

    /* Otherwise, mark the SQLite tests as skipped. */
    count = ARRAY_SIZE(sqlite_tests) + ARRAY_SIZE(principal_tests);
    skip_block(count * 2, "not built with SQLite support");

#    endif /* !HAVE_SQLITE3 */

    /* Manually clean up after the results of make-krb5-conf. */
    basprintf(&path, "%s/krb5.conf", tmpdir);
    unlink(path);
    free(path);
    test_tmpdir_free(tmpdir);

    /* Close down the module. */
    if (dlclose(handle) != 0)
        bail("cannot close plugin: %s", dlerror());

    /* Keep valgrind clean by freeing environmental memory. */
    putenv((char *) "KRB5_CONFIG=");
    free(krb5_config);
    return 0;
}

#endif /* HAVE_KADM5_KADM5_PWCHECK_H */