r20640: Commit part 2/2
[samba.git] / source4 / heimdal / lib / krb5 / plugin.c
1 /*
2  * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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. 
20  *
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. 
32  */
33
34 #include "krb5_locl.h"
35 RCSID("$Id: plugin.c,v 1.4 2007/01/09 17:46:01 lha Exp $");
36 #ifdef HAVE_DLFCN_H
37 #include <dlfcn.h>
38 #endif
39 #include <dirent.h>
40
41 struct krb5_plugin {
42     void *symbol;
43     void *dsohandle;
44     struct krb5_plugin *next;
45 };
46
47 struct plugin {
48     enum plugin_type type;
49     void *name;
50     void *symbol;
51     struct plugin *next;
52 };
53
54 static HEIMDAL_MUTEX plugin_mutex = HEIMDAL_MUTEX_INITIALIZER;
55 static struct plugin *registered = NULL;
56
57 static const char *plugin_dir = LIBDIR "/plugin/krb5";
58
59 /*
60  *
61  */
62
63 void *
64 _krb5_plugin_get_symbol(struct krb5_plugin *p)
65 {
66     return p->symbol;
67 }
68
69 struct krb5_plugin *
70 _krb5_plugin_get_next(struct krb5_plugin *p)
71 {
72     return p->next;
73 }
74
75 /*
76  *
77  */
78
79 static krb5_error_code
80 loadlib(krb5_context context,
81         enum plugin_type type,
82         const char *name,
83         const char *lib,
84         struct krb5_plugin **e)
85 {
86     *e = calloc(1, sizeof(**e));
87     if (*e == NULL) {
88         krb5_set_error_string(context, "out of memory");
89         return ENOMEM;
90     }
91
92 #ifndef RTLD_LAZY
93 #define RTLD_LAZY 0
94 #endif
95
96     (*e)->dsohandle = dlopen(lib, RTLD_LAZY);
97     if ((*e)->dsohandle == NULL) {
98         free(*e);
99         krb5_set_error_string(context, "Failed to load %s: %s", 
100                               lib, dlerror());
101         return ENOMEM;
102     }
103
104     /* dlsym doesn't care about the type */
105     (*e)->symbol = dlsym((*e)->dsohandle, name);
106     if ((*e)->symbol == NULL) {
107         dlclose((*e)->dsohandle);
108         free(*e);
109         krb5_clear_error_string(context);
110         return ENOMEM;
111     }
112
113     return 0;
114 }
115
116 krb5_error_code
117 _krb5_plugin_register(krb5_context context,
118                       enum plugin_type type,
119                       const char *name, 
120                       void *symbol)
121 {
122     struct plugin *e;
123
124     e = calloc(1, sizeof(*e));
125     if (e == NULL) {
126         krb5_set_error_string(context, "out of memory");
127         return ENOMEM;
128     }
129     e->type = type;
130     e->name = strdup(name);
131     if (e->name == NULL) {
132         free(e);
133         krb5_set_error_string(context, "out of memory");
134         return ENOMEM;
135     }
136     e->symbol = symbol;
137
138     HEIMDAL_MUTEX_lock(&plugin_mutex);
139     e->next = registered;
140     registered = e;
141     HEIMDAL_MUTEX_unlock(&plugin_mutex);
142
143     return 0;
144 }
145
146 krb5_error_code
147 _krb5_plugin_find(krb5_context context,
148                   enum plugin_type type,
149                   const char *name, 
150                   struct krb5_plugin **list)
151 {
152     struct krb5_plugin *e;
153     struct plugin *p;
154     krb5_error_code ret;
155     char *sysdirs[2] = { NULL, NULL };
156     char **dirs = NULL, **di;
157     struct dirent *entry;
158     char *path;
159     DIR *d = NULL;
160
161     *list = NULL;
162
163     HEIMDAL_MUTEX_lock(&plugin_mutex);
164
165     for (p = registered; p != NULL; p = p->next) {
166         if (p->type != type || strcmp(p->name, name) != 0)
167             continue;
168
169         e = calloc(1, sizeof(*e));
170         if (e == NULL) {
171             HEIMDAL_MUTEX_unlock(&plugin_mutex);
172             krb5_set_error_string(context, "out of memory");
173             ret = ENOMEM;
174             goto out;
175         }
176         e->symbol = p->symbol;
177         e->dsohandle = NULL;
178         e->next = *list;
179         *list = e;
180     }
181     HEIMDAL_MUTEX_unlock(&plugin_mutex);
182
183     dirs = krb5_config_get_strings(context, NULL, "libdefaults", 
184                                    "plugin_dir", NULL);
185     if (dirs == NULL) {
186         sysdirs[0] = rk_UNCONST(plugin_dir);
187         dirs = sysdirs;
188     }
189
190     for (di = dirs; *di != NULL; di++) {
191
192         d = opendir(*di);
193         if (d == NULL)
194             continue;
195
196         while ((entry = readdir(d)) != NULL) {
197             asprintf(&path, "%s/%s", *di, entry->d_name);
198             if (path == NULL) {
199                 krb5_set_error_string(context, "out of memory");
200                 ret = ENOMEM;
201                 goto out;
202             }
203             ret = loadlib(context, type, name, path, &e);
204             free(path);
205             if (ret)
206                 continue;
207             
208             e->next = *list;
209             *list = e;
210         }
211         closedir(d);
212     }
213     if (dirs != sysdirs)
214         krb5_config_free_strings(dirs);
215
216     if (*list == NULL) {
217         krb5_set_error_string(context, "Did not find a plugin for %s", name);
218         return ENOENT;
219     }
220
221     return 0;
222
223 out:
224     if (dirs && dirs != sysdirs)
225         krb5_config_free_strings(dirs);
226     if (d)
227         closedir(d);
228     _krb5_plugin_free(*list);
229     *list = NULL;
230
231     return ret;
232 }
233
234 void
235 _krb5_plugin_free(struct krb5_plugin *list)
236 {
237     struct krb5_plugin *next;
238     while (list) {
239         next = list->next;
240         if (list->dsohandle)
241             dlclose(list->dsohandle);
242         free(list);
243         list = next;
244     }
245 }
246