]> git.samba.org - samba.git/blob - source4/heimdal/lib/krb5/plugin.c
s4:heimdal: import lorikeet-heimdal-201011102149 (commit 5734d03c20e104c8f45533d07f2a...
[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     struct krb5_plugin *next;
44 };
45
46 struct plugin {
47     enum { DSO, SYMBOL } type;
48     union {
49         struct {
50             char *path;
51             void *dsohandle;
52         } dso;
53         struct {
54             enum krb5_plugin_type type;
55             char *name;
56             char *symbol;
57         } symbol;
58     } u;
59     struct plugin *next;
60 };
61
62 static HEIMDAL_MUTEX plugin_mutex = HEIMDAL_MUTEX_INITIALIZER;
63 static struct plugin *registered = NULL;
64 static int plugins_needs_scan = 1;
65
66 static const char *sysplugin_dirs[] =  { 
67     LIBDIR "/plugin/krb5",
68 #ifdef __APPLE__
69     "/System/Library/KerberosPlugins/KerberosFrameworkPlugins",
70 #endif
71     NULL
72 };
73
74 /*
75  *
76  */
77
78 void *
79 _krb5_plugin_get_symbol(struct krb5_plugin *p)
80 {
81     return p->symbol;
82 }
83
84 struct krb5_plugin *
85 _krb5_plugin_get_next(struct krb5_plugin *p)
86 {
87     return p->next;
88 }
89
90 /*
91  *
92  */
93
94 #ifdef HAVE_DLOPEN
95
96 static krb5_error_code
97 loadlib(krb5_context context, char *path)
98 {
99     struct plugin *e;
100
101     e = calloc(1, sizeof(*e));
102     if (e == NULL) {
103         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
104         free(path);
105         return ENOMEM;
106     }
107
108 #ifndef RTLD_LAZY
109 #define RTLD_LAZY 0
110 #endif
111 #ifndef RTLD_LOCAL
112 #define RTLD_LOCAL 0
113 #endif
114     e->type = DSO;
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;
118
119     e->next = registered;
120     registered = e;
121
122     return 0;
123 }
124 #endif /* HAVE_DLOPEN */
125
126 /**
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.
134  *
135  * @ingroup krb5_support
136  */
137
138 krb5_error_code
139 krb5_plugin_register(krb5_context context,
140                      enum krb5_plugin_type type,
141                      const char *name,
142                      void *symbol)
143 {
144     struct plugin *e;
145
146     HEIMDAL_MUTEX_lock(&plugin_mutex);
147
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);
154             return 0;
155         }
156     }
157
158     e = calloc(1, sizeof(*e));
159     if (e == NULL) {
160         HEIMDAL_MUTEX_unlock(&plugin_mutex);
161         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
162         return ENOMEM;
163     }
164     e->type = SYMBOL;
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);
169         free(e);
170         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
171         return ENOMEM;
172     }
173     e->u.symbol.symbol = symbol;
174
175     e->next = registered;
176     registered = e;
177     HEIMDAL_MUTEX_unlock(&plugin_mutex);
178
179     return 0;
180 }
181
182 static krb5_error_code
183 load_plugins(krb5_context context)
184 {
185     struct plugin *e;
186     krb5_error_code ret;
187     char **dirs = NULL, **di;
188     struct dirent *entry;
189     char *path;
190     DIR *d = NULL;
191
192     if (!plugins_needs_scan)
193         return 0;
194     plugins_needs_scan = 0;
195
196 #ifdef HAVE_DLOPEN
197
198     dirs = krb5_config_get_strings(context, NULL, "libdefaults",
199                                    "plugin_dir", NULL);
200     if (dirs == NULL)
201         dirs = rk_UNCONST(sysplugin_dirs);
202
203     for (di = dirs; *di != NULL; di++) {
204 #ifdef KRB5_USE_PATH_TOKENS
205         {
206             char * dir = NULL;
207
208             if (_krb5_expand_path_tokens(context, *di, &dir))
209                 continue;
210             d = opendir(dir);
211
212             free(dir);
213         }
214 #else
215         d = opendir(*di);
216 #endif
217         if (d == NULL)
218             continue;
219         rk_cloexec_dir(d);
220
221         while ((entry = readdir(d)) != NULL) {
222             char *n = entry->d_name;
223
224             /* skip . and .. */
225             if (n[0] == '.' && (n[1] == '\0' || (n[1] == '.' && n[2] == '\0')))
226                 continue;
227
228             path = NULL;
229             ret = 0;
230 #ifdef __APPLE__
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);
235             }
236 #endif
237             if (ret < 0 || path == NULL)
238                 ret = asprintf(&path, "%s/%s", *di, n);
239
240             if (ret < 0 || path == NULL) {
241                 ret = ENOMEM;
242                 krb5_set_error_message(context, ret, "malloc: out of memory");
243                 return ret;
244             }
245
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)
249                     break;
250             if (e) {
251                 free(path);
252             } else {
253                 loadlib(context, path); /* store or frees path */
254             }
255         }
256         closedir(d);
257     }
258     if (dirs != rk_UNCONST(sysplugin_dirs))
259         krb5_config_free_strings(dirs);
260 #endif /* HAVE_DLOPEN */
261     return 0;
262 }
263
264 static krb5_error_code
265 add_symbol(krb5_context context, struct krb5_plugin **list, void *symbol)
266 {
267     struct krb5_plugin *e;
268     
269     e = calloc(1, sizeof(*e));
270     if (e == NULL) {
271         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
272         return ENOMEM;
273     }
274     e->symbol = symbol;
275     e->next = *list;
276     *list = e;
277     return 0;
278 }
279
280 krb5_error_code
281 _krb5_plugin_find(krb5_context context,
282                   enum krb5_plugin_type type,
283                   const char *name,
284                   struct krb5_plugin **list)
285 {
286     struct plugin *e;
287     krb5_error_code ret;
288
289     *list = NULL;
290
291     HEIMDAL_MUTEX_lock(&plugin_mutex);
292     
293     load_plugins(context);
294
295     for (ret = 0, e = registered; e != NULL; e = e->next) {
296         switch(e->type) {
297         case DSO: {
298             void *sym;
299             if (e->u.dso.dsohandle == NULL)
300                 continue;
301             sym = dlsym(e->u.dso.dsohandle, name);
302             if (sym)
303                 ret = add_symbol(context, list, sym);
304             break;
305         }
306         case SYMBOL:
307             if (strcmp(e->u.symbol.name, name) == 0 && e->u.symbol.type == type)
308                 ret = add_symbol(context, list, e->u.symbol.symbol);
309             break;
310         }
311         if (ret) {
312             _krb5_plugin_free(*list);
313             *list = NULL;
314         }
315     }
316
317     HEIMDAL_MUTEX_unlock(&plugin_mutex);
318     if (ret)
319         return ret;
320
321     if (*list == NULL) {
322         krb5_set_error_message(context, ENOENT, "Did not find a plugin for %s", name);
323         return ENOENT;
324     }
325
326     return 0;
327 }
328
329 void
330 _krb5_plugin_free(struct krb5_plugin *list)
331 {
332     struct krb5_plugin *next;
333     while (list) {
334         next = list->next;
335         free(list);
336         list = next;
337     }
338 }
339 /*
340  * module - dict of {
341  *      ModuleName = [
342  *          plugin = object{
343  *              array = { ptr, ctx }
344  *          }
345  *      ]
346  * }
347  */
348
349 static heim_dict_t modules;
350
351 struct plugin2 {
352     heim_string_t path;
353     void *dsohandle;
354     heim_dict_t names;
355 };
356
357 static void
358 plug_dealloc(void *ptr)
359 {
360     struct plugin2 *p = ptr;
361     heim_release(p->path);
362     heim_release(p->names);
363     if (p->dsohandle)
364         dlclose(p->dsohandle);
365 }
366
367
368 void
369 _krb5_load_plugins(krb5_context context, const char *name, const char **paths)
370 {
371 #ifdef HAVE_DLOPEN
372     heim_string_t s = heim_string_create(name);
373     heim_dict_t module;
374     struct dirent *entry;
375     krb5_error_code ret;
376     const char **di;
377     DIR *d;
378
379     HEIMDAL_MUTEX_lock(&plugin_mutex);
380
381     if (modules == NULL) {
382         modules = heim_dict_create(11);
383         if (modules == NULL) {
384             HEIMDAL_MUTEX_unlock(&plugin_mutex);
385             return;
386         }
387     }
388
389     module = heim_dict_copy_value(modules, s);
390     if (module == NULL) {
391         module = heim_dict_create(11);
392         if (module == NULL) {
393             HEIMDAL_MUTEX_unlock(&plugin_mutex);
394             heim_release(s);
395             return;
396         }
397         heim_dict_add_value(modules, s, module);
398     }
399     heim_release(s);
400
401     for (di = paths; *di != NULL; di++) {
402         d = opendir(*di);
403         if (d == NULL)
404             continue;
405         rk_cloexec_dir(d);
406
407         while ((entry = readdir(d)) != NULL) {
408             char *n = entry->d_name;
409             char *path = NULL;
410             heim_string_t spath;
411             struct plugin2 *p;
412
413             /* skip . and .. */
414             if (n[0] == '.' && (n[1] == '\0' || (n[1] == '.' && n[2] == '\0')))
415                 continue;
416
417             ret = 0;
418 #ifdef __APPLE__
419             { /* support loading bundles on MacOS */
420                 size_t len = strlen(n);
421                 if (len > 7 && strcmp(&n[len - 7],  ".bundle") == 0)
422                     ret = asprintf(&path, "%s/%s/Contents/MacOS/%.*s", *di, n, (int)(len - 7), n);
423             }
424 #endif
425             if (ret < 0 || path == NULL)
426                 ret = asprintf(&path, "%s/%s", *di, n);
427
428             if (ret < 0 || path == NULL)
429                 continue;
430
431             spath = heim_string_create(n);
432             if (spath == NULL) {
433                 free(path);
434                 continue;
435             }
436
437             /* check if already cached */
438             p = heim_dict_copy_value(module, spath);
439             if (p == NULL) {
440                 p = heim_alloc(sizeof(*p), "krb5-plugin", plug_dealloc);
441                 if (p)
442                     p->dsohandle = dlopen(path, RTLD_LOCAL|RTLD_LAZY);
443
444                 if (p->dsohandle) {
445                     p->path = heim_retain(spath);
446                     p->names = heim_dict_create(11);
447                     heim_dict_add_value(module, spath, p);
448                 }
449             }
450             heim_release(spath);
451             heim_release(p);
452             free(path);
453         }
454         closedir(d);
455     }
456     heim_release(module);
457     HEIMDAL_MUTEX_unlock(&plugin_mutex);
458 #endif /* HAVE_DLOPEN */
459 }
460
461 void
462 _krb5_unload_plugins(krb5_context context, const char *name)
463 {
464     HEIMDAL_MUTEX_lock(&plugin_mutex);
465     heim_release(modules);
466     modules = NULL;
467     HEIMDAL_MUTEX_unlock(&plugin_mutex);
468 }
469
470 /*
471  *
472  */
473
474 struct common_plugin_method {
475     int                 version;
476     krb5_error_code     (*init)(krb5_context, void **);
477     void                (*fini)(void *);
478 };
479
480 struct plug {
481     void *dataptr;
482     void *ctx;
483 };
484
485 static void
486 plug_free(void *ptr)
487 {
488     struct plug *pl = ptr;
489     if (pl->dataptr) {
490         struct common_plugin_method *cpm = pl->dataptr;
491         cpm->fini(pl->ctx);
492     }
493 }
494
495 struct iter_ctx {
496     krb5_context context;
497     heim_string_t n;
498     const char *name;
499     int min_version;
500     heim_array_t result;
501     krb5_error_code (*func)(krb5_context, const void *, void *, void *);
502     void *userctx;
503     krb5_error_code ret;
504 };
505
506 static void
507 search_modules(void *ctx, heim_object_t key, heim_object_t value)
508 {
509     struct iter_ctx *s = ctx;
510     struct plugin2 *p = value;
511     struct plug *pl = heim_dict_copy_value(p->names, s->n);
512     struct common_plugin_method *cpm;
513
514     if (pl == NULL) {
515         if (p->dsohandle == NULL)
516             return;
517
518         pl = heim_alloc(sizeof(*pl), "struct-plug", plug_free);
519
520         cpm = pl->dataptr = dlsym(p->dsohandle, s->name);
521         if (cpm) {
522             int ret;
523
524             ret = cpm->init(s->context, &pl->ctx);
525             if (ret)
526                 cpm = pl->dataptr = NULL;
527         }
528         heim_dict_add_value(p->names, s->n, pl);
529     } else {
530         cpm = pl->dataptr;
531     }
532
533     if (cpm && cpm->version >= s->min_version)
534         heim_array_append_value(s->result, pl);
535
536     heim_release(pl);
537 }
538
539 static void
540 eval_results(heim_object_t value, void *ctx)
541 {
542     struct plug *pl = value;
543     struct iter_ctx *s = ctx;
544
545     if (s->ret != KRB5_PLUGIN_NO_HANDLE)
546         return;
547
548     s->ret = s->func(s->context, pl->dataptr, pl->ctx, s->userctx);
549 }
550
551 krb5_error_code
552 _krb5_plugin_run_f(krb5_context context,
553                    const char *module,
554                    const char *name,
555                    int min_version,
556                    int flags,
557                    void *userctx,
558                    krb5_error_code (*func)(krb5_context, const void *, void *, void *))
559 {
560     heim_string_t m = heim_string_create(module);
561     heim_dict_t dict;
562     struct iter_ctx s;
563
564     HEIMDAL_MUTEX_lock(&plugin_mutex);
565
566     dict = heim_dict_copy_value(modules, m);
567     heim_release(m);
568     if (dict == NULL) {
569         HEIMDAL_MUTEX_unlock(&plugin_mutex);
570         return KRB5_PLUGIN_NO_HANDLE;
571     }
572
573     s.context = context;
574     s.name = name;
575     s.n = heim_string_create(name);
576     s.min_version = min_version;
577     s.result = heim_array_create();
578     s.func = func;
579     s.userctx = userctx;
580
581     heim_dict_iterate_f(dict, search_modules, &s);
582
583     heim_release(dict);
584
585     HEIMDAL_MUTEX_unlock(&plugin_mutex);
586
587     s.ret = KRB5_PLUGIN_NO_HANDLE;
588
589     heim_array_iterate_f(s.result, eval_results, &s);
590
591     heim_release(s.result);
592     heim_release(s.n);
593
594     return s.ret;
595 }