aboutsummaryrefslogtreecommitdiffstats
path: root/epan/plugins.c
blob: cc84b475ec8d894caef3850409c6a55c882094a2 (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* plugins.c
 * plugin routines
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1999 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "plugins.h"

#ifdef HAVE_PLUGINS

#include <time.h>

#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif

#ifdef HAVE_DIRECT_H
#include <direct.h>
#endif

#include <string.h>
#include <stdlib.h>
#include <errno.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "filesystem.h"
#include "privileges.h"
#include <wsutil/file_util.h>
#include "report_err.h"

/* linked list of all plugins */
plugin *plugin_list;

#define PLUGINS_DIR_NAME	"plugins"

/*
 * add a new plugin to the list
 * returns :
 * - 0 : OK
 * - ENOMEM : memory allocation problem
 * - EEXIST : the same plugin (i.e. name/version) was already registered.
 */
static int
add_plugin(void *handle, gchar *name, gchar *version,
	   void (*register_protoinfo)(void), void (*reg_handoff)(void),
	   void (*register_tap_listener)(void),
	   void (*register_wtap_module)(void),
	   void (*register_codec_module)(void))
{
    plugin *new_plug, *pt_plug;

    pt_plug = plugin_list;
    if (!pt_plug) /* the list is empty */
    {
	new_plug = (plugin *)g_malloc(sizeof(plugin));
	if (new_plug == NULL) return ENOMEM;
        plugin_list = new_plug;
    }
    else
    {
	while (1)
	{
	    /* check if the same name/version is already registered */
	    if (!strcmp(pt_plug->name, name) &&
		!strcmp(pt_plug->version, version))
	    {
		return EEXIST;
	    }

	    /* we found the last plugin in the list */
	    if (pt_plug->next == NULL) break;

	    pt_plug = pt_plug->next;
	}
	new_plug = (plugin *)g_malloc(sizeof(plugin));
	if (new_plug == NULL) return ENOMEM;
	pt_plug->next = new_plug;
    }

    new_plug->handle = handle;
    new_plug->name = name;
    new_plug->version = version;
    new_plug->register_protoinfo = register_protoinfo;
    new_plug->reg_handoff = reg_handoff;
    new_plug->register_tap_listener = register_tap_listener;
    new_plug->register_wtap_module = register_wtap_module;
    new_plug->register_codec_module = register_codec_module;
    new_plug->next = NULL;

    return 0;
}

/*
 * XXX - when we remove support for old-style plugins (which we should
 * probably do eventually, as all plugins should be written as new-style
 * ones), we may want to have "init_plugins()" merely save a pointer
 * to the plugin's "init" routine, just as we save a pointer to its
 * "reg_handoff" routine, and have a "register_all_plugins()" routine
 * to go through the list of plugins and call all of them.
 *
 * Then we'd have "epan_init()", or perhaps even something higher up
 * in the call tree, call "init_plugins()", and have "proto_init()"
 * call "register_all_plugins()" right after calling "register_all_protocols()";
 * this might be a bit cleaner.
 */
static void
plugins_scan_dir(const char *dirname)
{
#define FILENAME_LEN	1024
    ETH_DIR       *dir;             /* scanned directory */
    ETH_DIRENT    *file;            /* current file */
    const char    *name;
    gchar          filename[FILENAME_LEN];   /* current file name */
    GModule       *handle;          /* handle returned by dlopen */
    gchar         *version;
    gpointer       gp;
    void         (*register_protoinfo)(void);
    void         (*reg_handoff)(void);
    void         (*register_tap_listener)(void);
    void (*register_wtap_module)(void);
    void (*register_codec_module)(void);

    gchar         *dot;
    int            cr;

    if ((dir = ws_dir_open(dirname, 0, NULL)) != NULL)
    {

    while ((file = ws_dir_read_name(dir)) != NULL)
	{
	    name = ws_dir_get_name(file);

	    /*
	     * GLib 2.x defines G_MODULE_SUFFIX as the extension used on
	     * this platform for loadable modules.
	     */
	    /* skip anything but files with G_MODULE_SUFFIX */
            dot = strrchr(name, '.');
            if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0)
	        continue;

	    g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
	        dirname, name);
	    if ((handle = g_module_open(filename, 0)) == NULL)
	    {
		report_failure("Couldn't load module %s: %s", filename,
			  g_module_error());
		continue;
	    }

	    if (!g_module_symbol(handle, "version", &gp))
	    {
	        report_failure("The plugin %s has no version symbol", name);
		g_module_close(handle);
		continue;
	    }
	    version = gp;

	    /*
	     * Do we have a register routine?
	     */
	    if (g_module_symbol(handle, "plugin_register", &gp))
	    {
		/*
		 * Yes - this plugin includes one or more dissectors.
		 */
		register_protoinfo = gp;
	    }
	    else
	    {
		/*
		 * No - no dissectors.
		 */
		register_protoinfo = NULL;
	    }

	    /*
	     * Do we have a reg_handoff routine?
	     */
	    if (g_module_symbol(handle, "plugin_reg_handoff", &gp))
	    {
		/*
		 * Yes.
		 */
		reg_handoff = gp;
	    }
	    else
	    {
		/*
		 * No - that's OK even if we have dissectors, as long
		 * as the plugin registers by name *and* there's
		 * a caller looking for that name.
		 */
		reg_handoff = NULL;
	    }

	    /*
	     * Do we have a register_tap_listener routine?
	     */
	    if (g_module_symbol(handle, "plugin_register_tap_listener", &gp))
	    {
		/*
		 * Yes - this plugin includes one or more taps.
		 */
		register_tap_listener = gp;
	    }
	    else
	    {
		/*
		 * No - no taps here.
		 */
		register_tap_listener = NULL;
	    }

		/*
	     * Do we have an old-style init routine?
	     */
	    if (g_module_symbol(handle, "plugin_init", &gp))
	    {
			/*
			 * Yes - do we also have a register routine or a
			 * register_tap_listener routine?  If so, this is a bogus
			 * hybrid of an old-style and new-style plugin.
			 */
			if (register_protoinfo != NULL || register_tap_listener != NULL)
			{
				report_failure("The plugin '%s' has an old plugin init routine\nand a new register or register_tap_listener routine.",
				name);
				g_module_close(handle);
				continue;
			}

			/*
			 * It's just an unsupported old-style plugin;
			 */
			report_failure("The plugin '%s' has an old plugin init routine. Support has been dropped.\n Information on how to update your plugin is available at \nhttp://anonsvn.wireshark.org/wireshark/trunk/doc/README.plugins",
				name);
			g_module_close(handle);
			continue;
	    }

	    /*
		 * Do we have a register_wtap_module routine?
		 */
           if (g_module_symbol(handle, "register_wtap_module", &gp))
           {
               register_wtap_module = gp;
           } else {
               register_wtap_module = NULL;
           }

	    /*
		 * Do we have a register_codec_module routine?
		 */
           if (g_module_symbol(handle, "register_codec_module", &gp))
           {
               register_codec_module = gp;
           } else {
               register_codec_module = NULL;
           }

	   /*
		* Does this dissector do anything useful?
		*/
	    if (register_protoinfo == NULL &&
		    register_tap_listener == NULL &&
		    register_wtap_module == NULL &&
		    register_codec_module == NULL )
	    {
		/*
		 * No.
		 */
		report_failure("The plugin '%s' has neither a register routine, "
					   "a register_tap_listener or a register_wtap_module or a register_codec_module routine",
		    name);
		g_module_close(handle);
		continue;
	    }

	    /*
	     * OK, attempt to add it to the list of plugins.
	     */
	    if ((cr = add_plugin(handle, g_strdup(name), version,
				 register_protoinfo, reg_handoff,
				 register_tap_listener,register_wtap_module,register_codec_module)))
	    {
		if (cr == EEXIST)
		    fprintf(stderr, "The plugin %s, version %s\n"
			    "was found in multiple directories\n", name, version);
		else
		    fprintf(stderr, "Memory allocation problem\n"
			    "when processing plugin %s, version %s\n",
			    name, version);
		g_module_close(handle);
		continue;
	    }

	}
	ws_dir_close(dir);
	}
}

/* get the personal plugin dir */
/* Return value is malloced so the caller should g_free() it. */
char *get_plugins_pers_dir(void)
{
    return get_persconffile_path(PLUGINS_DIR_NAME, FALSE, FALSE);
}

/*
 * init plugins
 */
void
init_plugins(void)
{
    const char *plugin_dir;
    const char *name;
    char *plugin_dir_path;
    char *plugins_pers_dir;
    ETH_DIR *dir;		/* scanned directory */
    ETH_DIRENT *file;		/* current file */

    if (plugin_list == NULL)      /* ensure init_plugins is only run once */
    {
	/*
	 * Scan the global plugin directory.
	 * If we're running from a build directory, scan the subdirectories
	 * of that directory, as the global plugin directory is the
	 * "plugins" directory of the source tree, and the subdirectories
	 * are the source directories for the plugins, with the plugins
	 * built in those subdirectories.
	 */
	plugin_dir = get_plugin_dir();
	if (running_in_build_directory()) {
	    if ((dir = ws_dir_open(plugin_dir, 0, NULL)) != NULL) {
		while ((file = ws_dir_read_name(dir)) != NULL)	{
		    name = ws_dir_get_name(file);
		    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
			continue;	/* skip "." and ".." */
		    /*
		     * Get the full path of a ".libs" subdirectory of that
		     * directory.
		     */
		    plugin_dir_path = g_strdup_printf(
			"%s" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S ".libs",
			plugin_dir, name);
		    if (test_for_directory(plugin_dir_path) != EISDIR) {
			/*
			 * Either it doesn't refer to a directory or it
			 * refers to something that doesn't exist.
			 *
			 * Assume that means that the plugins are in
			 * the subdirectory of the plugin directory, not
			 * a ".libs" subdirectory of that subdirectory.
			 */
			g_free(plugin_dir_path);
			plugin_dir_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
			    plugin_dir, name);
		    }
		    plugins_scan_dir(plugin_dir_path);
		    g_free(plugin_dir_path);
		}
	    }
	} else
	    plugins_scan_dir(plugin_dir);

	/*
	 * If the program wasn't started with special privileges,
	 * scan the users plugin directory.  (Even if we relinquish
	 * them, plugins aren't safe unless we've *permanently*
	 * relinquished them, and we can't do that in Wireshark as,
	 * if we need privileges to start capturing, we'd need to
	 * reclaim them before each time we start capturing.)
	 */
	if (!started_with_special_privs()) {
	    plugins_pers_dir = get_plugins_pers_dir();
	    plugins_scan_dir(plugins_pers_dir);
	    g_free(plugins_pers_dir);
	}
    }
	register_all_wiretap_modules();
	register_all_codecs();
}

void
register_all_plugin_registrations(void)
{
    plugin *pt_plug;

    /*
     * For all plugins with register-handoff routines, call the routines.
     * This is called from "proto_init()"; it must be called after
     * "register_all_protocols()" and "init_plugins()" are called,
     * in case one plugin registers itself either with a built-in
     * dissector or with another plugin; we must first register all
     * dissectors, whether built-in or plugin, so their dissector tables
     * are initialized, and only then register all handoffs.
     */
    for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
    {
		if (pt_plug->register_protoinfo)
			(pt_plug->register_protoinfo)();
    }
}

void
register_all_plugin_handoffs(void)
{
    plugin *pt_plug;

    /*
     * For all plugins with register-handoff routines, call the routines.
     * This is called from "proto_init()"; it must be called after
     * "register_all_protocols()" and "init_plugins()" are called,
     * in case one plugin registers itself either with a built-in
     * dissector or with another plugin; we must first register all
     * dissectors, whether built-in or plugin, so their dissector tables
     * are initialized, and only then register all handoffs.
     */
    for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
    {
	if (pt_plug->reg_handoff)
	    (pt_plug->reg_handoff)();
    }
}

void
register_all_plugin_tap_listeners(void)
{
    plugin *pt_plug;

    /*
     * For all plugins with register-tap-listener routines, call the
     * routines.
     */
    for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
    {
	if (pt_plug->register_tap_listener)
	    (pt_plug->register_tap_listener)();
    }
}

void
register_all_wiretap_modules(void)
{
    plugin *pt_plug;

    /*
     * For all plugins with register_wtap_module routines, call the
     * routines.
     */
    for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
    {
		if (pt_plug->register_wtap_module)
			(pt_plug->register_wtap_module)();
    }
}

void
register_all_codecs(void)
{
    plugin *pt_plug;

    /*
     * For all plugins with register_wtap_module routines, call the
     * routines.
     */
    for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
    {
		if (pt_plug->register_codec_module)
			(pt_plug->register_codec_module)();
    }
}
#endif