use DES_set_key_unchecked().
[heimdal.git] / lib / krb5 / plugin.c
blob3be348c9892bfd1e3fc8928e8b16164d9441d454
1 /*
2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
35 RCSID("$Id$");
36 #ifdef HAVE_DLFCN_H
37 #include <dlfcn.h>
38 #endif
39 #include <dirent.h>
41 struct krb5_plugin {
42 void *symbol;
43 void *dsohandle;
44 struct krb5_plugin *next;
47 struct plugin {
48 enum krb5_plugin_type type;
49 void *name;
50 void *symbol;
51 struct plugin *next;
54 static HEIMDAL_MUTEX plugin_mutex = HEIMDAL_MUTEX_INITIALIZER;
55 static struct plugin *registered = NULL;
57 static const char *plugin_dir = LIBDIR "/plugin/krb5";
63 void *
64 _krb5_plugin_get_symbol(struct krb5_plugin *p)
66 return p->symbol;
69 struct krb5_plugin *
70 _krb5_plugin_get_next(struct krb5_plugin *p)
72 return p->next;
79 #ifdef HAVE_DLOPEN
81 static krb5_error_code
82 loadlib(krb5_context context,
83 enum krb5_plugin_type type,
84 const char *name,
85 const char *lib,
86 struct krb5_plugin **e)
88 *e = calloc(1, sizeof(**e));
89 if (*e == NULL) {
90 krb5_set_error_string(context, "out of memory");
91 return ENOMEM;
94 #ifndef RTLD_LAZY
95 #define RTLD_LAZY 0
96 #endif
98 (*e)->dsohandle = dlopen(lib, RTLD_LAZY);
99 if ((*e)->dsohandle == NULL) {
100 free(*e);
101 *e = NULL;
102 krb5_set_error_string(context, "Failed to load %s: %s",
103 lib, dlerror());
104 return ENOMEM;
107 /* dlsym doesn't care about the type */
108 (*e)->symbol = dlsym((*e)->dsohandle, name);
109 if ((*e)->symbol == NULL) {
110 dlclose((*e)->dsohandle);
111 free(*e);
112 krb5_clear_error_string(context);
113 return ENOMEM;
116 return 0;
118 #endif /* HAVE_DLOPEN */
121 * Register a plugin symbol name of specific type.
122 * @param context a Keberos context
123 * @param type type of plugin symbol
124 * @param name name of plugin symbol
125 * @param symbol a pointer to the named symbol
126 * @return In case of error a non zero error com_err error is returned
127 * and the Kerberos error string is set.
129 * @ingroup krb5_support
132 krb5_error_code
133 krb5_plugin_register(krb5_context context,
134 enum krb5_plugin_type type,
135 const char *name,
136 void *symbol)
138 struct plugin *e;
140 e = calloc(1, sizeof(*e));
141 if (e == NULL) {
142 krb5_set_error_string(context, "out of memory");
143 return ENOMEM;
145 e->type = type;
146 e->name = strdup(name);
147 if (e->name == NULL) {
148 free(e);
149 krb5_set_error_string(context, "out of memory");
150 return ENOMEM;
152 e->symbol = symbol;
154 HEIMDAL_MUTEX_lock(&plugin_mutex);
155 e->next = registered;
156 registered = e;
157 HEIMDAL_MUTEX_unlock(&plugin_mutex);
159 return 0;
162 krb5_error_code
163 _krb5_plugin_find(krb5_context context,
164 enum krb5_plugin_type type,
165 const char *name,
166 struct krb5_plugin **list)
168 struct krb5_plugin *e;
169 struct plugin *p;
170 krb5_error_code ret;
171 char *sysdirs[2] = { NULL, NULL };
172 char **dirs = NULL, **di;
173 struct dirent *entry;
174 char *path;
175 DIR *d = NULL;
177 *list = NULL;
179 HEIMDAL_MUTEX_lock(&plugin_mutex);
181 for (p = registered; p != NULL; p = p->next) {
182 if (p->type != type || strcmp(p->name, name) != 0)
183 continue;
185 e = calloc(1, sizeof(*e));
186 if (e == NULL) {
187 HEIMDAL_MUTEX_unlock(&plugin_mutex);
188 krb5_set_error_string(context, "out of memory");
189 ret = ENOMEM;
190 goto out;
192 e->symbol = p->symbol;
193 e->dsohandle = NULL;
194 e->next = *list;
195 *list = e;
197 HEIMDAL_MUTEX_unlock(&plugin_mutex);
199 #ifdef HAVE_DLOPEN
201 dirs = krb5_config_get_strings(context, NULL, "libdefaults",
202 "plugin_dir", NULL);
203 if (dirs == NULL) {
204 sysdirs[0] = rk_UNCONST(plugin_dir);
205 dirs = sysdirs;
208 for (di = dirs; *di != NULL; di++) {
210 d = opendir(*di);
211 if (d == NULL)
212 continue;
214 while ((entry = readdir(d)) != NULL) {
215 asprintf(&path, "%s/%s", *di, entry->d_name);
216 if (path == NULL) {
217 krb5_set_error_string(context, "out of memory");
218 ret = ENOMEM;
219 goto out;
221 ret = loadlib(context, type, name, path, &e);
222 free(path);
223 if (ret)
224 continue;
226 e->next = *list;
227 *list = e;
229 closedir(d);
231 if (dirs != sysdirs)
232 krb5_config_free_strings(dirs);
233 #endif /* HAVE_DLOPEN */
235 if (*list == NULL) {
236 krb5_set_error_string(context, "Did not find a plugin for %s", name);
237 return ENOENT;
240 return 0;
242 out:
243 if (dirs && dirs != sysdirs)
244 krb5_config_free_strings(dirs);
245 if (d)
246 closedir(d);
247 _krb5_plugin_free(*list);
248 *list = NULL;
250 return ret;
253 void
254 _krb5_plugin_free(struct krb5_plugin *list)
256 struct krb5_plugin *next;
257 while (list) {
258 next = list->next;
259 if (list->dsohandle)
260 dlclose(list->dsohandle);
261 free(list);
262 list = next;