2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
43 struct krb5_plugin
*next
;
47 enum { DSO
, SYMBOL
} type
;
54 enum krb5_plugin_type type
;
62 static HEIMDAL_MUTEX plugin_mutex
= HEIMDAL_MUTEX_INITIALIZER
;
63 static struct plugin
*registered
= NULL
;
64 static int plugins_needs_scan
= 1;
66 static const char *sysplugin_dirs
[] = {
67 LIBDIR
"/plugin/krb5",
69 "/System/Library/KerberosPlugins/KerberosFrameworkPlugins",
79 _krb5_plugin_get_symbol(struct krb5_plugin
*p
)
85 _krb5_plugin_get_next(struct krb5_plugin
*p
)
96 static krb5_error_code
97 loadlib(krb5_context context
, char *path
)
101 e
= calloc(1, sizeof(*e
));
103 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
115 /* ignore error from dlopen, and just keep it as negative cache entry */
116 e
->u
.dso
.dsohandle
= dlopen(path
, RTLD_LOCAL
|RTLD_LAZY
);
117 e
->u
.dso
.path
= path
;
119 e
->next
= registered
;
124 #endif /* HAVE_DLOPEN */
127 * Register a plugin symbol name of specific type.
128 * @param context a Keberos context
129 * @param type type of plugin symbol
130 * @param name name of plugin symbol
131 * @param symbol a pointer to the named symbol
132 * @return In case of error a non zero error com_err error is returned
133 * and the Kerberos error string is set.
135 * @ingroup krb5_support
139 krb5_plugin_register(krb5_context context
,
140 enum krb5_plugin_type type
,
146 HEIMDAL_MUTEX_lock(&plugin_mutex
);
148 /* check for duplicates */
149 for (e
= registered
; e
!= NULL
; e
= e
->next
) {
150 if (e
->type
== SYMBOL
&&
151 strcmp(e
->u
.symbol
.name
, name
) == 0 &&
152 e
->u
.symbol
.type
== type
&& e
->u
.symbol
.symbol
== symbol
) {
153 HEIMDAL_MUTEX_unlock(&plugin_mutex
);
158 e
= calloc(1, sizeof(*e
));
160 HEIMDAL_MUTEX_unlock(&plugin_mutex
);
161 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
165 e
->u
.symbol
.type
= type
;
166 e
->u
.symbol
.name
= strdup(name
);
167 if (e
->u
.symbol
.name
== NULL
) {
168 HEIMDAL_MUTEX_unlock(&plugin_mutex
);
170 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
173 e
->u
.symbol
.symbol
= symbol
;
175 e
->next
= registered
;
177 HEIMDAL_MUTEX_unlock(&plugin_mutex
);
182 static krb5_error_code
183 load_plugins(krb5_context context
)
187 char **dirs
= NULL
, **di
;
188 struct dirent
*entry
;
192 if (!plugins_needs_scan
)
194 plugins_needs_scan
= 0;
198 dirs
= krb5_config_get_strings(context
, NULL
, "libdefaults",
201 dirs
= rk_UNCONST(sysplugin_dirs
);
203 for (di
= dirs
; *di
!= NULL
; di
++) {
204 #ifdef KRB5_USE_PATH_TOKENS
208 if (_krb5_expand_path_tokens(context
, *di
, &dir
))
221 while ((entry
= readdir(d
)) != NULL
) {
222 char *n
= entry
->d_name
;
225 if (n
[0] == '.' && (n
[1] == '\0' || (n
[1] == '.' && n
[2] == '\0')))
231 { /* support loading bundles on MacOS */
232 size_t len
= strlen(n
);
233 if (len
> 7 && strcmp(&n
[len
- 7], ".bundle") == 0)
234 ret
= asprintf(&path
, "%s/%s/Contents/MacOS/%.*s", *di
, n
, (int)(len
- 7), n
);
237 if (ret
< 0 || path
== NULL
)
238 ret
= asprintf(&path
, "%s/%s", *di
, n
);
240 if (ret
< 0 || path
== NULL
) {
242 krb5_set_error_message(context
, ret
, "malloc: out of memory");
246 /* check if already tried */
247 for (e
= registered
; e
!= NULL
; e
= e
->next
)
248 if (e
->type
== DSO
&& strcmp(e
->u
.dso
.path
, path
) == 0)
253 loadlib(context
, path
); /* store or frees path */
258 if (dirs
!= rk_UNCONST(sysplugin_dirs
))
259 krb5_config_free_strings(dirs
);
260 #endif /* HAVE_DLOPEN */
264 static krb5_error_code
265 add_symbol(krb5_context context
, struct krb5_plugin
**list
, void *symbol
)
267 struct krb5_plugin
*e
;
269 e
= calloc(1, sizeof(*e
));
271 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
281 _krb5_plugin_find(krb5_context context
,
282 enum krb5_plugin_type type
,
284 struct krb5_plugin
**list
)
291 HEIMDAL_MUTEX_lock(&plugin_mutex
);
293 load_plugins(context
);
295 for (ret
= 0, e
= registered
; e
!= NULL
; e
= e
->next
) {
299 if (e
->u
.dso
.dsohandle
== NULL
)
301 sym
= dlsym(e
->u
.dso
.dsohandle
, name
);
303 ret
= add_symbol(context
, list
, sym
);
307 if (strcmp(e
->u
.symbol
.name
, name
) == 0 && e
->u
.symbol
.type
== type
)
308 ret
= add_symbol(context
, list
, e
->u
.symbol
.symbol
);
312 _krb5_plugin_free(*list
);
317 HEIMDAL_MUTEX_unlock(&plugin_mutex
);
322 krb5_set_error_message(context
, ENOENT
, "Did not find a plugin for %s", name
);
330 _krb5_plugin_free(struct krb5_plugin
*list
)
332 struct krb5_plugin
*next
;