summaryrefslogtreecommitdiffstats
path: root/src/util/support/plugins.c
blob: 320dd6e839278db57b570c693f35f6bfd03e939b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * util/support/plugins.c
 *
 * Copyright 2006 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 *
 * Plugin module support, and shims around dlopen/whatever.
 */

#include "k5-plugin.h"
#if USE_DLOPEN
#include <dlfcn.h>
#endif
#include <stdio.h>
#include <sys/types.h>
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <stdarg.h>
static void Tprintf (const char *fmt, ...)
{
#ifdef DEBUG
    va_list va;
    va_start (va, fmt);
    vfprintf (stderr, fmt, va);
    va_end (va);
#endif
}

struct plugin_file_handle {
#if USE_DLOPEN
    void *dlhandle;
#define NULL_HANDLE(X) ((X)->dlhandle == NULL)
#define MAKE_NULL_HANDLE(X) ((X)->dlhandle = NULL)
/* #elif _WIN32 ... */
#else
    char dummy;
#define NULL_HANDLE(X) (1)
#define MAKE_NULL_HANDLE(X) (0)
#endif
};

int32_t KRB5_CALLCONV
krb5int_open_plugin (const char *filename, struct plugin_file_handle **h)
{
#if USE_DLOPEN
    struct plugin_file_handle *htmp;
    void *handle;

    handle = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
    if (handle == NULL) {
	const char *e;
	e = dlerror();
	/* XXX copy and save away */
	return ENOENT;		/* XXX */
    }
    htmp = malloc (sizeof (*htmp));
    if (htmp == NULL) {
	int err = errno;
	dlclose(handle);
	return err;
    }
    *h = htmp;
    htmp->dlhandle = handle;
    return 0;
/* #elif _WIN32 */
#else
    return ENOENT;
#endif
}

int32_t KRB5_CALLCONV
krb5int_get_plugin_data (struct plugin_file_handle *h, const char *csymname,
			 void **ptr)
{
#if USE_DLOPEN
    void *sym;
    /* XXX Do we need to add a leading "_" to the symbol name on any
       modern platforms?  */
    sym = dlsym(h->dlhandle, csymname);
    if (sym == NULL) {
	const char *e;
	e = dlerror();
	return ENOENT;
    }
    *ptr = sym;
    return 0;
/* #elif _WIN32 */
#else
    return ENOENT;
#endif
}

int32_t KRB5_CALLCONV
krb5int_get_plugin_func (struct plugin_file_handle *h, const char *csymname,
			 void (**ptr)())
{
    /* This code should do for any systems where function and data
       symbols are handled the same.  Note that this means there's no
       function version of (say) dlsym, *and* the symbol-prefix
       handling is the same for both data and functions.  (And the
       casting we do here works, etc.)  */
    void *dptr;
    int32_t err;

    err = krb5int_get_plugin_data (h, csymname, &dptr);
    if (err == 0)
	*ptr = (void (*)()) dptr;
    return err;
}

void KRB5_CALLCONV
krb5int_close_plugin (struct plugin_file_handle *h)
{
#if USE_DLOPEN
    dlclose(h->dlhandle);
    h->dlhandle = NULL;
    free (h);
/* #elif _WIN32 */
#endif
}

/* autoconf docs suggest using this preference order */
#if HAVE_DIRENT_H || USE_DIRENT_H
#include <dirent.h>
#define NAMELEN(D) strlen((D)->d_name)
#else
#define dirent direct
#define NAMELEN(D) ((D)->d->namlen)
#if HAVE_SYS_NDIR_H
# include <sys/ndir.h>
#elif HAVE_SYS_DIR_H
# include <sys/dir.h>
#elif HAVE_NDIR_H
# include <ndir.h>
#endif
#endif


#ifdef HAVE_STRERROR_R
#define ERRSTR(ERR, BUF) \
    (strerror_r (ERR, BUF, sizeof(BUF)) == 0 ? BUF : strerror (ERR))
#else
#define ERRSTR(ERR, BUF) \
    (strerror (ERR))
#endif

int32_t KRB5_CALLCONV
krb5int_open_plugin_dir (const char *dirname,
			 struct plugin_dir_handle *dirhandle)
{
    /* Q: Should names be sorted in some way first?  */
    /* XXX This should be a portable directory-scanning routine which
       calls on the above routines; shouldn't be calling dlopen
       directly here.  */
#if USE_DLOPEN
    DIR *dir;
    struct dirent *d;
    struct plugin_file_handle *h, *newh, handle;
    int nh;
    int error = 0;
    char path[MAXPATHLEN];
    char errbuf[1024];

    h = NULL;
    nh = 0;
    Tprintf("opening plugin directory '%s' to scan...\n", dirname);
    dir = opendir(dirname);
    if (dir == NULL) {
	error = errno;
	Tprintf("-> error %d/%s\n", error, ERRSTR(error, errbuf));
	if (error == ENOENT)
	    return 0;
	return error;
    }
    do {
	size_t len;
	struct stat statbuf;

	d = readdir (dir);
	if (d == NULL)
	    break;
	len = NAMELEN(d);
	if (strlen(dirname) + len + 2 > sizeof(path))
	    continue;
	sprintf(path, "%s/%*s", dirname, (int) len, d->d_name);
	/* Optimization: Linux includes a file type field in the
	   directory structure.  */
	if (stat(path, &statbuf) < 0) {
	    Tprintf("stat(%s): %s\n", path, ERRSTR(errno, errbuf));
	    continue;
	}
	if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
	    Tprintf("stat(%s): not a regular file\n", path);
	    continue;
	}
	Tprintf("trying to dlopen '%s'\n", path);
	handle.dlhandle = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
	if (handle.dlhandle == NULL) {
	    const char *e = dlerror();
	    Tprintf("dlopen error: %s\n", e);
	    /* dlerror(); */
	    continue;
	} else {
	    Tprintf("dlopen succeeds: %p\n", handle.dlhandle);
	}
	newh = realloc (h, (nh+1) * sizeof(*h));
	if (newh == NULL) {
	    int i;
	close_and_return_errno:
	    error = errno;
	    for (i = 0; i < nh; i++)
		dlclose(h[i].dlhandle);
	    free(h);
	    closedir(dir);
	    return error;
	}
	h = newh;
	h[nh] = handle;
	nh++;
    } while (1);
    Tprintf("done scanning plugin directory\n");
    newh = realloc (h, (nh+1) * sizeof(*h));
    if (newh == NULL)
	goto close_and_return_errno;
    h = newh;
    MAKE_NULL_HANDLE (&h[nh]);
    dirhandle->files = h;
    closedir(dir);
    return 0;
/* #elif _WIN32 */
#else
    dirhandle->files = NULL;
    return 0;
#endif
}

void KRB5_CALLCONV
krb5int_close_plugin_dir (struct plugin_dir_handle *dirhandle)
{
#if USE_DLOPEN
    struct plugin_file_handle *h;
    if (dirhandle->files == NULL)
	return;
    for (h = dirhandle->files; !NULL_HANDLE (h); h++) {
	dlclose (h->dlhandle);
    }
    free(dirhandle->files);
    dirhandle->files = NULL;
#endif
}

void KRB5_CALLCONV
krb5int_free_plugin_dir_data (void **ptrs)
{
    /* Nothing special to be done per pointer.  */
    free(ptrs);
}

int32_t KRB5_CALLCONV
krb5int_get_plugin_dir_data (struct plugin_dir_handle *dirhandle,
			     const char *symname,
			     void ***ptrs)
{
    void **p, **newp, *sym;
    int count, i, err;

    /* XXX Do we need to add a leading "_" to the symbol name on any
       modern platforms?  */

    Tprintf("get_plugin_data_sym(%s)\n", symname);
    p = 0;
    count = 0;
    if (dirhandle == NULL || dirhandle->files == NULL)
	goto skip_loop;
    for (i = 0; !NULL_HANDLE (&dirhandle->files[i]); i++) {
	int32_t kerr;
	sym = NULL;
	kerr = krb5int_get_plugin_data(&dirhandle->files[i], symname, &sym);
	if (kerr)
	    continue;
	newp = realloc (p, (count+1) * sizeof(*p));
	if (newp == NULL) {
	realloc_failure:
	    err = errno;
	    free(p);
	    return err;
	}
	p = newp;
	p[count] = sym;
	count++;
    }
skip_loop:
    newp = realloc(p, (count+1) * sizeof(*p));
    if (newp == NULL)
	goto realloc_failure;
    p = newp;
    p[count] = NULL;
    *ptrs = p;
    return 0;
}

void KRB5_CALLCONV
krb5int_free_plugin_dir_func (void (**ptrs)(void))
{
    /* Nothing special to be done per pointer.  */
    free(ptrs);
}

int32_t KRB5_CALLCONV
krb5int_get_plugin_dir_func (struct plugin_dir_handle *dirhandle,
			     const char *symname,
			     void (***ptrs)(void))
{
    void (**p)(), (**newp)(), (*sym)();
    int count, i, err;

    if (dirhandle == NULL) {
	*ptrs = 0;
	return 0;
    }

    /* XXX Do we need to add a leading "_" to the symbol name on any
       modern platforms?  */

    p = 0;
    count = 0;
    for (i = 0; !NULL_HANDLE (&dirhandle->files[i]); i++) {
	int32_t kerr;
	kerr = krb5int_get_plugin_func(&dirhandle->files[i], symname, &sym);
	if (kerr)
	    continue;
	newp = realloc (p, (count+1) * sizeof(*p));
	if (newp == NULL) {
	realloc_failure:
	    err = errno;
	    free(p);
	    return err;
	}
	p = newp;
	p[count] = sym;
	count++;
    }
    newp = realloc(p, (count+1) * sizeof(*p));
    if (newp == NULL)
	goto realloc_failure;
    p[count] = NULL;
    *ptrs = p;
    return 0;
}