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 "app-layer-protos.h"
29 #include "app-layer-parser.h"
30 #include "detect-engine-register.h"
31 #include "output.h"
32 
33 #include <dlfcn.h>
34 
35 typedef struct PluginListNode_ {
36  SCPlugin *plugin;
37  void *lib;
38  TAILQ_ENTRY(PluginListNode_) entries;
39 } PluginListNode;
40 
41 /**
42  * The list of loaded plugins.
43  *
44  * Currently only used as a place to stash the pointer returned from
45  * dlopen, but could have other uses, such as a plugin unload destructor.
46  */
47 static TAILQ_HEAD(, PluginListNode_) plugins = TAILQ_HEAD_INITIALIZER(plugins);
48 
49 static TAILQ_HEAD(, SCCapturePlugin_) capture_plugins = TAILQ_HEAD_INITIALIZER(capture_plugins);
50 
51 bool RegisterPlugin(SCPlugin *plugin, void *lib)
52 {
53  BUG_ON(plugin->name == NULL);
54  BUG_ON(plugin->author == NULL);
55  BUG_ON(plugin->license == NULL);
56  BUG_ON(plugin->Init == NULL);
57 
58  PluginListNode *node = SCCalloc(1, sizeof(*node));
59  if (node == NULL) {
60  SCLogError("Failed to allocate memory for plugin");
61  return false;
62  }
63  node->plugin = plugin;
64  node->lib = lib;
65  TAILQ_INSERT_TAIL(&plugins, node, entries);
66  SCLogNotice("Initializing plugin %s; author=%s; license=%s", plugin->name, plugin->author,
67  plugin->license);
68  (*plugin->Init)();
69  return true;
70 }
71 
72 static void InitPlugin(char *path)
73 {
74  void *lib = dlopen(path, RTLD_NOW);
75  if (lib == NULL) {
76  SCLogNotice("Failed to open %s as a plugin: %s", path, dlerror());
77  } else {
78  SCLogNotice("Loading plugin %s", path);
79 
80  SCPluginRegisterFunc plugin_register = dlsym(lib, "SCPluginRegister");
81  if (plugin_register == NULL) {
82  SCLogError("Plugin does not export SCPluginRegister function: %s", path);
83  dlclose(lib);
84  return;
85  }
86 
87  if (!RegisterPlugin(plugin_register(), lib)) {
88  SCLogError("Plugin registration failed: %s", path);
89  dlclose(lib);
90  return;
91  }
92  }
93 }
94 
95 void SCPluginsLoad(const char *capture_plugin_name, const char *capture_plugin_args)
96 {
97  ConfNode *conf = ConfGetNode("plugins");
98  if (conf == NULL) {
99  return;
100  }
101  ConfNode *plugin = NULL;
102  TAILQ_FOREACH(plugin, &conf->head, next) {
103  struct stat statbuf;
104  if (stat(plugin->val, &statbuf) == -1) {
105  SCLogError("Bad plugin path: %s: %s", plugin->val, strerror(errno));
106  continue;
107  }
108  if (S_ISDIR(statbuf.st_mode)) {
109  // coverity[toctou : FALSE]
110  DIR *dir = opendir(plugin->val);
111  if (dir == NULL) {
112  SCLogError("Failed to open plugin directory %s: %s", plugin->val, strerror(errno));
113  continue;
114  }
115  struct dirent *entry = NULL;
116  char path[PATH_MAX];
117  while ((entry = readdir(dir)) != NULL) {
118  if (strstr(entry->d_name, ".so") != NULL) {
119  snprintf(path, sizeof(path), "%s/%s", plugin->val, entry->d_name);
120  InitPlugin(path);
121  }
122  }
123  closedir(dir);
124  } else {
125  InitPlugin(plugin->val);
126  }
127  }
128 
129  if (SCRunmodeGet() == RUNMODE_PLUGIN) {
130  SCCapturePlugin *capture = SCPluginFindCaptureByName(capture_plugin_name);
131  if (capture == NULL) {
132  FatalError("No capture plugin found with name %s", capture_plugin_name);
133  }
134  capture->Init(capture_plugin_args, RUNMODE_PLUGIN, TMM_RECEIVEPLUGIN,
136  }
137 }
138 
140 {
141  TAILQ_INSERT_TAIL(&capture_plugins, plugin, entries);
142  SCLogNotice("Capture plugin registered: %s", plugin->name);
143  return 0;
144 }
145 
147 {
148  SCCapturePlugin *plugin = NULL;
149  TAILQ_FOREACH(plugin, &capture_plugins, entries) {
150  if (strcmp(name, plugin->name) == 0) {
151  return plugin;
152  }
153  }
154  return plugin;
155 }
156 
158 {
159  if (plugin->version != SC_PLUGIN_API_VERSION) {
160  return 1;
161  }
162  AppProto alproto = g_alproto_max;
163  AppProtoRegisterProtoString(alproto, plugin->name);
164  if (plugin->Register) {
165  if (AppLayerParserPreRegister(plugin->Register) != 0) {
166  return 1;
167  }
168  }
169  if (plugin->KeywordsRegister) {
170  if (SigTablePreRegister(plugin->KeywordsRegister) != 0) {
171  return 1;
172  }
173  }
174  if (plugin->Logger) {
176  .confname = plugin->confname,
177  .logname = plugin->logname,
178  .alproto = alproto,
179  .LogTx = (EveJsonSimpleTxLogFunc)plugin->Logger,
180  };
181  if (OutputPreRegisterLogger(reg_data) != 0) {
182  return 1;
183  }
184  }
185  return 0;
186 }
187 #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
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:85
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
detect-engine-register.h
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
SCAppLayerPlugin_::KeywordsRegister
void(* KeywordsRegister)(void)
Definition: suricata-plugin.h:63
EveJsonSimpleTxLogFunc
bool(* EveJsonSimpleTxLogFunc)(void *, struct JsonBuilder *)
Definition: output.h:164
TAILQ_ENTRY
#define TAILQ_ENTRY(type)
Definition: queue.h:239
TMM_DECODEPLUGIN
@ TMM_DECODEPLUGIN
Definition: tm-threads-common.h:43
SCAppLayerPlugin_::logname
char * logname
Definition: suricata-plugin.h:64
SCAppLayerPlugin_::name
char * name
Definition: suricata-plugin.h:61
TAILQ_HEAD_INITIALIZER
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:236
util-debug.h
g_alproto_max
AppProto g_alproto_max
Definition: app-layer-protos.c:29
SCCapturePlugin_::name
char * name
Definition: suricata-plugin.h:45
AppProtoRegisterProtoString
void AppProtoRegisterProtoString(AppProto alproto, const char *proto_name)
Definition: app-layer-protos.c:74
SCCapturePlugin_
Definition: suricata-plugin.h:44
RegisterPlugin
bool RegisterPlugin(SCPlugin *, void *)
app-layer-parser.h
SCAppLayerPlugin_
Definition: suricata-plugin.h:58
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
EveJsonTxLoggerRegistrationData
Definition: output.h:173
conf.h
name
const char * name
Definition: tm-threads.c:2081
util-plugin.h
SC_PLUGIN_API_VERSION
#define SC_PLUGIN_API_VERSION
Definition: suricata-plugin.h:56
runmodes.h
SCPluginsLoad
void SCPluginsLoad(const char *capture_plugin_name, const char *capture_plugin_args)
SCAppLayerPlugin_::Register
void(* Register)(void)
Definition: suricata-plugin.h:62
SCPluginRegisterAppLayer
int SCPluginRegisterAppLayer(SCAppLayerPlugin *)
AppLayerParserPreRegister
int AppLayerParserPreRegister(void(*Register)(void))
Definition: app-layer-parser.c:1708
suricata-common.h
EveJsonTxLoggerRegistrationData::confname
const char * confname
Definition: output.h:174
SCPlugin_::Init
void(* Init)(void)
Definition: suricata-plugin.h:39
FatalError
#define FatalError(...)
Definition: util-debug.h:502
SigTablePreRegister
int SigTablePreRegister(void(*KeywordsRegister)(void))
Definition: detect-engine-register.c:474
SCAppLayerPlugin_::confname
char * confname
Definition: suricata-plugin.h:65
SCAppLayerPlugin_::Logger
bool(* Logger)(void *tx, void *jb)
Definition: suricata-plugin.h:66
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
app-layer-protos.h
suricata.h
SCPlugin_::name
const char * name
Definition: suricata-plugin.h:36
OutputPreRegisterLogger
int OutputPreRegisterLogger(EveJsonTxLoggerRegistrationData reg_data)
Definition: output.c:961
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:237
SCAppLayerPlugin_::version
uint64_t version
Definition: suricata-plugin.h:60
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
RUNMODE_PLUGIN
@ RUNMODE_PLUGIN
Definition: runmodes.h:43
SCPluginFindCaptureByName
SCCapturePlugin * SCPluginFindCaptureByName(const char *name)
output.h
SCPluginRegisterFunc
SCPlugin *(* SCPluginRegisterFunc)(void)
Definition: suricata-plugin.h:42