2 * Test for the MIT Kerberos shared module API.
4 * Written by Russ Allbery <rra@stanford.edu>
6 * The Board of Trustees of the Leland Stanford Junior University
8 * See LICENSE for licensing terms.
12 #include <portable/system.h>
17 #ifdef HAVE_KRB5_PWQUAL_PLUGIN_H
18 # include <krb5/pwqual_plugin.h>
21 #include <tests/tap/macros.h>
23 #ifndef HAVE_KRB5_PWQUAL_PLUGIN_H
25 * If we're not building with MIT Kerberos, we can't run this test. Exit with
26 * a special status to communicate this to the test wrapper.
29 main(int argc UNUSED, char *argv[] UNUSED)
36 /* The public symbol that we load and call to get the vtable. */
37 typedef krb5_error_code pwqual_strength_initvt(krb5_context, int, int,
41 * Expects a principal and a password to check on the command line. Loads the
42 * MIT Kerberos plugin, converts the input into the necessary format, calls
43 * the plugin, and reports the results. Exits with a status matching the
44 * return value of the plugin function.
46 * We assume that the plugin is available as:
48 * BUILD/../plugin/.libs/passwd_strength.so
50 * since we don't want to embed Libtool's libtldl just to run a test.
53 main(int argc, char *argv[])
60 krb5_error_code status;
62 krb5_pwqual_moddata data;
63 krb5_pwqual_vtable verifier = NULL;
64 krb5_error_code (*init)(krb5_context, int, int, krb5_plugin_vtable);
66 /* Build the path of the plugin. */
68 fprintf(stderr, "Wrong number of arguments\n");
71 build = getenv("BUILD");
73 fprintf(stderr, "No BUILD environment variable set\n");
76 length = strlen(build) + strlen("/../plugin/.libs/passwd_strength.so");
77 path = malloc(length + 1);
79 fprintf(stderr, "Cannot allocate memory: %s\n", strerror(errno));
82 strlcpy(path, build, length + 1);
83 strlcat(path, "/../plugin/.libs/passwd_strength.so", length + 1);
85 /* Initialize the data structures. */
86 status = krb5_init_context(&ctx);
88 fprintf(stderr, "Cannot initialize Kerberos context\n");
91 status = krb5_parse_name(ctx, argv[1], &princ);
93 fprintf(stderr, "Cannot parse principal name\n");
97 /* Load the module and find the correct symbol. */
98 handle = dlopen(path, RTLD_NOW);
100 fprintf(stderr, "Cannot dlopen %s: %s\n", path, dlerror());
103 init = dlsym(handle, "pwqual_strength_initvt");
105 fprintf(stderr, "Cannot get pwqual_strength_initvt symbol: %s\n",
110 /* Call that function to get the vtable. */
111 verifier = malloc(sizeof(*verifier));
112 if (verifier == NULL) {
113 fprintf(stderr, "Cannot allocate memory: %s\n", strerror(errno));
116 status = init(ctx, 1, 1, (krb5_plugin_vtable) verifier);
118 fprintf(stderr, "Cannot obtain module vtable\n");
121 if (strcmp(verifier->name, "krb5-strength") != 0) {
122 fprintf(stderr, "Invalid metadata in plugin\n");
126 /* Open the verifier, run the check function, and close it. */
127 status = verifier->open(ctx, argv[3], &data);
129 fprintf(stderr, "%s\n", krb5_get_error_message(ctx, status));
132 status = verifier->check(ctx, data, argv[2], NULL, princ, NULL);
134 fprintf(stderr, "%s\n", krb5_get_error_message(ctx, status));
135 verifier->close(ctx, data);
139 #endif /* HAVE_KRB5_PWQUAL_PLUGIN_H */