]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/mit/plugin.c
Update LICENSE statements
[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
18 /* Allow for a build without the plugin header. */
19 # ifdef HAVE_KRB5_PWCHECK_PLUGIN_H
20 #  include <krb5/pwcheck_plugin.h>
21 # else
22 typedef struct krb5plugin_kadmin_pwcheck_ftable_v0 {
23     int minor_version;
24     krb5_error_code (*init)(krb5_context, void **);
25     void (*fini)(krb5_context, void *);
26     int (*check)(krb5_context, void *, krb5_const_principal,
27                  const krb5_data *password);
28 } krb5plugin_kadmin_pwcheck_ftable_v0;
29 # endif /* !HAVE_KRB5_PWCHECK_PLUGIN_H */
30
31
32 /*
33  * Expects a principal and a password to check on the command line.  Loads the
34  * MIT Kerberos plugin, converts the input into the necessary format, calls
35  * the plugin, and reports the results.  Exits with a status matching the
36  * return value of the plugin function.
37  *
38  * We assume that the plugin is available as:
39  *
40  *     BUILD/../plugin/.libs/passwd_strength.so
41  *
42  * since we don't want to embed Libtool's libtldl just to run a test.
43  */
44 int
45 main(int argc, char *argv[])
46 {
47     const char *build;
48     char *path;
49     size_t length;
50     krb5_context ctx;
51     krb5_principal princ;
52     krb5_data password;
53     krb5_error_code status;
54     void *handle, *data;
55     struct krb5plugin_kadmin_pwcheck_ftable_v0 *verifier;
56
57     /*
58      * If we're not building with MIT Kerberos, we can't run this test.  Exit
59      * with a special status to communicate this to the test wrapper.
60      */
61 #ifdef HAVE_KRB5_REALM
62     exit(42);
63 #endif
64
65     /* Build the path of the plugin. */
66     if (argc != 3) {
67         fprintf(stderr, "Wrong number of arguments\n");
68         exit(1);
69     }
70     build = getenv("BUILD");
71     if (build == NULL) {
72         fprintf(stderr, "No BUILD environment variable set\n");
73         exit(1);
74     }
75     length = strlen(build) + strlen("/../plugin/.libs/passwd_strength.so");
76     path = malloc(length + 1);
77     if (path == NULL) {
78         fprintf(stderr, "Cannot allocate memory: %s\n", strerror(errno));
79         exit(1);
80     }
81     strlcpy(path, build, length + 1);
82     strlcat(path, "/../plugin/.libs/passwd_strength.so", length + 1);
83
84     /* Initialize the data structures. */
85     status = krb5_init_context(&ctx);
86     if (status != 0) {
87         fprintf(stderr, "Cannot initialize Kerberos context\n");
88         exit(1);
89     }
90     status = krb5_parse_name(ctx, argv[1], &princ);
91     if (status != 0) {
92         fprintf(stderr, "Cannot parse principal name\n");
93         exit(1);
94     }
95     password.length = strlen(argv[2]);
96     password.data = argv[2];
97
98     /* Load the module and find the correct symbol. */
99     handle = dlopen(path, RTLD_NOW);
100     if (handle == NULL) {
101         fprintf(stderr, "Cannot dlopen %s: %s\n", path, dlerror());
102         exit(1);
103     }
104     verifier = dlsym(handle, "kadmin_pwcheck_0");
105     if (verifier == NULL) {
106         fprintf(stderr, "Cannot get kadmin_pwcheck_0 symbol: %s\n", dlerror());
107         exit(1);
108     }
109     if (verifier->minor_version != 0
110         || verifier->init == NULL
111         || verifier->check == NULL
112         || verifier->fini == NULL) {
113         fprintf(stderr, "Invalid metadata in plugin\n");
114         exit(1);
115     }
116     status = verifier->init(ctx, &data);
117     if (status != 0) {
118         fprintf(stderr, "%s\n", krb5_get_error_message(ctx, status));
119         exit(1);
120     }
121     status = verifier->check(ctx, data, princ, &password);
122     if (status != 0)
123         fprintf(stderr, "%s\n", krb5_get_error_message(ctx, status));
124     verifier->fini(ctx, data);
125     exit(status);
126 }