aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil/plugins.c
blob: 8c89f4657c836ce8436a2d34b818c9ee1a11f566 (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
/* plugins.c
 * plugin routines
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#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>
#include <wsutil/report_message.h>

#include <wsutil/plugins.h>
#include <wsutil/ws_printf.h> /* ws_debug_printf */

typedef struct _plugin {
    GModule        *handle;       /* handle returned by g_module_open */
    gchar          *name;         /* plugin name */
    const gchar    *version;      /* plugin version */
    const gchar    *type_name;    /* user-facing name (what it does). Should these be capitalized? */
} plugin;

#define TYPE_DIR_EPAN       "epan"
#define TYPE_DIR_WIRETAP    "wiretap"
#define TYPE_DIR_CODECS     "codecs"

#define TYPE_NAME_DISSECTOR "dissector"
#define TYPE_NAME_FILE_TYPE "file type"
#define TYPE_NAME_CODEC     "codec"

/* array of plugins */
static GPtrArray *plugins_array = NULL;
/* map of names to plugin */
static GHashTable *plugins_table = NULL;


static inline const char *
type_to_dir(plugin_type_e type)
{
    switch (type) {
    case WS_PLUGIN_EPAN:
        return TYPE_DIR_EPAN;
    case WS_PLUGIN_WIRETAP:
        return TYPE_DIR_WIRETAP;
    case WS_PLUGIN_CODEC:
        return TYPE_DIR_CODECS;
    default:
        g_error("Unknown plugin type: %u. Aborting.", (unsigned) type);
        break;
    }
    g_assert_not_reached();
}

static inline const char *
type_to_name(plugin_type_e type)
{
    switch (type) {
    case WS_PLUGIN_EPAN:
        return TYPE_NAME_DISSECTOR;
    case WS_PLUGIN_WIRETAP:
        return TYPE_NAME_FILE_TYPE;
    case WS_PLUGIN_CODEC:
        return TYPE_NAME_CODEC;
    default:
        g_error("Unknown plugin type: %u. Aborting.", (unsigned) type);
        break;
    }
    g_assert_not_reached();
}

static void
free_plugin(gpointer data)
{
    plugin *p = (plugin *)data;
    g_module_close(p->handle);
    g_free(p->name);
    g_free(p);
}

static gint
compare_plugins(gconstpointer a, gconstpointer b)
{
    return g_strcmp0((*(const plugin **)a)->name, (*(const plugin **)b)->name);
}

static void
plugins_scan_dir(GPtrArray **plugins_ptr, const char *dirpath, plugin_type_e type, gboolean append_type)
{
    GDir          *dir;
    const char    *name;            /* current file name */
    gchar         *plugin_folder;
    gchar         *plugin_file;     /* current file full path */
    GModule       *handle;          /* handle returned by g_module_open */
    gpointer       symbol;
    const char    *plug_version, *plug_release;
    plugin        *new_plug;
    gchar         *dot;

    if (append_type)
        plugin_folder = g_build_filename(dirpath, type_to_dir(type), (gchar *)NULL);
    else
        plugin_folder = g_strdup(dirpath);

    dir = g_dir_open(plugin_folder, 0, NULL);
    if (dir == NULL) {
        g_free(plugin_folder);
        return;
    }

    while ((name = g_dir_read_name(dir)) != NULL) {
        /* Skip anything but files with G_MODULE_SUFFIX. */
        dot = strrchr(name, '.');
        if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0)
            continue;

#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

        /*
         * Check if the same name is already registered.
         */
        if (g_hash_table_lookup(plugins_table, name)) {
            /* Yes, it is. */
            report_warning("The plugin '%s' was found "
                                "in multiple directories", name);
            continue;
        }

        plugin_file = g_build_filename(plugin_folder, name, (gchar *)NULL);
        handle = g_module_open(plugin_file, G_MODULE_BIND_LOCAL);
        g_free(plugin_file);
        if (handle == NULL) {
            /* g_module_error() provides file path. */
            report_failure("Couldn't load plugin '%s': %s", name,
                            g_module_error());
            continue;
        }

        if (!g_module_symbol(handle, "plugin_version", &symbol))
        {
            report_failure("The plugin '%s' has no \"plugin_version\" symbol", name);
            g_module_close(handle);
            continue;
        }
        plug_version = (const char *)symbol;

        if (!g_module_symbol(handle, "plugin_release", &symbol))
        {
            report_failure("The plugin '%s' has no \"plugin_release\" symbol", name);
            g_module_close(handle);
            continue;
        }
        plug_release = (const char *)symbol;
        if (strcmp(plug_release, VERSION_RELEASE) != 0) {
            report_failure("The plugin '%s' was compiled for Wireshark version %s", name, plug_release);
            g_module_close(handle);
            continue;
        }

        /* Search for the entry point for the plugin registration function */
        if (!g_module_symbol(handle, "plugin_register", &symbol)) {
            report_failure("The plugin '%s' has no \"plugin_register\" symbol", name);
            g_module_close(handle);
            continue;
        }

DIAG_OFF(pedantic)
        /* Found it, call the plugin registration function. */
        ((plugin_register_func)symbol)();
DIAG_ON(pedantic)

        new_plug = (plugin *)g_malloc(sizeof(plugin));
        new_plug->handle = handle;
        new_plug->name = g_strdup(name);
        new_plug->version = plug_version;
        new_plug->type_name = type_to_name(type);

        /* Add it to the list of plugins. */
        if (*plugins_ptr == NULL)
            *plugins_ptr = g_ptr_array_new_with_free_func(free_plugin);
        g_ptr_array_add(*plugins_ptr, new_plug);
        g_ptr_array_add(plugins_array, new_plug);
        g_hash_table_insert(plugins_table, new_plug->name, new_plug);
    }
    ws_dir_close(dir);
    g_free(plugin_folder);
}

/*
 * Scan the buildir for plugins.
 */
static void
scan_plugins_build_dir(GPtrArray **plugins_ptr, plugin_type_e type)
{
    const char *name;
    char *dirpath;
    char *plugin_folder;
    WS_DIR *dir;                /* scanned directory */
    WS_DIRENT *file;            /* current file */

    /* Cmake */
    plugins_scan_dir(plugins_ptr, get_plugins_dir_with_version(), type, TRUE);

    /* Autotools */
    dirpath = g_build_filename(get_plugins_dir(), type_to_dir(type), (char *)NULL);
    dir = ws_dir_open(dirpath, 0, NULL);
    if (dir == NULL) {
        g_free(dirpath);
        return;
    }

    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_folder = g_build_filename(dirpath, name, ".libs", (gchar *)NULL);
        if (test_for_directory(plugin_folder) != 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_folder);
            plugin_folder = g_build_filename(get_plugins_dir(), name, (gchar *)NULL);
        }
        plugins_scan_dir(plugins_ptr, plugin_folder, type, FALSE);
        g_free(plugin_folder);
    }
    ws_dir_close(dir);
    g_free(dirpath);
}

/*
 * Scan for plugins.
 */
plugins_t *
plugins_init(plugin_type_e type)
{
    if (!g_module_supported())
        return NULL; /* nothing to do */

    GPtrArray *plugins = NULL;

    if (plugins_table == NULL)
        plugins_table = g_hash_table_new(g_str_hash, g_str_equal);
    if (plugins_array == NULL)
        plugins_array = g_ptr_array_new();

    /*
     * 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(&plugins, type);
    }
    else
    {
        plugins_scan_dir(&plugins, get_plugins_dir_with_version(), type, TRUE);
    }

    /*
     * 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(&plugins, get_plugins_pers_dir_with_version(), type, TRUE);
    }

    g_ptr_array_sort(plugins_array, compare_plugins);

    return plugins;
}

WS_DLL_PUBLIC void
plugins_get_descriptions(plugin_description_callback callback, void *callback_data)
{
    if (!plugins_array)
        return;

    for (guint i = 0; i < plugins_array->len; i++) {
        plugin *plug = (plugin *)plugins_array->pdata[i];
        callback(plug->name, plug->version, plug->type_name, g_module_name(plug->handle), callback_data);
    }
}

static void
print_plugin_description(const char *name, const char *version,
                         const char *description, const char *filename,
                         void *user_data _U_)
{
    ws_debug_printf("%s\t%s\t%s\t%s\n", name, version, description, filename);
}

void
plugins_dump_all(void)
{
    plugins_get_descriptions(print_plugin_description, NULL);
}

int
plugins_get_count(void)
{
    if (plugins_table)
        return g_hash_table_size(plugins_table);
    return 0;
}

void
plugins_cleanup(plugins_t *plugins)
{
    if (plugins)
        g_ptr_array_free((GPtrArray *)plugins, TRUE);

    /*
     * This module uses global bookkeeping data structures and per-library
     * objects sharing data. To avoid having to walk the plugins GPtrArray
     * and delete each plugin from the global data structures we purge them
     * once the first plugin cleanup function is called. This means that after
     * calling ONE OF POSSIBLY MANY plugin cleanup function NO OTHER plugin
     * APIs can be used except plugins_cleanup. If it ever becomes an issue
     * it will be easy to change, for a small performance penalty.
     */
    if (plugins_table) {
        g_hash_table_destroy(plugins_table);
        plugins_table = NULL;
    }
    if (plugins_array) {
        g_ptr_array_free(plugins_array, FALSE);
        plugins_array = NULL;
    }
}

#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:
 */