aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil/plugins.c
blob: 5a6d735df0165efc482f95a6fa7f1274f1909f04 (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
/* 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>
#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 */
    GString        *types;        /* description with types this plugin supports */
} plugin;

static GHashTable *plugins_table = NULL;

static void
free_plugin(gpointer _p)
{
    plugin *p = (plugin *)_p;
    g_module_close(p->handle);
    g_free(p->name);
    g_string_free(p->types, TRUE);
    g_free(p);
}

/*
 * 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 {
    const char *name;
    plugin_check_type_callback check_type;
} plugin_type;

static GSList *plugin_types = NULL;

static void
free_plugin_type(gpointer p, gpointer user_data _U_)
{
    g_free(p);
}

void
add_plugin_type(const char *name, plugin_check_type_callback check_type)
{
    plugin_type *new_type;

    new_type = (plugin_type *)g_malloc(sizeof (plugin_type));
    new_type->name = name;
    new_type->check_type = check_type;
    plugin_types = g_slist_prepend(plugin_types, new_type);
}

static void
call_plugin_callback(gpointer data, gpointer user_data)
{
    plugin_type *type = (plugin_type *)data;
    plugin *new_plug = (plugin *)user_data;

    if (type->check_type(new_plug->handle)) {
        /* The plugin supports this type */
        if (new_plug->types->len > 0)
            g_string_append(new_plug->types, ", ");
        g_string_append(new_plug->types, type->name);
    }
}

static void
plugins_scan_dir(const char *dirname, plugin_load_failure_mode mode)
{
#define FILENAME_LEN        1024
    WS_DIR        *dir;             /* scanned directory */
    WS_DIRENT     *file;            /* current file */
    const char    *name;
    gchar          filename[FILENAME_LEN];   /* current file name */
    GModule       *handle;          /* handle returned by g_module_open */
    gpointer       gp;
    plugin        *new_plug;
    gchar         *dot;

    if (!g_file_test(dirname, G_FILE_TEST_EXISTS) || !g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
        return;
    }

    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;
#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
            g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
                       dirname, name);

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

            if ((handle = g_module_open(filename, G_MODULE_BIND_LOCAL)) == NULL)
            {
                /*
                 * 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) {
                    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;
            }

            new_plug = (plugin *)g_malloc(sizeof(plugin));
            new_plug->handle = handle;
            new_plug->name = g_strdup(name);
            new_plug->version = (char *)gp;
            new_plug->types = g_string_new(NULL);

            /*
             * 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?
             */
            if (new_plug->types->len == 0)
            {
                /*
                 * No.
                 *
                 * 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.
                 */
                if (mode == REPORT_LOAD_FAILURE) {
                    report_failure("The plugin '%s' has no registration routines",
                                   name);
                }
                free_plugin(new_plug);
                continue;
            }

            /*
             * OK, add it to the list of plugins.
             */
            g_hash_table_insert(plugins_table, new_plug->name, new_plug);
        }
        ws_dir_close(dir);
    }
}


/*
 * Scan for plugins.
 */
void
scan_plugins(plugin_load_failure_mode mode)
{
    const char *plugin_dir;
    const char *name;
    char *plugin_dir_path;
    WS_DIR *dir;                /* scanned directory */
    WS_DIRENT *file;            /* current file */

    if (plugins_table == NULL)    /* 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.
         */
        plugin_dir = get_plugins_dir();
        if (plugin_dir == NULL)
        {
            /* We couldn't find the plugin directory. */
            return;
        }
        if (running_in_build_directory())
        {
            if ((dir = ws_dir_open(plugin_dir, 0, NULL)) != NULL)
            {
                plugins_scan_dir(plugin_dir, mode);
                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, mode);
                    g_free(plugin_dir_path);
                }
                ws_dir_close(dir);
            }
        }
        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);
        }
    }
}

static void
add_plugin_to_descriptions(gpointer key _U_, gpointer value, gpointer user_data)
{
    g_ptr_array_add((GPtrArray *)user_data, value);
}

static int
compare_plugins(gconstpointer _a, gconstpointer _b)
{
    const plugin *a = *(const plugin **)_a;
    const plugin *b = *(const plugin **)_b;

    return strcmp(a->name, b->name);
}

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);
}

WS_DLL_PUBLIC void
plugins_get_descriptions(plugin_description_callback callback, void *user_data)
{
    GPtrArray *descriptions;
    struct description_callback cb;

    descriptions = g_ptr_array_sized_new(g_hash_table_size(plugins_table));
    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);
}

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);
}

void
plugins_cleanup(void)
{
    g_hash_table_destroy(plugins_table);
    g_slist_foreach(plugin_types, free_plugin_type, NULL);
    g_slist_free(plugin_types);
}

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