2013-12-02 08:30:29 +00:00
|
|
|
/* plugins.c
|
|
|
|
* plugin routines
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_PLUGINS
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <gmodule.h>
|
|
|
|
|
|
|
|
#include <wsutil/filesystem.h>
|
|
|
|
#include <wsutil/privileges.h>
|
|
|
|
#include <wsutil/file_util.h>
|
2017-04-08 19:45:19 +00:00
|
|
|
#include <wsutil/report_message.h>
|
2013-12-02 08:30:29 +00:00
|
|
|
|
|
|
|
#include <wsutil/plugins.h>
|
2016-07-24 12:53:39 +00:00
|
|
|
#include <wsutil/ws_printf.h> /* ws_debug_printf */
|
2013-12-02 08:30:29 +00:00
|
|
|
|
|
|
|
typedef struct _plugin {
|
|
|
|
GModule *handle; /* handle returned by g_module_open */
|
|
|
|
gchar *name; /* plugin name */
|
2017-09-25 18:05:40 +00:00
|
|
|
const gchar *version; /* plugin version */
|
2017-09-25 23:15:24 +00:00
|
|
|
GString *types; /* description with types this plugin supports */
|
2013-12-02 08:30:29 +00:00
|
|
|
} plugin;
|
|
|
|
|
2017-09-25 18:05:40 +00:00
|
|
|
static GHashTable *plugins_table = NULL;
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_plugin(gpointer _p)
|
|
|
|
{
|
|
|
|
plugin *p = (plugin *)_p;
|
|
|
|
g_module_close(p->handle);
|
|
|
|
g_free(p->name);
|
2017-09-25 23:15:24 +00:00
|
|
|
g_string_free(p->types, TRUE);
|
2017-09-25 18:05:40 +00:00
|
|
|
g_free(p);
|
|
|
|
}
|
2013-12-02 08:30:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new plugin type.
|
|
|
|
* Takes a callback routine as an argument; it is called for each plugin
|
|
|
|
* we find, and handed a handle for the plugin, the name of the plugin,
|
|
|
|
* and the version string for the plugin. The plugin returns TRUE if
|
|
|
|
* it's a plugin for that type and FALSE if not.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2017-09-25 23:15:24 +00:00
|
|
|
const char *name;
|
|
|
|
plugin_check_type_callback check_type;
|
2013-12-02 08:30:29 +00:00
|
|
|
} plugin_type;
|
|
|
|
|
|
|
|
static GSList *plugin_types = NULL;
|
|
|
|
|
2017-09-25 18:05:40 +00:00
|
|
|
static void
|
|
|
|
free_plugin_type(gpointer p, gpointer user_data _U_)
|
|
|
|
{
|
|
|
|
g_free(p);
|
|
|
|
}
|
|
|
|
|
2013-12-02 08:30:29 +00:00
|
|
|
void
|
2017-09-25 23:15:24 +00:00
|
|
|
add_plugin_type(const char *name, plugin_check_type_callback check_type)
|
2013-12-02 08:30:29 +00:00
|
|
|
{
|
|
|
|
plugin_type *new_type;
|
|
|
|
|
|
|
|
new_type = (plugin_type *)g_malloc(sizeof (plugin_type));
|
2017-09-25 23:15:24 +00:00
|
|
|
new_type->name = name;
|
|
|
|
new_type->check_type = check_type;
|
|
|
|
plugin_types = g_slist_prepend(plugin_types, new_type);
|
2013-12-02 08:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
call_plugin_callback(gpointer data, gpointer user_data)
|
|
|
|
{
|
|
|
|
plugin_type *type = (plugin_type *)data;
|
|
|
|
plugin *new_plug = (plugin *)user_data;
|
|
|
|
|
2017-09-25 23:15:24 +00:00
|
|
|
if (type->check_type(new_plug->handle)) {
|
2013-12-02 08:30:29 +00:00
|
|
|
/* The plugin supports this type */
|
2017-09-25 23:15:24 +00:00
|
|
|
if (new_plug->types->len > 0)
|
|
|
|
g_string_append(new_plug->types, ", ");
|
|
|
|
g_string_append(new_plug->types, type->name);
|
2013-12-02 08:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-09-05 23:26:29 +00:00
|
|
|
plugins_scan_dir(const char *dirname, plugin_load_failure_mode mode)
|
2013-12-02 08:30:29 +00:00
|
|
|
{
|
|
|
|
WS_DIR *dir; /* scanned directory */
|
|
|
|
WS_DIRENT *file; /* current file */
|
|
|
|
const char *name;
|
2017-10-01 09:47:35 +00:00
|
|
|
gchar *filename; /* current file name */
|
2013-12-02 08:30:29 +00:00
|
|
|
GModule *handle; /* handle returned by g_module_open */
|
2017-10-01 09:47:35 +00:00
|
|
|
gpointer symbol;
|
2013-12-02 08:30:29 +00:00
|
|
|
plugin *new_plug;
|
|
|
|
gchar *dot;
|
|
|
|
|
2017-02-08 13:25:57 +00:00
|
|
|
if (!g_file_test(dirname, G_FILE_TEST_EXISTS) || !g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-02 08:30:29 +00:00
|
|
|
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;
|
2017-10-01 09:47:35 +00:00
|
|
|
|
2016-12-13 18:28:29 +00:00
|
|
|
#if WIN32
|
|
|
|
if (strncmp(name, "nordic_ble.dll", 14) == 0)
|
|
|
|
/*
|
|
|
|
* Skip the Nordic BLE Sniffer dll on WIN32 because
|
|
|
|
* the dissector has been added as internal.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
#endif
|
2017-02-08 13:25:57 +00:00
|
|
|
|
2017-09-25 11:51:27 +00:00
|
|
|
/*
|
|
|
|
* Check if the same name is already registered.
|
|
|
|
*/
|
2017-09-25 18:05:40 +00:00
|
|
|
if (g_hash_table_lookup(plugins_table, name)) {
|
2017-09-25 11:51:27 +00:00
|
|
|
/* Yes, it is. */
|
|
|
|
if (mode == REPORT_LOAD_FAILURE) {
|
|
|
|
report_warning("The plugin '%s' was found "
|
2017-10-01 09:47:35 +00:00
|
|
|
"in multiple directories", name);
|
2017-09-25 11:51:27 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-01 09:47:35 +00:00
|
|
|
filename = g_build_filename(dirname, name, (gchar *)NULL);
|
|
|
|
handle = g_module_open(filename, G_MODULE_BIND_LOCAL);
|
|
|
|
g_free(filename);
|
|
|
|
if (handle == NULL)
|
2013-12-02 08:30:29 +00:00
|
|
|
{
|
2016-09-05 23:26:29 +00:00
|
|
|
/*
|
|
|
|
* Only report load failures if we were asked to.
|
|
|
|
*
|
|
|
|
* XXX - we really should put different types of plugins
|
|
|
|
* (libwiretap, libwireshark) in different subdirectories,
|
|
|
|
* give libwiretap and libwireshark init routines that
|
|
|
|
* load the plugins, and have them scan the appropriate
|
|
|
|
* subdirectories so tha we don't even *try* to, for
|
|
|
|
* example, load libwireshark plugins in programs that
|
|
|
|
* only use libwiretap.
|
|
|
|
*/
|
|
|
|
if (mode == REPORT_LOAD_FAILURE) {
|
2017-10-01 09:47:35 +00:00
|
|
|
/* g_module_error() provides filename. */
|
|
|
|
report_failure("Couldn't load plugin '%s': %s", name,
|
2016-09-05 23:26:29 +00:00
|
|
|
g_module_error());
|
|
|
|
}
|
2013-12-02 08:30:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-01 09:47:35 +00:00
|
|
|
if (!g_module_symbol(handle, "version", &symbol))
|
2013-12-02 08:30:29 +00:00
|
|
|
{
|
2017-10-01 09:47:35 +00:00
|
|
|
report_failure("The plugin '%s' has no \"version\" symbol", name);
|
2013-12-02 08:30:29 +00:00
|
|
|
g_module_close(handle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_plug = (plugin *)g_malloc(sizeof(plugin));
|
|
|
|
new_plug->handle = handle;
|
|
|
|
new_plug->name = g_strdup(name);
|
2017-10-01 09:47:35 +00:00
|
|
|
new_plug->version = (char *)symbol;
|
2017-09-25 23:15:24 +00:00
|
|
|
new_plug->types = g_string_new(NULL);
|
2013-12-02 08:30:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Hand the plugin to each of the plugin type callbacks.
|
|
|
|
*/
|
|
|
|
g_slist_foreach(plugin_types, call_plugin_callback, new_plug);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Does this dissector do anything useful?
|
|
|
|
*/
|
2017-09-25 23:15:24 +00:00
|
|
|
if (new_plug->types->len == 0)
|
2013-12-02 08:30:29 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* No.
|
2016-09-06 17:14:12 +00:00
|
|
|
*
|
|
|
|
* Only report this failure if we were asked to; it might
|
|
|
|
* just mean that it's a plugin type that this program
|
|
|
|
* doesn't support, such as a libwireshark plugin in
|
|
|
|
* a program that doesn't use libwireshark.
|
|
|
|
*
|
|
|
|
* XXX - we really should put different types of plugins
|
|
|
|
* (libwiretap, libwireshark) in different subdirectories,
|
|
|
|
* give libwiretap and libwireshark init routines that
|
|
|
|
* load the plugins, and have them scan the appropriate
|
|
|
|
* subdirectories so tha we don't even *try* to, for
|
|
|
|
* example, load libwireshark plugins in programs that
|
|
|
|
* only use libwiretap.
|
2013-12-02 08:30:29 +00:00
|
|
|
*/
|
2016-09-06 17:14:12 +00:00
|
|
|
if (mode == REPORT_LOAD_FAILURE) {
|
|
|
|
report_failure("The plugin '%s' has no registration routines",
|
|
|
|
name);
|
|
|
|
}
|
2017-09-25 23:15:24 +00:00
|
|
|
free_plugin(new_plug);
|
2013-12-02 08:30:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-09-25 11:51:27 +00:00
|
|
|
* OK, add it to the list of plugins.
|
2013-12-02 08:30:29 +00:00
|
|
|
*/
|
2017-09-25 18:05:40 +00:00
|
|
|
g_hash_table_insert(plugins_table, new_plug->name, new_plug);
|
2013-12-02 08:30:29 +00:00
|
|
|
}
|
|
|
|
ws_dir_close(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-09-26 19:43:39 +00:00
|
|
|
* Scan the buildir for plugins.
|
2013-12-02 08:30:29 +00:00
|
|
|
*/
|
2017-09-26 19:43:39 +00:00
|
|
|
static void
|
|
|
|
scan_plugins_build_dir(plugin_load_failure_mode mode)
|
2013-12-02 08:30:29 +00:00
|
|
|
{
|
|
|
|
const char *plugin_dir;
|
|
|
|
const char *name;
|
|
|
|
char *plugin_dir_path;
|
|
|
|
WS_DIR *dir; /* scanned directory */
|
2016-12-05 04:25:51 +00:00
|
|
|
WS_DIRENT *file; /* current file */
|
2013-12-02 08:30:29 +00:00
|
|
|
|
2017-09-26 19:43:39 +00:00
|
|
|
plugin_dir = get_plugins_dir();
|
|
|
|
if ((dir = ws_dir_open(plugin_dir, 0, NULL)) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
plugins_scan_dir(plugin_dir, mode);
|
|
|
|
while ((file = ws_dir_read_name(dir)) != NULL)
|
2013-12-02 08:30:29 +00:00
|
|
|
{
|
2017-09-26 19:43:39 +00:00
|
|
|
name = ws_dir_get_name(file);
|
|
|
|
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
|
|
|
|
continue; /* skip "." and ".." */
|
2013-12-02 08:30:29 +00:00
|
|
|
/*
|
2017-09-26 19:43:39 +00:00
|
|
|
* Get the full path of a ".libs" subdirectory of that
|
|
|
|
* directory.
|
2013-12-02 08:30:29 +00:00
|
|
|
*/
|
2017-09-26 19:43:39 +00:00
|
|
|
plugin_dir_path = g_build_filename(plugin_dir, name, ".libs", (gchar *)NULL);
|
|
|
|
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_build_filename(plugin_dir, name, (gchar *)NULL);
|
2017-09-12 09:51:46 +00:00
|
|
|
}
|
2017-09-26 19:43:39 +00:00
|
|
|
plugins_scan_dir(plugin_dir_path, mode);
|
|
|
|
g_free(plugin_dir_path);
|
|
|
|
}
|
|
|
|
ws_dir_close(dir);
|
|
|
|
}
|
2013-12-02 08:30:29 +00:00
|
|
|
|
2017-09-26 19:43:39 +00:00
|
|
|
/*
|
|
|
|
* Scan for plugins.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
scan_plugins(plugin_load_failure_mode mode)
|
|
|
|
{
|
|
|
|
if (plugins_table != NULL)
|
|
|
|
return; /* only scan for plugins once */
|
|
|
|
|
|
|
|
plugins_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_plugin);
|
|
|
|
/*
|
|
|
|
* Scan the global plugin directory.
|
|
|
|
* If we're running from a build directory, scan the "plugins"
|
|
|
|
* subdirectory, as that's where plugins are located in an
|
|
|
|
* out-of-tree build. If we find subdirectories scan those since
|
|
|
|
* they will contain plugins in the case of an in-tree build.
|
|
|
|
*/
|
|
|
|
if (running_in_build_directory())
|
|
|
|
{
|
|
|
|
scan_plugins_build_dir(mode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
plugins_scan_dir(get_plugins_dir_with_version(), mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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_scan_dir(get_plugins_pers_dir_with_version(), mode);
|
2013-12-02 08:30:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-09-26 18:17:52 +00:00
|
|
|
add_plugin_to_descriptions(gpointer key _U_, gpointer value, gpointer user_data)
|
2013-12-02 08:30:29 +00:00
|
|
|
{
|
2017-09-26 18:17:52 +00:00
|
|
|
g_ptr_array_add((GPtrArray *)user_data, value);
|
2017-09-25 19:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-09-26 18:17:52 +00:00
|
|
|
compare_plugins(gconstpointer _a, gconstpointer _b)
|
2017-09-25 19:42:29 +00:00
|
|
|
{
|
2017-09-26 18:17:52 +00:00
|
|
|
const plugin *a = *(const plugin **)_a;
|
|
|
|
const plugin *b = *(const plugin **)_b;
|
2017-09-25 18:05:40 +00:00
|
|
|
|
2017-09-25 19:42:29 +00:00
|
|
|
return strcmp(a->name, b->name);
|
2013-12-02 08:30:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 18:17:52 +00:00
|
|
|
struct description_callback {
|
|
|
|
plugin_description_callback callback;
|
|
|
|
gpointer callback_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
call_description_callback(gpointer data, gpointer user_data)
|
|
|
|
{
|
|
|
|
plugin *plug = (plugin *)data;
|
|
|
|
struct description_callback *desc = (struct description_callback *)user_data;
|
|
|
|
|
|
|
|
desc->callback(plug->name, plug->version, plug->types->str, g_module_name(plug->handle), desc->callback_data);
|
|
|
|
}
|
|
|
|
|
2013-12-02 08:30:29 +00:00
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
plugins_get_descriptions(plugin_description_callback callback, void *user_data)
|
|
|
|
{
|
2017-09-25 19:42:29 +00:00
|
|
|
GPtrArray *descriptions;
|
2017-09-26 18:17:52 +00:00
|
|
|
struct description_callback cb;
|
2017-09-25 19:42:29 +00:00
|
|
|
|
|
|
|
descriptions = g_ptr_array_sized_new(g_hash_table_size(plugins_table));
|
2017-09-26 18:17:52 +00:00
|
|
|
g_hash_table_foreach(plugins_table, add_plugin_to_descriptions, descriptions);
|
|
|
|
g_ptr_array_sort(descriptions, compare_plugins);
|
|
|
|
cb.callback = callback;
|
|
|
|
cb.callback_data = user_data;
|
|
|
|
g_ptr_array_foreach(descriptions, call_description_callback, &cb);
|
|
|
|
g_ptr_array_free(descriptions, FALSE);
|
2013-12-02 08:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_plugin_description(const char *name, const char *version,
|
|
|
|
const char *description, const char *filename,
|
|
|
|
void *user_data _U_)
|
|
|
|
{
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf("%s\t%s\t%s\t%s\n", name, version, description, filename);
|
2013-12-02 08:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
plugins_dump_all(void)
|
|
|
|
{
|
|
|
|
plugins_get_descriptions(print_plugin_description, NULL);
|
|
|
|
}
|
|
|
|
|
2017-02-08 13:25:57 +00:00
|
|
|
void
|
|
|
|
plugins_cleanup(void)
|
|
|
|
{
|
2017-09-25 18:05:40 +00:00
|
|
|
g_hash_table_destroy(plugins_table);
|
2017-02-12 15:52:11 +00:00
|
|
|
g_slist_foreach(plugin_types, free_plugin_type, NULL);
|
|
|
|
g_slist_free(plugin_types);
|
2017-02-08 13:25:57 +00:00
|
|
|
}
|
|
|
|
|
2013-12-02 08:30:29 +00:00
|
|
|
#endif /* HAVE_PLUGINS */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|