]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/mit/plugin.c
Add support for new MIT plugin interface, drop old patch
[kerberos/krb5-strength.git] / tests / mit / plugin.c
1 /*
2  * Test for the MIT Kerberos shared module API.
3  *
4  * Written by Russ Allbery <rra@stanford.edu>
5  * Copyright 2010
6  *     The Board of Trustees of the Leland Stanford Junior University
7  *
8  * See LICENSE for licensing terms.
9  */
10
11 #include <config.h>
12 #include <portable/system.h>
13
14 #include <dlfcn.h>
15 #include <errno.h>
16 #include <krb5.h>
17 #ifdef HAVE_KRB5_PWQUAL_PLUGIN_H
18 # include <krb5/pwqual_plugin.h>
19 #endif
20
21 #include <tests/tap/macros.h>
22
23 #ifndef HAVE_KRB5_PWQUAL_PLUGIN_H
24 /*
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.
27  */
28 int
29 main(int argc UNUSED, char *argv[] UNUSED)
30 {
31     exit(42);
32 }
33
34 #else
35
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,
38                                        krb5_plugin_vtable);
39
40 /*
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.
45  *
46  * We assume that the plugin is available as:
47  *
48  *     BUILD/../plugin/.libs/passwd_strength.so
49  *
50  * since we don't want to embed Libtool's libtldl just to run a test.
51  */
52 int
53 main(int argc, char *argv[])
54 {
55     const char *build;
56     char *path;
57     size_t length;
58     krb5_context ctx;
59     krb5_principal princ;
60     krb5_error_code status;
61     void *handle;
62     krb5_pwqual_moddata data;
63     krb5_pwqual_vtable verifier = NULL;
64     krb5_error_code (*init)(krb5_context, int, int, krb5_plugin_vtable);
65
66     /* Build the path of the plugin. */
67     if (argc != 4) {
68         fprintf(stderr, "Wrong number of arguments\n");
69         exit(1);
70     }
71     build = getenv("BUILD");
72     if (build == NULL) {
73         fprintf(stderr, "No BUILD environment variable set\n");
74         exit(1);
75     }
76     length = strlen(build) + strlen("/../plugin/.libs/passwd_strength.so");
77     path = malloc(length + 1);
78     if (path == NULL) {
79         fprintf(stderr, "Cannot allocate memory: %s\n", strerror(errno));
80         exit(1);
81     }
82     strlcpy(path, build, length + 1);
83     strlcat(path, "/../plugin/.libs/passwd_strength.so", length + 1);
84
85     /* Initialize the data structures. */
86     status = krb5_init_context(&ctx);
87     if (status != 0) {
88         fprintf(stderr, "Cannot initialize Kerberos context\n");
89         exit(1);
90     }
91     status = krb5_parse_name(ctx, argv[1], &princ);
92     if (status != 0) {
93         fprintf(stderr, "Cannot parse principal name\n");
94         exit(1);
95     }
96
97     /* Load the module and find the correct symbol. */
98     handle = dlopen(path, RTLD_NOW);
99     if (handle == NULL) {
100         fprintf(stderr, "Cannot dlopen %s: %s\n", path, dlerror());
101         exit(1);
102     }
103     init = dlsym(handle, "pwqual_strength_initvt");
104     if (init == NULL) {
105         fprintf(stderr, "Cannot get pwqual_strength_initvt symbol: %s\n",
106                 dlerror());
107         exit(1);
108     }
109
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));
114         exit(1);
115     }
116     status = init(ctx, 1, 1, (krb5_plugin_vtable) verifier);
117     if (status != 0) {
118         fprintf(stderr, "Cannot obtain module vtable\n");
119         exit(1);
120     }
121     if (strcmp(verifier->name, "krb5-strength") != 0) {
122         fprintf(stderr, "Invalid metadata in plugin\n");
123         exit(1);
124     }
125
126     /* Open the verifier, run the check function, and close it. */
127     status = verifier->open(ctx, argv[3], &data);
128     if (status != 0) {
129         fprintf(stderr, "%s\n", krb5_get_error_message(ctx, status));
130         exit(1);
131     }
132     status = verifier->check(ctx, data, argv[2], NULL, princ, NULL);
133     if (status != 0)
134         fprintf(stderr, "%s\n", krb5_get_error_message(ctx, status));
135     verifier->close(ctx, data);
136     exit(status);
137 }
138
139 #endif /* HAVE_KRB5_PWQUAL_PLUGIN_H */