xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/plugin.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: plugin.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "krb5_locl.h"
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc #ifdef HAVE_DLFCN_H
39ebfedea0SLionel Sambuc #include <dlfcn.h>
40ebfedea0SLionel Sambuc #endif
41ebfedea0SLionel Sambuc #include <dirent.h>
42ebfedea0SLionel Sambuc 
43ebfedea0SLionel Sambuc struct krb5_plugin {
44ebfedea0SLionel Sambuc     void *symbol;
45ebfedea0SLionel Sambuc     struct krb5_plugin *next;
46ebfedea0SLionel Sambuc };
47ebfedea0SLionel Sambuc 
48ebfedea0SLionel Sambuc struct plugin {
49ebfedea0SLionel Sambuc     enum { DSO, SYMBOL } type;
50ebfedea0SLionel Sambuc     union {
51ebfedea0SLionel Sambuc 	struct {
52ebfedea0SLionel Sambuc 	    char *path;
53ebfedea0SLionel Sambuc 	    void *dsohandle;
54ebfedea0SLionel Sambuc 	} dso;
55ebfedea0SLionel Sambuc 	struct {
56ebfedea0SLionel Sambuc 	    enum krb5_plugin_type type;
57ebfedea0SLionel Sambuc 	    char *name;
58ebfedea0SLionel Sambuc 	    char *symbol;
59ebfedea0SLionel Sambuc 	} symbol;
60ebfedea0SLionel Sambuc     } u;
61ebfedea0SLionel Sambuc     struct plugin *next;
62ebfedea0SLionel Sambuc };
63ebfedea0SLionel Sambuc 
64ebfedea0SLionel Sambuc static HEIMDAL_MUTEX plugin_mutex = HEIMDAL_MUTEX_INITIALIZER;
65ebfedea0SLionel Sambuc static struct plugin *registered = NULL;
66ebfedea0SLionel Sambuc static int plugins_needs_scan = 1;
67ebfedea0SLionel Sambuc 
68ebfedea0SLionel Sambuc static const char *sysplugin_dirs[] =  {
69ebfedea0SLionel Sambuc     LIBDIR "/plugin/krb5",
70ebfedea0SLionel Sambuc #ifdef __APPLE__
71ebfedea0SLionel Sambuc     "/System/Library/KerberosPlugins/KerberosFrameworkPlugins",
72ebfedea0SLionel Sambuc #endif
73ebfedea0SLionel Sambuc     NULL
74ebfedea0SLionel Sambuc };
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc /*
77ebfedea0SLionel Sambuc  *
78ebfedea0SLionel Sambuc  */
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc void *
_krb5_plugin_get_symbol(struct krb5_plugin * p)81ebfedea0SLionel Sambuc _krb5_plugin_get_symbol(struct krb5_plugin *p)
82ebfedea0SLionel Sambuc {
83ebfedea0SLionel Sambuc     return p->symbol;
84ebfedea0SLionel Sambuc }
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc struct krb5_plugin *
_krb5_plugin_get_next(struct krb5_plugin * p)87ebfedea0SLionel Sambuc _krb5_plugin_get_next(struct krb5_plugin *p)
88ebfedea0SLionel Sambuc {
89ebfedea0SLionel Sambuc     return p->next;
90ebfedea0SLionel Sambuc }
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc /*
93ebfedea0SLionel Sambuc  *
94ebfedea0SLionel Sambuc  */
95ebfedea0SLionel Sambuc 
96ebfedea0SLionel Sambuc #ifdef HAVE_DLOPEN
97ebfedea0SLionel Sambuc 
98ebfedea0SLionel Sambuc static krb5_error_code
loadlib(krb5_context context,char * path)99ebfedea0SLionel Sambuc loadlib(krb5_context context, char *path)
100ebfedea0SLionel Sambuc {
101ebfedea0SLionel Sambuc     struct plugin *e;
102ebfedea0SLionel Sambuc 
103ebfedea0SLionel Sambuc     e = calloc(1, sizeof(*e));
104ebfedea0SLionel Sambuc     if (e == NULL) {
105ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
106ebfedea0SLionel Sambuc 	free(path);
107ebfedea0SLionel Sambuc 	return ENOMEM;
108ebfedea0SLionel Sambuc     }
109ebfedea0SLionel Sambuc 
110ebfedea0SLionel Sambuc #ifndef RTLD_LAZY
111ebfedea0SLionel Sambuc #define RTLD_LAZY 0
112ebfedea0SLionel Sambuc #endif
113ebfedea0SLionel Sambuc #ifndef RTLD_LOCAL
114ebfedea0SLionel Sambuc #define RTLD_LOCAL 0
115ebfedea0SLionel Sambuc #endif
116ebfedea0SLionel Sambuc     e->type = DSO;
117ebfedea0SLionel Sambuc     /* ignore error from dlopen, and just keep it as negative cache entry */
118ebfedea0SLionel Sambuc     e->u.dso.dsohandle = dlopen(path, RTLD_LOCAL|RTLD_LAZY);
119ebfedea0SLionel Sambuc     e->u.dso.path = path;
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc     e->next = registered;
122ebfedea0SLionel Sambuc     registered = e;
123ebfedea0SLionel Sambuc 
124ebfedea0SLionel Sambuc     return 0;
125ebfedea0SLionel Sambuc }
126ebfedea0SLionel Sambuc #endif /* HAVE_DLOPEN */
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc /**
129ebfedea0SLionel Sambuc  * Register a plugin symbol name of specific type.
130ebfedea0SLionel Sambuc  * @param context a Keberos context
131ebfedea0SLionel Sambuc  * @param type type of plugin symbol
132ebfedea0SLionel Sambuc  * @param name name of plugin symbol
133ebfedea0SLionel Sambuc  * @param symbol a pointer to the named symbol
134ebfedea0SLionel Sambuc  * @return In case of error a non zero error com_err error is returned
135ebfedea0SLionel Sambuc  * and the Kerberos error string is set.
136ebfedea0SLionel Sambuc  *
137ebfedea0SLionel Sambuc  * @ingroup krb5_support
138ebfedea0SLionel Sambuc  */
139ebfedea0SLionel Sambuc 
140ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_plugin_register(krb5_context context,enum krb5_plugin_type type,const char * name,void * symbol)141ebfedea0SLionel Sambuc krb5_plugin_register(krb5_context context,
142ebfedea0SLionel Sambuc 		     enum krb5_plugin_type type,
143ebfedea0SLionel Sambuc 		     const char *name,
144ebfedea0SLionel Sambuc 		     void *symbol)
145ebfedea0SLionel Sambuc {
146ebfedea0SLionel Sambuc     struct plugin *e;
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_lock(&plugin_mutex);
149ebfedea0SLionel Sambuc 
150ebfedea0SLionel Sambuc     /* check for duplicates */
151ebfedea0SLionel Sambuc     for (e = registered; e != NULL; e = e->next) {
152ebfedea0SLionel Sambuc 	if (e->type == SYMBOL &&
153ebfedea0SLionel Sambuc 	    strcmp(e->u.symbol.name, name) == 0 &&
154ebfedea0SLionel Sambuc 	    e->u.symbol.type == type && e->u.symbol.symbol == symbol) {
155ebfedea0SLionel Sambuc 	    HEIMDAL_MUTEX_unlock(&plugin_mutex);
156ebfedea0SLionel Sambuc 	    return 0;
157ebfedea0SLionel Sambuc 	}
158ebfedea0SLionel Sambuc     }
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc     e = calloc(1, sizeof(*e));
161ebfedea0SLionel Sambuc     if (e == NULL) {
162ebfedea0SLionel Sambuc 	HEIMDAL_MUTEX_unlock(&plugin_mutex);
163ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
164ebfedea0SLionel Sambuc 	return ENOMEM;
165ebfedea0SLionel Sambuc     }
166ebfedea0SLionel Sambuc     e->type = SYMBOL;
167ebfedea0SLionel Sambuc     e->u.symbol.type = type;
168ebfedea0SLionel Sambuc     e->u.symbol.name = strdup(name);
169ebfedea0SLionel Sambuc     if (e->u.symbol.name == NULL) {
170ebfedea0SLionel Sambuc 	HEIMDAL_MUTEX_unlock(&plugin_mutex);
171ebfedea0SLionel Sambuc 	free(e);
172ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
173ebfedea0SLionel Sambuc 	return ENOMEM;
174ebfedea0SLionel Sambuc     }
175ebfedea0SLionel Sambuc     e->u.symbol.symbol = symbol;
176ebfedea0SLionel Sambuc 
177ebfedea0SLionel Sambuc     e->next = registered;
178ebfedea0SLionel Sambuc     registered = e;
179ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_unlock(&plugin_mutex);
180ebfedea0SLionel Sambuc 
181ebfedea0SLionel Sambuc     return 0;
182ebfedea0SLionel Sambuc }
183ebfedea0SLionel Sambuc 
184ebfedea0SLionel Sambuc static int
is_valid_plugin_filename(const char * n)185ebfedea0SLionel Sambuc is_valid_plugin_filename(const char * n)
186ebfedea0SLionel Sambuc {
187ebfedea0SLionel Sambuc     if (n[0] == '.' && (n[1] == '\0' || (n[1] == '.' && n[2] == '\0')))
188ebfedea0SLionel Sambuc         return 0;
189ebfedea0SLionel Sambuc 
190ebfedea0SLionel Sambuc #ifdef _WIN32
191ebfedea0SLionel Sambuc     /* On Windows, we only attempt to load .dll files as plug-ins. */
192ebfedea0SLionel Sambuc     {
193ebfedea0SLionel Sambuc         const char * ext;
194ebfedea0SLionel Sambuc 
195ebfedea0SLionel Sambuc         ext = strrchr(n, '.');
196ebfedea0SLionel Sambuc         if (ext == NULL)
197ebfedea0SLionel Sambuc             return 0;
198ebfedea0SLionel Sambuc 
199ebfedea0SLionel Sambuc         return !stricmp(ext, ".dll");
200ebfedea0SLionel Sambuc     }
201*0a6a1f1dSLionel Sambuc #else
202ebfedea0SLionel Sambuc     return 1;
203*0a6a1f1dSLionel Sambuc #endif
204ebfedea0SLionel Sambuc }
205ebfedea0SLionel Sambuc 
206ebfedea0SLionel Sambuc static void
trim_trailing_slash(char * path)207ebfedea0SLionel Sambuc trim_trailing_slash(char * path)
208ebfedea0SLionel Sambuc {
209ebfedea0SLionel Sambuc     size_t l;
210ebfedea0SLionel Sambuc 
211ebfedea0SLionel Sambuc     l = strlen(path);
212ebfedea0SLionel Sambuc     while (l > 0 && (path[l - 1] == '/'
213ebfedea0SLionel Sambuc #ifdef BACKSLASH_PATH_DELIM
214ebfedea0SLionel Sambuc                      || path[l - 1] == '\\'
215ebfedea0SLionel Sambuc #endif
216ebfedea0SLionel Sambuc                )) {
217ebfedea0SLionel Sambuc         path[--l] = '\0';
218ebfedea0SLionel Sambuc     }
219ebfedea0SLionel Sambuc }
220ebfedea0SLionel Sambuc 
221ebfedea0SLionel Sambuc static krb5_error_code
load_plugins(krb5_context context)222ebfedea0SLionel Sambuc load_plugins(krb5_context context)
223ebfedea0SLionel Sambuc {
224ebfedea0SLionel Sambuc     struct plugin *e;
225ebfedea0SLionel Sambuc     krb5_error_code ret;
226ebfedea0SLionel Sambuc     char **dirs = NULL, **di;
227ebfedea0SLionel Sambuc     struct dirent *entry;
228ebfedea0SLionel Sambuc     char *path;
229ebfedea0SLionel Sambuc     DIR *d = NULL;
230ebfedea0SLionel Sambuc 
231ebfedea0SLionel Sambuc     if (!plugins_needs_scan)
232ebfedea0SLionel Sambuc 	return 0;
233ebfedea0SLionel Sambuc     plugins_needs_scan = 0;
234ebfedea0SLionel Sambuc 
235ebfedea0SLionel Sambuc #ifdef HAVE_DLOPEN
236ebfedea0SLionel Sambuc 
237ebfedea0SLionel Sambuc     dirs = krb5_config_get_strings(context, NULL, "libdefaults",
238ebfedea0SLionel Sambuc 				   "plugin_dir", NULL);
239ebfedea0SLionel Sambuc     if (dirs == NULL)
240ebfedea0SLionel Sambuc 	dirs = rk_UNCONST(sysplugin_dirs);
241ebfedea0SLionel Sambuc 
242ebfedea0SLionel Sambuc     for (di = dirs; *di != NULL; di++) {
243ebfedea0SLionel Sambuc         char * dir = *di;
244ebfedea0SLionel Sambuc 
245ebfedea0SLionel Sambuc #ifdef KRB5_USE_PATH_TOKENS
246ebfedea0SLionel Sambuc         if (_krb5_expand_path_tokens(context, *di, &dir))
247ebfedea0SLionel Sambuc             goto next_dir;
248ebfedea0SLionel Sambuc #endif
249ebfedea0SLionel Sambuc 
250ebfedea0SLionel Sambuc         trim_trailing_slash(dir);
251ebfedea0SLionel Sambuc 
252ebfedea0SLionel Sambuc         d = opendir(dir);
253ebfedea0SLionel Sambuc 
254ebfedea0SLionel Sambuc 	if (d == NULL)
255ebfedea0SLionel Sambuc 	    goto next_dir;
256ebfedea0SLionel Sambuc 
257ebfedea0SLionel Sambuc 	rk_cloexec_dir(d);
258ebfedea0SLionel Sambuc 
259ebfedea0SLionel Sambuc 	while ((entry = readdir(d)) != NULL) {
260ebfedea0SLionel Sambuc 	    char *n = entry->d_name;
261ebfedea0SLionel Sambuc 
262ebfedea0SLionel Sambuc 	    /* skip . and .. */
263ebfedea0SLionel Sambuc             if (!is_valid_plugin_filename(n))
264ebfedea0SLionel Sambuc 		continue;
265ebfedea0SLionel Sambuc 
266ebfedea0SLionel Sambuc 	    path = NULL;
267ebfedea0SLionel Sambuc 	    ret = 0;
268ebfedea0SLionel Sambuc #ifdef __APPLE__
269ebfedea0SLionel Sambuc 	    { /* support loading bundles on MacOS */
270ebfedea0SLionel Sambuc 		size_t len = strlen(n);
271ebfedea0SLionel Sambuc 		if (len > 7 && strcmp(&n[len - 7],  ".bundle") == 0)
272ebfedea0SLionel Sambuc 		    ret = asprintf(&path, "%s/%s/Contents/MacOS/%.*s", dir, n, (int)(len - 7), n);
273ebfedea0SLionel Sambuc 	    }
274ebfedea0SLionel Sambuc #endif
275ebfedea0SLionel Sambuc 	    if (ret < 0 || path == NULL)
276ebfedea0SLionel Sambuc 		ret = asprintf(&path, "%s/%s", dir, n);
277ebfedea0SLionel Sambuc 
278ebfedea0SLionel Sambuc 	    if (ret < 0 || path == NULL) {
279ebfedea0SLionel Sambuc 		ret = ENOMEM;
280ebfedea0SLionel Sambuc 		krb5_set_error_message(context, ret, "malloc: out of memory");
281ebfedea0SLionel Sambuc 		return ret;
282ebfedea0SLionel Sambuc 	    }
283ebfedea0SLionel Sambuc 
284ebfedea0SLionel Sambuc 	    /* check if already tried */
285ebfedea0SLionel Sambuc 	    for (e = registered; e != NULL; e = e->next)
286ebfedea0SLionel Sambuc 		if (e->type == DSO && strcmp(e->u.dso.path, path) == 0)
287ebfedea0SLionel Sambuc 		    break;
288ebfedea0SLionel Sambuc 	    if (e) {
289ebfedea0SLionel Sambuc 		free(path);
290ebfedea0SLionel Sambuc 	    } else {
291ebfedea0SLionel Sambuc 		loadlib(context, path); /* store or frees path */
292ebfedea0SLionel Sambuc 	    }
293ebfedea0SLionel Sambuc 	}
294ebfedea0SLionel Sambuc 	closedir(d);
295ebfedea0SLionel Sambuc 
296ebfedea0SLionel Sambuc     next_dir:
297ebfedea0SLionel Sambuc         if (dir != *di)
298ebfedea0SLionel Sambuc             free(dir);
299ebfedea0SLionel Sambuc     }
300ebfedea0SLionel Sambuc     if (dirs != rk_UNCONST(sysplugin_dirs))
301ebfedea0SLionel Sambuc 	krb5_config_free_strings(dirs);
302ebfedea0SLionel Sambuc #endif /* HAVE_DLOPEN */
303ebfedea0SLionel Sambuc     return 0;
304ebfedea0SLionel Sambuc }
305ebfedea0SLionel Sambuc 
306ebfedea0SLionel Sambuc static krb5_error_code
add_symbol(krb5_context context,struct krb5_plugin ** list,void * symbol)307ebfedea0SLionel Sambuc add_symbol(krb5_context context, struct krb5_plugin **list, void *symbol)
308ebfedea0SLionel Sambuc {
309ebfedea0SLionel Sambuc     struct krb5_plugin *e;
310ebfedea0SLionel Sambuc 
311ebfedea0SLionel Sambuc     e = calloc(1, sizeof(*e));
312ebfedea0SLionel Sambuc     if (e == NULL) {
313ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
314ebfedea0SLionel Sambuc 	return ENOMEM;
315ebfedea0SLionel Sambuc     }
316ebfedea0SLionel Sambuc     e->symbol = symbol;
317ebfedea0SLionel Sambuc     e->next = *list;
318ebfedea0SLionel Sambuc     *list = e;
319ebfedea0SLionel Sambuc     return 0;
320ebfedea0SLionel Sambuc }
321ebfedea0SLionel Sambuc 
322ebfedea0SLionel Sambuc krb5_error_code
_krb5_plugin_find(krb5_context context,enum krb5_plugin_type type,const char * name,struct krb5_plugin ** list)323ebfedea0SLionel Sambuc _krb5_plugin_find(krb5_context context,
324ebfedea0SLionel Sambuc 		  enum krb5_plugin_type type,
325ebfedea0SLionel Sambuc 		  const char *name,
326ebfedea0SLionel Sambuc 		  struct krb5_plugin **list)
327ebfedea0SLionel Sambuc {
328ebfedea0SLionel Sambuc     struct plugin *e;
329ebfedea0SLionel Sambuc     krb5_error_code ret;
330ebfedea0SLionel Sambuc 
331ebfedea0SLionel Sambuc     *list = NULL;
332ebfedea0SLionel Sambuc 
333ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_lock(&plugin_mutex);
334ebfedea0SLionel Sambuc 
335ebfedea0SLionel Sambuc     load_plugins(context);
336ebfedea0SLionel Sambuc 
337ebfedea0SLionel Sambuc     for (ret = 0, e = registered; e != NULL; e = e->next) {
338ebfedea0SLionel Sambuc 	switch(e->type) {
339ebfedea0SLionel Sambuc 	case DSO: {
340ebfedea0SLionel Sambuc 	    void *sym;
341ebfedea0SLionel Sambuc 	    if (e->u.dso.dsohandle == NULL)
342ebfedea0SLionel Sambuc 		continue;
343ebfedea0SLionel Sambuc 	    sym = dlsym(e->u.dso.dsohandle, name);
344ebfedea0SLionel Sambuc 	    if (sym)
345ebfedea0SLionel Sambuc 		ret = add_symbol(context, list, sym);
346ebfedea0SLionel Sambuc 	    break;
347ebfedea0SLionel Sambuc 	}
348ebfedea0SLionel Sambuc 	case SYMBOL:
349ebfedea0SLionel Sambuc 	    if (strcmp(e->u.symbol.name, name) == 0 && e->u.symbol.type == type)
350ebfedea0SLionel Sambuc 		ret = add_symbol(context, list, e->u.symbol.symbol);
351ebfedea0SLionel Sambuc 	    break;
352ebfedea0SLionel Sambuc 	}
353ebfedea0SLionel Sambuc 	if (ret) {
354ebfedea0SLionel Sambuc 	    _krb5_plugin_free(*list);
355ebfedea0SLionel Sambuc 	    *list = NULL;
356ebfedea0SLionel Sambuc 	}
357ebfedea0SLionel Sambuc     }
358ebfedea0SLionel Sambuc 
359ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_unlock(&plugin_mutex);
360ebfedea0SLionel Sambuc     if (ret)
361ebfedea0SLionel Sambuc 	return ret;
362ebfedea0SLionel Sambuc 
363ebfedea0SLionel Sambuc     if (*list == NULL) {
364ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOENT, "Did not find a plugin for %s", name);
365ebfedea0SLionel Sambuc 	return ENOENT;
366ebfedea0SLionel Sambuc     }
367ebfedea0SLionel Sambuc 
368ebfedea0SLionel Sambuc     return 0;
369ebfedea0SLionel Sambuc }
370ebfedea0SLionel Sambuc 
371ebfedea0SLionel Sambuc void
_krb5_plugin_free(struct krb5_plugin * list)372ebfedea0SLionel Sambuc _krb5_plugin_free(struct krb5_plugin *list)
373ebfedea0SLionel Sambuc {
374ebfedea0SLionel Sambuc     struct krb5_plugin *next;
375ebfedea0SLionel Sambuc     while (list) {
376ebfedea0SLionel Sambuc 	next = list->next;
377ebfedea0SLionel Sambuc 	free(list);
378ebfedea0SLionel Sambuc 	list = next;
379ebfedea0SLionel Sambuc     }
380ebfedea0SLionel Sambuc }
381ebfedea0SLionel Sambuc /*
382ebfedea0SLionel Sambuc  * module - dict of {
383ebfedea0SLionel Sambuc  *      ModuleName = [
384ebfedea0SLionel Sambuc  *          plugin = object{
385ebfedea0SLionel Sambuc  *              array = { ptr, ctx }
386ebfedea0SLionel Sambuc  *          }
387ebfedea0SLionel Sambuc  *      ]
388ebfedea0SLionel Sambuc  * }
389ebfedea0SLionel Sambuc  */
390ebfedea0SLionel Sambuc 
391ebfedea0SLionel Sambuc static heim_dict_t modules;
392ebfedea0SLionel Sambuc 
393ebfedea0SLionel Sambuc struct plugin2 {
394ebfedea0SLionel Sambuc     heim_string_t path;
395ebfedea0SLionel Sambuc     void *dsohandle;
396ebfedea0SLionel Sambuc     heim_dict_t names;
397ebfedea0SLionel Sambuc };
398ebfedea0SLionel Sambuc 
399ebfedea0SLionel Sambuc static void
plug_dealloc(void * ptr)400ebfedea0SLionel Sambuc plug_dealloc(void *ptr)
401ebfedea0SLionel Sambuc {
402ebfedea0SLionel Sambuc     struct plugin2 *p = ptr;
403ebfedea0SLionel Sambuc     heim_release(p->path);
404ebfedea0SLionel Sambuc     heim_release(p->names);
405ebfedea0SLionel Sambuc     if (p->dsohandle)
406ebfedea0SLionel Sambuc 	dlclose(p->dsohandle);
407ebfedea0SLionel Sambuc }
408ebfedea0SLionel Sambuc 
409ebfedea0SLionel Sambuc 
410ebfedea0SLionel Sambuc void
_krb5_load_plugins(krb5_context context,const char * name,const char ** paths)411ebfedea0SLionel Sambuc _krb5_load_plugins(krb5_context context, const char *name, const char **paths)
412ebfedea0SLionel Sambuc {
413ebfedea0SLionel Sambuc #ifdef HAVE_DLOPEN
414ebfedea0SLionel Sambuc     heim_string_t s = heim_string_create(name);
415ebfedea0SLionel Sambuc     heim_dict_t module;
416ebfedea0SLionel Sambuc     struct dirent *entry;
417ebfedea0SLionel Sambuc     krb5_error_code ret;
418ebfedea0SLionel Sambuc     const char **di;
419ebfedea0SLionel Sambuc     DIR *d;
420ebfedea0SLionel Sambuc 
421ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_lock(&plugin_mutex);
422ebfedea0SLionel Sambuc 
423ebfedea0SLionel Sambuc     if (modules == NULL) {
424ebfedea0SLionel Sambuc 	modules = heim_dict_create(11);
425ebfedea0SLionel Sambuc 	if (modules == NULL) {
426ebfedea0SLionel Sambuc 	    HEIMDAL_MUTEX_unlock(&plugin_mutex);
427ebfedea0SLionel Sambuc 	    return;
428ebfedea0SLionel Sambuc 	}
429ebfedea0SLionel Sambuc     }
430ebfedea0SLionel Sambuc 
431ebfedea0SLionel Sambuc     module = heim_dict_copy_value(modules, s);
432ebfedea0SLionel Sambuc     if (module == NULL) {
433ebfedea0SLionel Sambuc 	module = heim_dict_create(11);
434ebfedea0SLionel Sambuc 	if (module == NULL) {
435ebfedea0SLionel Sambuc 	    HEIMDAL_MUTEX_unlock(&plugin_mutex);
436ebfedea0SLionel Sambuc 	    heim_release(s);
437ebfedea0SLionel Sambuc 	    return;
438ebfedea0SLionel Sambuc 	}
439ebfedea0SLionel Sambuc 	heim_dict_add_value(modules, s, module);
440ebfedea0SLionel Sambuc     }
441ebfedea0SLionel Sambuc     heim_release(s);
442ebfedea0SLionel Sambuc 
443ebfedea0SLionel Sambuc     for (di = paths; *di != NULL; di++) {
444ebfedea0SLionel Sambuc 	d = opendir(*di);
445ebfedea0SLionel Sambuc 	if (d == NULL)
446ebfedea0SLionel Sambuc 	    continue;
447ebfedea0SLionel Sambuc 	rk_cloexec_dir(d);
448ebfedea0SLionel Sambuc 
449ebfedea0SLionel Sambuc 	while ((entry = readdir(d)) != NULL) {
450ebfedea0SLionel Sambuc 	    char *n = entry->d_name;
451ebfedea0SLionel Sambuc 	    char *path = NULL;
452ebfedea0SLionel Sambuc 	    heim_string_t spath;
453ebfedea0SLionel Sambuc 	    struct plugin2 *p;
454ebfedea0SLionel Sambuc 
455ebfedea0SLionel Sambuc 	    /* skip . and .. */
456ebfedea0SLionel Sambuc 	    if (n[0] == '.' && (n[1] == '\0' || (n[1] == '.' && n[2] == '\0')))
457ebfedea0SLionel Sambuc 		continue;
458ebfedea0SLionel Sambuc 
459ebfedea0SLionel Sambuc 	    ret = 0;
460ebfedea0SLionel Sambuc #ifdef __APPLE__
461ebfedea0SLionel Sambuc 	    { /* support loading bundles on MacOS */
462ebfedea0SLionel Sambuc 		size_t len = strlen(n);
463ebfedea0SLionel Sambuc 		if (len > 7 && strcmp(&n[len - 7],  ".bundle") == 0)
464ebfedea0SLionel Sambuc 		    ret = asprintf(&path, "%s/%s/Contents/MacOS/%.*s", *di, n, (int)(len - 7), n);
465ebfedea0SLionel Sambuc 	    }
466ebfedea0SLionel Sambuc #endif
467ebfedea0SLionel Sambuc 	    if (ret < 0 || path == NULL)
468ebfedea0SLionel Sambuc 		ret = asprintf(&path, "%s/%s", *di, n);
469ebfedea0SLionel Sambuc 
470ebfedea0SLionel Sambuc 	    if (ret < 0 || path == NULL)
471ebfedea0SLionel Sambuc 		continue;
472ebfedea0SLionel Sambuc 
473ebfedea0SLionel Sambuc 	    spath = heim_string_create(n);
474ebfedea0SLionel Sambuc 	    if (spath == NULL) {
475ebfedea0SLionel Sambuc 		free(path);
476ebfedea0SLionel Sambuc 		continue;
477ebfedea0SLionel Sambuc 	    }
478ebfedea0SLionel Sambuc 
479ebfedea0SLionel Sambuc 	    /* check if already cached */
480ebfedea0SLionel Sambuc 	    p = heim_dict_copy_value(module, spath);
481ebfedea0SLionel Sambuc 	    if (p == NULL) {
482ebfedea0SLionel Sambuc 		p = heim_alloc(sizeof(*p), "krb5-plugin", plug_dealloc);
483ebfedea0SLionel Sambuc 		if (p)
484ebfedea0SLionel Sambuc 		    p->dsohandle = dlopen(path, RTLD_LOCAL|RTLD_LAZY);
485ebfedea0SLionel Sambuc 
486ebfedea0SLionel Sambuc 		if (p->dsohandle) {
487ebfedea0SLionel Sambuc 		    p->path = heim_retain(spath);
488ebfedea0SLionel Sambuc 		    p->names = heim_dict_create(11);
489ebfedea0SLionel Sambuc 		    heim_dict_add_value(module, spath, p);
490ebfedea0SLionel Sambuc 		}
491ebfedea0SLionel Sambuc 	    }
492ebfedea0SLionel Sambuc 	    heim_release(spath);
493ebfedea0SLionel Sambuc 	    heim_release(p);
494ebfedea0SLionel Sambuc 	    free(path);
495ebfedea0SLionel Sambuc 	}
496ebfedea0SLionel Sambuc 	closedir(d);
497ebfedea0SLionel Sambuc     }
498ebfedea0SLionel Sambuc     heim_release(module);
499ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_unlock(&plugin_mutex);
500ebfedea0SLionel Sambuc #endif /* HAVE_DLOPEN */
501ebfedea0SLionel Sambuc }
502ebfedea0SLionel Sambuc 
503ebfedea0SLionel Sambuc void
_krb5_unload_plugins(krb5_context context,const char * name)504ebfedea0SLionel Sambuc _krb5_unload_plugins(krb5_context context, const char *name)
505ebfedea0SLionel Sambuc {
506ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_lock(&plugin_mutex);
507ebfedea0SLionel Sambuc     heim_release(modules);
508ebfedea0SLionel Sambuc     modules = NULL;
509ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_unlock(&plugin_mutex);
510ebfedea0SLionel Sambuc }
511ebfedea0SLionel Sambuc 
512ebfedea0SLionel Sambuc /*
513ebfedea0SLionel Sambuc  *
514ebfedea0SLionel Sambuc  */
515ebfedea0SLionel Sambuc 
516ebfedea0SLionel Sambuc struct common_plugin_method {
517ebfedea0SLionel Sambuc     int			version;
518ebfedea0SLionel Sambuc     krb5_error_code	(*init)(krb5_context, void **);
519ebfedea0SLionel Sambuc     void		(*fini)(void *);
520ebfedea0SLionel Sambuc };
521ebfedea0SLionel Sambuc 
522ebfedea0SLionel Sambuc struct plug {
523ebfedea0SLionel Sambuc     void *dataptr;
524ebfedea0SLionel Sambuc     void *ctx;
525ebfedea0SLionel Sambuc };
526ebfedea0SLionel Sambuc 
527ebfedea0SLionel Sambuc static void
plug_free(void * ptr)528ebfedea0SLionel Sambuc plug_free(void *ptr)
529ebfedea0SLionel Sambuc {
530ebfedea0SLionel Sambuc     struct plug *pl = ptr;
531ebfedea0SLionel Sambuc     if (pl->dataptr) {
532ebfedea0SLionel Sambuc 	struct common_plugin_method *cpm = pl->dataptr;
533ebfedea0SLionel Sambuc 	cpm->fini(pl->ctx);
534ebfedea0SLionel Sambuc     }
535ebfedea0SLionel Sambuc }
536ebfedea0SLionel Sambuc 
537ebfedea0SLionel Sambuc struct iter_ctx {
538ebfedea0SLionel Sambuc     krb5_context context;
539ebfedea0SLionel Sambuc     heim_string_t n;
540ebfedea0SLionel Sambuc     const char *name;
541ebfedea0SLionel Sambuc     int min_version;
542ebfedea0SLionel Sambuc     heim_array_t result;
543ebfedea0SLionel Sambuc     krb5_error_code (*func)(krb5_context, const void *, void *, void *);
544ebfedea0SLionel Sambuc     void *userctx;
545ebfedea0SLionel Sambuc     krb5_error_code ret;
546ebfedea0SLionel Sambuc };
547ebfedea0SLionel Sambuc 
548ebfedea0SLionel Sambuc static void
search_modules(void * ctx,heim_object_t key,heim_object_t value)549ebfedea0SLionel Sambuc search_modules(void *ctx, heim_object_t key, heim_object_t value)
550ebfedea0SLionel Sambuc {
551ebfedea0SLionel Sambuc     struct iter_ctx *s = ctx;
552ebfedea0SLionel Sambuc     struct plugin2 *p = value;
553ebfedea0SLionel Sambuc     struct plug *pl = heim_dict_copy_value(p->names, s->n);
554ebfedea0SLionel Sambuc     struct common_plugin_method *cpm;
555ebfedea0SLionel Sambuc 
556ebfedea0SLionel Sambuc     if (pl == NULL) {
557ebfedea0SLionel Sambuc 	if (p->dsohandle == NULL)
558ebfedea0SLionel Sambuc 	    return;
559ebfedea0SLionel Sambuc 
560ebfedea0SLionel Sambuc 	pl = heim_alloc(sizeof(*pl), "struct-plug", plug_free);
561ebfedea0SLionel Sambuc 
562ebfedea0SLionel Sambuc 	cpm = pl->dataptr = dlsym(p->dsohandle, s->name);
563ebfedea0SLionel Sambuc 	if (cpm) {
564ebfedea0SLionel Sambuc 	    int ret;
565ebfedea0SLionel Sambuc 
566ebfedea0SLionel Sambuc 	    ret = cpm->init(s->context, &pl->ctx);
567ebfedea0SLionel Sambuc 	    if (ret)
568ebfedea0SLionel Sambuc 		cpm = pl->dataptr = NULL;
569ebfedea0SLionel Sambuc 	}
570ebfedea0SLionel Sambuc 	heim_dict_add_value(p->names, s->n, pl);
571ebfedea0SLionel Sambuc     } else {
572ebfedea0SLionel Sambuc 	cpm = pl->dataptr;
573ebfedea0SLionel Sambuc     }
574ebfedea0SLionel Sambuc 
575ebfedea0SLionel Sambuc     if (cpm && cpm->version >= s->min_version)
576ebfedea0SLionel Sambuc 	heim_array_append_value(s->result, pl);
577ebfedea0SLionel Sambuc 
578ebfedea0SLionel Sambuc     heim_release(pl);
579ebfedea0SLionel Sambuc }
580ebfedea0SLionel Sambuc 
581ebfedea0SLionel Sambuc static void
eval_results(heim_object_t value,void * ctx)582ebfedea0SLionel Sambuc eval_results(heim_object_t value, void *ctx)
583ebfedea0SLionel Sambuc {
584ebfedea0SLionel Sambuc     struct plug *pl = value;
585ebfedea0SLionel Sambuc     struct iter_ctx *s = ctx;
586ebfedea0SLionel Sambuc 
587ebfedea0SLionel Sambuc     if (s->ret != KRB5_PLUGIN_NO_HANDLE)
588ebfedea0SLionel Sambuc 	return;
589ebfedea0SLionel Sambuc 
590ebfedea0SLionel Sambuc     s->ret = s->func(s->context, pl->dataptr, pl->ctx, s->userctx);
591ebfedea0SLionel Sambuc }
592ebfedea0SLionel Sambuc 
593ebfedea0SLionel Sambuc krb5_error_code
_krb5_plugin_run_f(krb5_context context,const char * module,const char * name,int min_version,int flags,void * userctx,krb5_error_code (* func)(krb5_context,const void *,void *,void *))594ebfedea0SLionel Sambuc _krb5_plugin_run_f(krb5_context context,
595ebfedea0SLionel Sambuc 		   const char *module,
596ebfedea0SLionel Sambuc 		   const char *name,
597ebfedea0SLionel Sambuc 		   int min_version,
598ebfedea0SLionel Sambuc 		   int flags,
599ebfedea0SLionel Sambuc 		   void *userctx,
600ebfedea0SLionel Sambuc 		   krb5_error_code (*func)(krb5_context, const void *, void *, void *))
601ebfedea0SLionel Sambuc {
602ebfedea0SLionel Sambuc     heim_string_t m = heim_string_create(module);
603ebfedea0SLionel Sambuc     heim_dict_t dict;
604ebfedea0SLionel Sambuc     struct iter_ctx s;
605ebfedea0SLionel Sambuc 
606ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_lock(&plugin_mutex);
607ebfedea0SLionel Sambuc 
608ebfedea0SLionel Sambuc     dict = heim_dict_copy_value(modules, m);
609ebfedea0SLionel Sambuc     heim_release(m);
610ebfedea0SLionel Sambuc     if (dict == NULL) {
611ebfedea0SLionel Sambuc 	HEIMDAL_MUTEX_unlock(&plugin_mutex);
612ebfedea0SLionel Sambuc 	return KRB5_PLUGIN_NO_HANDLE;
613ebfedea0SLionel Sambuc     }
614ebfedea0SLionel Sambuc 
615ebfedea0SLionel Sambuc     s.context = context;
616ebfedea0SLionel Sambuc     s.name = name;
617ebfedea0SLionel Sambuc     s.n = heim_string_create(name);
618ebfedea0SLionel Sambuc     s.min_version = min_version;
619ebfedea0SLionel Sambuc     s.result = heim_array_create();
620ebfedea0SLionel Sambuc     s.func = func;
621ebfedea0SLionel Sambuc     s.userctx = userctx;
622ebfedea0SLionel Sambuc 
623ebfedea0SLionel Sambuc     heim_dict_iterate_f(dict, search_modules, &s);
624ebfedea0SLionel Sambuc 
625ebfedea0SLionel Sambuc     heim_release(dict);
626ebfedea0SLionel Sambuc 
627ebfedea0SLionel Sambuc     HEIMDAL_MUTEX_unlock(&plugin_mutex);
628ebfedea0SLionel Sambuc 
629ebfedea0SLionel Sambuc     s.ret = KRB5_PLUGIN_NO_HANDLE;
630ebfedea0SLionel Sambuc 
631ebfedea0SLionel Sambuc     heim_array_iterate_f(s.result, eval_results, &s);
632ebfedea0SLionel Sambuc 
633ebfedea0SLionel Sambuc     heim_release(s.result);
634ebfedea0SLionel Sambuc     heim_release(s.n);
635ebfedea0SLionel Sambuc 
636ebfedea0SLionel Sambuc     return s.ret;
637ebfedea0SLionel Sambuc }
638