s4:heimdal: import lorikeet-heimdal-200906080040 (commit 904d0124b46eed7a8ad6e5b73e89...
[amitay/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
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 krb5_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 *sysplugin_dirs[] =  { 
58     LIBDIR "/plugin/krb5",
59 #ifdef __APPLE__
60     "/System/Library/KerberosPlugins/KerberosFrameworkPlugins",
61 #endif
62     NULL
63 };
64
65 /*
66  *
67  */
68
69 void *
70 _krb5_plugin_get_symbol(struct krb5_plugin *p)
71 {
72     return p->symbol;
73 }
74
75 struct krb5_plugin *
76 _krb5_plugin_get_next(struct krb5_plugin *p)
77 {
78     return p->next;
79 }
80
81 /*
82  *
83  */
84
85 #ifdef HAVE_DLOPEN
86
87 static krb5_error_code
88 loadlib(krb5_context context,
89         enum krb5_plugin_type type,
90         const char *name,
91         const char *lib,
92         struct krb5_plugin **e)
93 {
94     *e = calloc(1, sizeof(**e));
95     if (*e == NULL) {
96         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
97         return ENOMEM;
98     }
99
100 #ifndef RTLD_LAZY
101 #define RTLD_LAZY 0
102 #endif
103 #ifndef RTLD_LOCAL
104 #define RTLD_LOCAL 0
105 #endif
106
107     (*e)->dsohandle = dlopen(lib, RTLD_LOCAL|RTLD_LAZY);
108     if ((*e)->dsohandle == NULL) {
109         free(*e);
110         *e = NULL;
111         krb5_set_error_message(context, ENOMEM, "Failed to load %s: %s",
112                                lib, dlerror());
113         return ENOMEM;
114     }
115
116     /* dlsym doesn't care about the type */
117     (*e)->symbol = dlsym((*e)->dsohandle, name);
118     if ((*e)->symbol == NULL) {
119         dlclose((*e)->dsohandle);
120         free(*e);
121         krb5_clear_error_message(context);
122         return ENOMEM;
123     }
124
125     return 0;
126 }
127 #endif /* HAVE_DLOPEN */
128
129 /**
130  * Register a plugin symbol name of specific type.
131  * @param context a Keberos context
132  * @param type type of plugin symbol
133  * @param name name of plugin symbol
134  * @param symbol a pointer to the named symbol
135  * @return In case of error a non zero error com_err error is returned
136  * and the Kerberos error string is set.
137  *
138  * @ingroup krb5_support
139  */
140
141 krb5_error_code
142 krb5_plugin_register(krb5_context context,
143                      enum krb5_plugin_type type,
144                      const char *name,
145                      void *symbol)
146 {
147     struct plugin *e;
148
149     /* check for duplicates */
150     for (e = registered; e != NULL; e = e->next)
151         if (e->type == type && strcmp(e->name,name)== 0 && e->symbol == symbol)
152             return 0;
153
154     e = calloc(1, sizeof(*e));
155     if (e == NULL) {
156         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
157         return ENOMEM;
158     }
159     e->type = type;
160     e->name = strdup(name);
161     if (e->name == NULL) {
162         free(e);
163         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
164         return ENOMEM;
165     }
166     e->symbol = symbol;
167
168     HEIMDAL_MUTEX_lock(&plugin_mutex);
169     e->next = registered;
170     registered = e;
171     HEIMDAL_MUTEX_unlock(&plugin_mutex);
172
173     return 0;
174 }
175
176 krb5_error_code
177 _krb5_plugin_find(krb5_context context,
178                   enum krb5_plugin_type type,
179                   const char *name,
180                   struct krb5_plugin **list)
181 {
182     struct krb5_plugin *e;
183     struct plugin *p;
184     krb5_error_code ret;
185     char **dirs = NULL, **di;
186     struct dirent *entry;
187     char *path;
188     DIR *d = NULL;
189
190     *list = NULL;
191
192     HEIMDAL_MUTEX_lock(&plugin_mutex);
193
194     for (p = registered; p != NULL; p = p->next) {
195         if (p->type != type || strcmp(p->name, name) != 0)
196             continue;
197
198         e = calloc(1, sizeof(*e));
199         if (e == NULL) {
200             HEIMDAL_MUTEX_unlock(&plugin_mutex);
201             ret = ENOMEM;
202             krb5_set_error_message(context, ret, "malloc: out of memory");
203             goto out;
204         }
205         e->symbol = p->symbol;
206         e->dsohandle = NULL;
207         e->next = *list;
208         *list = e;
209     }
210     HEIMDAL_MUTEX_unlock(&plugin_mutex);
211
212 #ifdef HAVE_DLOPEN
213
214     dirs = krb5_config_get_strings(context, NULL, "libdefaults",
215                                    "plugin_dir", NULL);
216     if (dirs == NULL)
217         dirs = rk_UNCONST(sysplugin_dirs);
218
219     for (di = dirs; *di != NULL; di++) {
220
221         d = opendir(*di);
222         if (d == NULL)
223             continue;
224         rk_cloexec(dirfd(d));
225
226         while ((entry = readdir(d)) != NULL) {
227             char *n = entry->d_name;
228
229             /* skip . and .. */
230             if (n[0] == '.' && (n[1] == '\0' || (n[1] == '.' && n[2] == '\0')))
231                 continue;
232
233             path = NULL;
234 #ifdef __APPLE__
235             { /* support loading bundles on MacOS */
236                 size_t len = strlen(n);
237                 if (len > 7 && strcmp(&n[len - 7],  ".bundle") == 0)
238                     asprintf(&path, "%s/%s/Contents/MacOS/%.*s", *di, n, (int)(len - 7), n);
239             }
240 #endif
241             if (path == NULL)
242                 asprintf(&path, "%s/%s", *di, n);
243
244             if (path == NULL) {
245                 ret = ENOMEM;
246                 krb5_set_error_message(context, ret, "malloc: out of memory");
247                 goto out;
248             }
249             ret = loadlib(context, type, name, path, &e);
250             free(path);
251             if (ret)
252                 continue;
253         
254             e->next = *list;
255             *list = e;
256         }
257         closedir(d);
258     }
259     if (dirs != rk_UNCONST(sysplugin_dirs))
260         krb5_config_free_strings(dirs);
261 #endif /* HAVE_DLOPEN */
262
263     if (*list == NULL) {
264         krb5_set_error_message(context, ENOENT, "Did not find a plugin for %s", name);
265         return ENOENT;
266     }
267
268     return 0;
269
270 out:
271     if (dirs != rk_UNCONST(sysplugin_dirs))
272         krb5_config_free_strings(dirs);
273     if (d)
274         closedir(d);
275     _krb5_plugin_free(*list);
276     *list = NULL;
277
278     return ret;
279 }
280
281 void
282 _krb5_plugin_free(struct krb5_plugin *list)
283 {
284     struct krb5_plugin *next;
285     while (list) {
286         next = list->next;
287         if (list->dsohandle)
288             dlclose(list->dsohandle);
289         free(list);
290         list = next;
291     }
292 }