name change
[obnox/wireshark/wip.git] / epan / plugins.c
1 /* plugins.c
2  * plugin routines
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1999 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "plugins.h"
30
31 #ifdef HAVE_PLUGINS
32
33 #include <time.h>
34
35 #ifdef HAVE_DIRENT_H
36 #include <dirent.h>
37 #endif
38
39 #ifdef HAVE_DIRECT_H
40 #include <direct.h>
41 #endif
42
43 #include <string.h>
44 #include <stdlib.h>
45 #include <errno.h>
46
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50
51 #include "filesystem.h"
52 #include "privileges.h"
53 #include <wiretap/file_util.h>
54 #include "report_err.h"
55
56 /* linked list of all plugins */
57 plugin *plugin_list;
58
59 #define PLUGINS_DIR_NAME        "plugins"
60
61 /*
62  * add a new plugin to the list
63  * returns :
64  * - 0 : OK
65  * - ENOMEM : memory allocation problem
66  * - EEXIST : the same plugin (i.e. name/version) was already registered.
67  */
68 static int
69 add_plugin(void *handle, gchar *name, gchar *version,
70            void (*register_protoinfo)(void), void (*reg_handoff)(void),
71            void (*register_tap_listener)(void))
72 {
73     plugin *new_plug, *pt_plug;
74
75     pt_plug = plugin_list;
76     if (!pt_plug) /* the list is empty */
77     {
78         new_plug = (plugin *)g_malloc(sizeof(plugin));
79         if (new_plug == NULL) return ENOMEM;
80         plugin_list = new_plug;
81     }
82     else
83     {
84         while (1)
85         {
86             /* check if the same name/version is already registered */
87             if (!strcmp(pt_plug->name, name) &&
88                 !strcmp(pt_plug->version, version))
89             {
90                 return EEXIST;
91             }
92
93             /* we found the last plugin in the list */
94             if (pt_plug->next == NULL) break;
95
96             pt_plug = pt_plug->next;
97         }
98         new_plug = (plugin *)g_malloc(sizeof(plugin));
99         if (new_plug == NULL) return ENOMEM;
100         pt_plug->next = new_plug;
101     }
102
103     new_plug->handle = handle;
104     new_plug->name = name;
105     new_plug->version = version;
106     new_plug->register_protoinfo = register_protoinfo;
107     new_plug->reg_handoff = reg_handoff;
108     new_plug->register_tap_listener = register_tap_listener;
109     new_plug->next = NULL;
110     return 0;
111 }
112
113 /*
114  * XXX - when we remove support for old-style plugins (which we should
115  * probably do eventually, as all plugins should be written as new-style
116  * ones), we may want to have "init_plugins()" merely save a pointer
117  * to the plugin's "init" routine, just as we save a pointer to its
118  * "reg_handoff" routine, and have a "register_all_plugins()" routine
119  * to go through the list of plugins and call all of them.
120  *
121  * Then we'd have "epan_init()", or perhaps even something higher up
122  * in the call tree, call "init_plugins()", and have "proto_init()"
123  * call "register_all_plugins()" right after calling "register_all_protocols()";
124  * this might be a bit cleaner.
125  */
126 static void
127 plugins_scan_dir(const char *dirname)
128 {
129 #define FILENAME_LEN    1024
130     ETH_DIR       *dir;             /* scanned directory */
131     ETH_DIRENT    *file;            /* current file */
132     const char    *name;
133 #if GLIB_MAJOR_VERSION < 2
134     gchar         *hack_path;       /* pathname used to construct lt_lib_ext */
135     gchar         *lt_lib_ext;      /* extension for loadable modules */
136 #endif
137     gchar          filename[FILENAME_LEN];   /* current file name */
138     GModule       *handle;          /* handle returned by dlopen */
139     gchar         *version;
140     gpointer       gp;
141     void         (*register_protoinfo)(void);
142     void         (*reg_handoff)(void);
143     void         (*register_tap_listener)(void);
144     gchar         *dot;
145     int            cr;
146
147 #if GLIB_MAJOR_VERSION < 2
148     /*
149      * We find the extension used on this platform for loadable modules
150      * by the sneaky hack of calling "g_module_build_path" to build
151      * the pathname for a module with an empty directory name and
152      * empty module name, and then search for the last "." and use
153      * everything from the last "." on.
154      */
155     hack_path = g_module_build_path("", "");
156     lt_lib_ext = strrchr(hack_path, '.');
157     if (lt_lib_ext == NULL)
158     {
159         /*
160          * Does this mean there *is* no extension?  Assume so.
161          *
162          * XXX - the code below assumes that all loadable modules have
163          * an extension....
164          */
165         lt_lib_ext = "";
166     }
167 #endif
168
169     if ((dir = eth_dir_open(dirname, 0, NULL)) != NULL)
170     {
171     while ((file = eth_dir_read_name(dir)) != NULL)
172         {
173             name = eth_dir_get_name(file);
174 #if GLIB_MAJOR_VERSION < 2
175             /* don't try to open "." and ".." */
176             if (!(strcmp(name, "..") &&
177                   strcmp(name, "."))) continue;
178
179             /* skip anything but files with lt_lib_ext */
180             dot = strrchr(name, '.');
181             if (dot == NULL || strcmp(dot, lt_lib_ext) != 0) continue;
182
183 #else /* GLIB 2 */
184     /*
185      * GLib 2.x defines G_MODULE_SUFFIX as the extension used on this
186      * platform for loadable modules.
187      */
188             /* skip anything but files with G_MODULE_SUFFIX */
189             dot = strrchr(name, '.');
190             if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0) continue;
191
192 #endif
193             g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
194                 dirname, name);
195             if ((handle = g_module_open(filename, 0)) == NULL)
196             {
197                 report_failure("Couldn't load module %s: %s", filename,
198                           g_module_error());
199                 continue;
200             }
201             if (!g_module_symbol(handle, "version", &gp))
202             {
203                 report_failure("The plugin %s has no version symbol", name);
204                 g_module_close(handle);
205                 continue;
206             }
207             version = gp;
208             
209             /*
210              * Do we have a register routine?
211              */
212             if (g_module_symbol(handle, "plugin_register", &gp))
213             {
214                 /*
215                  * Yes - this plugin includes one or more dissectors.
216                  */
217                 register_protoinfo = gp;
218             }
219             else
220             {
221                 /*
222                  * No - no dissectors.
223                  */
224                 register_protoinfo = NULL;
225             }
226
227             /*
228              * Do we have a reg_handoff routine?
229              */
230             if (g_module_symbol(handle, "plugin_reg_handoff", &gp))
231             {
232                 /*
233                  * Yes.
234                  */
235                 reg_handoff = gp;
236             }
237             else
238             {
239                 /*
240                  * No - that's OK even if we have dissectors, as long
241                  * as the plugin registers by name *and* there's
242                  * a caller looking for that name.
243                  */
244                 reg_handoff = NULL;
245             }
246
247             /*
248              * Do we have a register_tap_listener routine?
249              */
250             if (g_module_symbol(handle, "plugin_register_tap_listener", &gp))
251             {
252                 /*
253                  * Yes - this plugin includes one or more taps.
254                  */
255                 register_tap_listener = gp;
256             }
257             else
258             {
259                 /*
260                  * No - no taps here.
261                  */
262                 register_tap_listener = NULL;
263             }
264
265             /*
266              * Do we have an old-style init routine?
267              */
268             if (g_module_symbol(handle, "plugin_init", &gp))
269             {
270                 /*
271                  * Yes - do we also have a register routine or a
272                  * register_tap_listener routine?  If so, this is a bogus
273                  * hybrid of an old-style and new-style plugin.
274                  */
275                 if (register_protoinfo != NULL || register_tap_listener != NULL)
276                 {
277                     report_failure("The plugin %s has an old plugin init routine\nand a new register or register_tap_listener routine.",
278                         name);
279                     g_module_close(handle);
280                     continue;
281                 }
282
283                 /*
284                  * It's just an unsupported old-style plugin;
285                  */
286                 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.ethereal.com/ethereal/trunk/doc/README.plugins",
287                     name);
288                 g_module_close(handle);
289                 continue;
290             }
291
292             /*
293              * Does this dissector do anything useful?
294              */
295             if (register_protoinfo == NULL &&
296                 register_tap_listener == NULL)
297             {
298                 /*
299                  * No.
300                  */
301                 report_failure("The plugin %s has neither a register routine, or a register_tap_listener routine",
302                     name);
303                 g_module_close(handle);
304                 continue;
305             }
306
307             /*
308              * OK, attempt to add it to the list of plugins.
309              */
310             if ((cr = add_plugin(handle, g_strdup(name), version,
311                                  register_protoinfo, reg_handoff,
312                                  register_tap_listener)))
313             {
314                 if (cr == EEXIST)
315                     fprintf(stderr, "The plugin %s, version %s\n"
316                             "was found in multiple directories\n", name, version);
317                 else
318                     fprintf(stderr, "Memory allocation problem\n"
319                             "when processing plugin %s, version %s\n",
320                             name, version);
321                 g_module_close(handle);
322                 continue;
323             }
324
325             /*
326              * Call its register routine if it has one.
327              * XXX - just save this and call it with the built-in
328              * dissector register routines?
329              */
330             if (register_protoinfo != NULL)
331                     register_protoinfo();
332
333         }
334         eth_dir_close(dir);
335         }
336 #if GLIB_MAJOR_VERSION < 2
337     g_free(hack_path);
338 #endif
339 }
340
341
342 /* get the global plugin dir */
343 /* Return value is malloced so the caller should g_free() it. */
344 char *get_plugins_global_dir(const char *plugin_dir)
345 {
346 #ifdef _WIN32
347         char *install_plugin_dir;
348
349         /*
350          * On Windows, the data file directory is the installation
351          * directory; the plugins are stored under it.
352          *
353          * Assume we're running the installed version of Wireshark;
354          * on Windows, the data file directory is the directory
355          * in which the Ethereal binary resides.
356          */
357         install_plugin_dir = g_strdup_printf("%s\\plugins\\%s", get_datafile_dir(), VERSION);
358
359         /*
360          * Make sure that pathname refers to a directory.
361          */
362         if (test_for_directory(install_plugin_dir) != EISDIR) {
363                 /*
364                  * Either it doesn't refer to a directory or it
365                  * refers to something that doesn't exist.
366                  *
367                  * Assume that means we're running, for example,
368                  * a version of Wireshark we've built in a source
369                  * directory, and fall back on the default
370                  * installation directory, so you can put the plugins
371                  * somewhere so they can be used with this version
372                  * of Wireshark.
373                  *
374                  * XXX - should we, instead, have the Windows build
375                  * procedure create a subdirectory of the "plugins"
376                  * source directory, and copy the plugin DLLs there,
377                  * so that you use the plugins from the build tree?
378                  */
379                 g_free(install_plugin_dir);
380                 install_plugin_dir =
381                     g_strdup("C:\\Program Files\\Ethereal\\plugins\\" VERSION);
382         }
383
384         return install_plugin_dir;
385 #else
386         /*
387          * Scan the plugin directory.
388          */
389         return g_strdup(plugin_dir);
390 #endif
391 }
392
393
394 /* get the personal plugin dir */
395 /* Return value is malloced so the caller should g_free() it. */
396 char *get_plugins_pers_dir(void)
397 {
398     return get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
399 }
400
401 /*
402  * init plugins
403  */
404 void
405 init_plugins(const char *plugin_dir)
406 {
407     char *datafile_dir;
408
409     if (plugin_list == NULL)      /* ensure init_plugins is only run once */
410     {
411         /*
412          * Scan the global plugin directory.
413          */
414         datafile_dir = get_plugins_global_dir(plugin_dir);
415         plugins_scan_dir(datafile_dir);
416         g_free(datafile_dir);
417
418         /*
419          * If the program wasn't started with special privileges,
420          * scan the users plugin directory.  (Even if we relinquish
421          * them, plugins aren't safe unless we've *permanently*
422          * relinquished them, and we can't do that in Ethereal as,
423          * if we need privileges to start capturing, we'd need to
424          * reclaim them before each time we start capturing.)
425          */
426         if (!started_with_special_privs()) {
427             datafile_dir = get_plugins_pers_dir();
428             plugins_scan_dir(datafile_dir);
429             g_free(datafile_dir);
430         }
431     }
432 }
433
434 void
435 register_all_plugin_handoffs(void)
436 {
437     plugin *pt_plug;
438
439     /*
440      * For all plugins with register-handoff routines, call the routines.
441      * This is called from "proto_init()"; it must be called after
442      * "register_all_protocols()" and "init_plugins()" are called,
443      * in case one plugin registers itself either with a built-in
444      * dissector or with another plugin; we must first register all
445      * dissectors, whether built-in or plugin, so their dissector tables
446      * are initialized, and only then register all handoffs.
447      */
448     for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
449     {
450         if (pt_plug->reg_handoff)
451             (pt_plug->reg_handoff)();
452     }
453 }
454
455 void
456 register_all_plugin_tap_listeners(void)
457 {
458     plugin *pt_plug;
459
460     /*
461      * For all plugins with register-tap-listener routines, call the
462      * routines.
463      */
464     for (pt_plug = plugin_list; pt_plug != NULL; pt_plug = pt_plug->next)
465     {
466         if (pt_plug->register_tap_listener)
467             (pt_plug->register_tap_listener)();
468     }
469 }
470 #endif