suricata
util-plugin.c
Go to the documentation of this file.
1 /* Copyright (C) 2020-2021 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 #include "suricata-common.h"
19 #include "suricata-plugin.h"
20 #include "suricata.h"
21 #include "runmodes.h"
22 #include "util-plugin.h"
23 #include "util-debug.h"
24 #include "conf.h"
25 
26 #ifdef HAVE_PLUGINS
27 
28 #include <dlfcn.h>
29 
30 typedef struct PluginListNode_ {
31  SCPlugin *plugin;
32  void *lib;
33  TAILQ_ENTRY(PluginListNode_) entries;
34 } PluginListNode;
35 
36 /**
37  * The list of loaded plugins.
38  *
39  * Currently only used as a place to stash the pointer returned from
40  * dlopen, but could have other uses, such as a plugin unload destructor.
41  */
42 static TAILQ_HEAD(, PluginListNode_) plugins = TAILQ_HEAD_INITIALIZER(plugins);
43 
44 static TAILQ_HEAD(, SCCapturePlugin_) capture_plugins = TAILQ_HEAD_INITIALIZER(capture_plugins);
45 
46 bool RegisterPlugin(SCPlugin *plugin, void *lib)
47 {
48  BUG_ON(plugin->name == NULL);
49  BUG_ON(plugin->author == NULL);
50  BUG_ON(plugin->license == NULL);
51  BUG_ON(plugin->Init == NULL);
52 
53  PluginListNode *node = SCCalloc(1, sizeof(*node));
54  if (node == NULL) {
55  SCLogError("Failed to allocate memory for plugin");
56  return false;
57  }
58  node->plugin = plugin;
59  node->lib = lib;
60  TAILQ_INSERT_TAIL(&plugins, node, entries);
61  SCLogNotice("Initializing plugin %s; author=%s; license=%s", plugin->name, plugin->author,
62  plugin->license);
63  (*plugin->Init)();
64  return true;
65 }
66 
67 static void InitPlugin(char *path)
68 {
69  void *lib = dlopen(path, RTLD_NOW);
70  if (lib == NULL) {
71  SCLogNotice("Failed to open %s as a plugin: %s", path, dlerror());
72  } else {
73  SCLogNotice("Loading plugin %s", path);
74 
75  SCPluginRegisterFunc plugin_register = dlsym(lib, "SCPluginRegister");
76  if (plugin_register == NULL) {
77  SCLogError("Plugin does not export SCPluginRegister function: %s", path);
78  dlclose(lib);
79  return;
80  }
81 
82  if (!RegisterPlugin(plugin_register(), lib)) {
83  SCLogError("Plugin registration failed: %s", path);
84  dlclose(lib);
85  return;
86  }
87  }
88 }
89 
90 void SCPluginsLoad(const char *capture_plugin_name, const char *capture_plugin_args)
91 {
92  ConfNode *conf = ConfGetNode("plugins");
93  if (conf == NULL) {
94  return;
95  }
96  ConfNode *plugin = NULL;
97  TAILQ_FOREACH(plugin, &conf->head, next) {
98  struct stat statbuf;
99  if (stat(plugin->val, &statbuf) == -1) {
100  SCLogError("Bad plugin path: %s: %s", plugin->val, strerror(errno));
101  continue;
102  }
103  if (S_ISDIR(statbuf.st_mode)) {
104  // coverity[toctou : FALSE]
105  DIR *dir = opendir(plugin->val);
106  if (dir == NULL) {
107  SCLogError("Failed to open plugin directory %s: %s", plugin->val, strerror(errno));
108  continue;
109  }
110  struct dirent *entry = NULL;
111  char path[PATH_MAX];
112  while ((entry = readdir(dir)) != NULL) {
113  if (strstr(entry->d_name, ".so") != NULL) {
114  snprintf(path, sizeof(path), "%s/%s", plugin->val, entry->d_name);
115  InitPlugin(path);
116  }
117  }
118  closedir(dir);
119  } else {
120  InitPlugin(plugin->val);
121  }
122  }
123 
124  if (SCRunmodeGet() == RUNMODE_PLUGIN) {
125  SCCapturePlugin *capture = SCPluginFindCaptureByName(capture_plugin_name);
126  if (capture == NULL) {
127  FatalError("No capture plugin found with name %s", capture_plugin_name);
128  }
129  capture->Init(capture_plugin_args, RUNMODE_PLUGIN, TMM_RECEIVEPLUGIN,
131  }
132 }
133 
135 {
136  TAILQ_INSERT_TAIL(&capture_plugins, plugin, entries);
137  SCLogNotice("Capture plugin registered: %s", plugin->name);
138  return 0;
139 }
140 
142 {
143  SCCapturePlugin *plugin = NULL;
144  TAILQ_FOREACH(plugin, &capture_plugins, entries) {
145  if (strcmp(name, plugin->name) == 0) {
146  return plugin;
147  }
148  }
149  return plugin;
150 }
151 #endif
suricata-plugin.h
SCPluginRegisterCapture
int SCPluginRegisterCapture(SCCapturePlugin *)
TMM_RECEIVEPLUGIN
@ TMM_RECEIVEPLUGIN
Definition: tm-threads-common.h:42
SCRunmodeGet
int SCRunmodeGet(void)
Get the current run mode.
Definition: suricata.c:260
ConfNode_::val
char * val
Definition: conf.h:34
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
SCCapturePlugin_::Init
void(* Init)(const char *args, int plugin_slot, int receive_slot, int decode_slot)
Definition: suricata-plugin.h:46
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
TAILQ_INSERT_TAIL
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:294
TAILQ_ENTRY
#define TAILQ_ENTRY(type)
Definition: queue.h:239
TMM_DECODEPLUGIN
@ TMM_DECODEPLUGIN
Definition: tm-threads-common.h:43
TAILQ_HEAD_INITIALIZER
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:236
util-debug.h
SCCapturePlugin_::name
char * name
Definition: suricata-plugin.h:45
SCCapturePlugin_
Definition: suricata-plugin.h:44
RegisterPlugin
bool RegisterPlugin(SCPlugin *, void *)
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
SCPlugin_::license
const char * license
Definition: suricata-plugin.h:37
SCPlugin_::author
const char * author
Definition: suricata-plugin.h:38
SCPlugin_
Definition: suricata-plugin.h:35
conf.h
util-plugin.h
runmodes.h
SCPluginsLoad
void SCPluginsLoad(const char *capture_plugin_name, const char *capture_plugin_args)
suricata-common.h
SCPlugin_::Init
void(* Init)(void)
Definition: suricata-plugin.h:39
FatalError
#define FatalError(...)
Definition: util-debug.h:502
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
ConfNode_
Definition: conf.h:32
TAILQ_HEAD
#define TAILQ_HEAD(name, type)
Definition: queue.h:230
suricata.h
SCPlugin_::name
const char * name
Definition: suricata-plugin.h:36
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:237
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
RUNMODE_PLUGIN
@ RUNMODE_PLUGIN
Definition: runmodes.h:43
SCPluginFindCaptureByName
SCCapturePlugin * SCPluginFindCaptureByName(const char *name)
SCPluginRegisterFunc
SCPlugin *(* SCPluginRegisterFunc)(void)
Definition: suricata-plugin.h:42