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