]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
templates: C stub output for Rust logger
authorJason Ish <ish@unx.ca>
Thu, 30 Aug 2018 15:34:18 +0000 (09:34 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 19 Sep 2018 19:00:51 +0000 (21:00 +0200)
src/Makefile.am
src/output-json-template-rust.c [new file with mode: 0644]
src/output-json-template-rust.h [new file with mode: 0644]
src/output.c
src/suricata-common.h
src/util-profiling.c

index e9f3b815978736f871b181ac3af47715b2d64a1d..a0d3a7d192563db2456cc3a41dd0357e9e7e764f 100644 (file)
@@ -334,6 +334,7 @@ output-json-ikev2.c output-json-ikev2.h \
 output-json-krb5.c output-json-krb5.h \
 output-json-dhcp.c output-json-dhcp.h \
 output-json-template.c output-json-template.h \
+output-json-template-rust.c output-json-template-rust.h \
 output-json-metadata.c output-json-metadata.h \
 output-lua.c output-lua.h \
 output-packet.c output-packet.h \
diff --git a/src/output-json-template-rust.c b/src/output-json-template-rust.c
new file mode 100644 (file)
index 0000000..1d71972
--- /dev/null
@@ -0,0 +1,197 @@
+/* Copyright (C) 2018 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * 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
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/*
+ * TODO: Update \author in this file and in output-json-template.h.
+ * TODO: Remove SCLogNotice statements, or convert to debug.
+ * TODO: Implement your app-layers logging.
+ */
+
+/**
+ * \file
+ *
+ * \author FirstName LastName <yourname@domain>
+ *
+ * Implement JSON/eve logging app-layer Template.
+ */
+
+#include "suricata-common.h"
+#include "debug.h"
+#include "detect.h"
+#include "pkt-var.h"
+#include "conf.h"
+
+#include "threads.h"
+#include "threadvars.h"
+#include "tm-threads.h"
+
+#include "util-unittest.h"
+#include "util-buffer.h"
+#include "util-debug.h"
+#include "util-byte.h"
+
+#include "output.h"
+#include "output-json.h"
+
+#include "app-layer.h"
+#include "app-layer-parser.h"
+
+#include "app-layer-template-rust.h"
+#include "output-json-template-rust.h"
+
+#if defined(HAVE_LIBJANSSON) && defined(HAVE_RUST)
+
+#include "rust-applayertemplate-logger-gen.h"
+
+typedef struct LogTemplateFileCtx_ {
+    LogFileCtx *file_ctx;
+    uint32_t    flags;
+} LogTemplateFileCtx;
+
+typedef struct LogTemplateLogThread_ {
+    LogTemplateFileCtx *templatelog_ctx;
+    uint32_t            count;
+    MemBuffer          *buffer;
+} LogTemplateLogThread;
+
+static int JsonTemplateLogger(ThreadVars *tv, void *thread_data,
+    const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
+{
+    SCLogNotice("JsonTemplateLogger");
+    LogTemplateLogThread *thread = thread_data;
+
+    json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "template-rust");
+    if (unlikely(js == NULL)) {
+        return TM_ECODE_FAILED;
+    }
+
+    json_t *template_js = rs_template_logger_log(tx);
+    if (unlikely(template_js == NULL)) {
+        goto error;
+    }
+    json_object_set_new(js, "template", template_js);
+
+    MemBufferReset(thread->buffer);
+    OutputJSONBuffer(js, thread->templatelog_ctx->file_ctx, &thread->buffer);
+    json_decref(js);
+
+    return TM_ECODE_OK;
+
+error:
+    json_decref(js);
+    return TM_ECODE_FAILED;
+}
+
+static void OutputTemplateLogDeInitCtxSub(OutputCtx *output_ctx)
+{
+    LogTemplateFileCtx *templatelog_ctx = (LogTemplateFileCtx *)output_ctx->data;
+    SCFree(templatelog_ctx);
+    SCFree(output_ctx);
+}
+
+static OutputInitResult OutputTemplateLogInitSub(ConfNode *conf,
+    OutputCtx *parent_ctx)
+{
+    OutputInitResult result = { NULL, false };
+    OutputJsonCtx *ajt = parent_ctx->data;
+
+    LogTemplateFileCtx *templatelog_ctx = SCCalloc(1, sizeof(*templatelog_ctx));
+    if (unlikely(templatelog_ctx == NULL)) {
+        return result;
+    }
+    templatelog_ctx->file_ctx = ajt->file_ctx;
+
+    OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
+    if (unlikely(output_ctx == NULL)) {
+        SCFree(templatelog_ctx);
+        return result;
+    }
+    output_ctx->data = templatelog_ctx;
+    output_ctx->DeInit = OutputTemplateLogDeInitCtxSub;
+
+    SCLogNotice("Template log sub-module initialized.");
+
+    AppLayerParserRegisterLogger(IPPROTO_TCP, ALPROTO_TEMPLATE_RUST);
+
+    result.ctx = output_ctx;
+    result.ok = true;
+    return result;
+}
+
+#define OUTPUT_BUFFER_SIZE 65535
+
+static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, const void *initdata, void **data)
+{
+    LogTemplateLogThread *thread = SCCalloc(1, sizeof(*thread));
+    if (unlikely(thread == NULL)) {
+        return TM_ECODE_FAILED;
+    }
+
+    if (initdata == NULL) {
+        SCLogDebug("Error getting context for EveLogTemplate.  \"initdata\" is NULL.");
+        SCFree(thread);
+        return TM_ECODE_FAILED;
+    }
+
+    thread->buffer = MemBufferCreateNew(OUTPUT_BUFFER_SIZE);
+    if (unlikely(thread->buffer == NULL)) {
+        SCFree(thread);
+        return TM_ECODE_FAILED;
+    }
+
+    thread->templatelog_ctx = ((OutputCtx *)initdata)->data;
+    *data = (void *)thread;
+
+    return TM_ECODE_OK;
+}
+
+static TmEcode JsonTemplateLogThreadDeinit(ThreadVars *t, void *data)
+{
+    LogTemplateLogThread *thread = (LogTemplateLogThread *)data;
+    if (thread == NULL) {
+        return TM_ECODE_OK;
+    }
+    if (thread->buffer != NULL) {
+        MemBufferFree(thread->buffer);
+    }
+    SCFree(thread);
+    return TM_ECODE_OK;
+}
+
+void JsonTemplateRustLogRegister(void)
+{
+    /* TEMPLATE_START_REMOVE */
+    if (ConfGetNode("app-layer.protocols.template-rust") == NULL) {
+        return;
+    }
+    /* TEMPLATE_END_REMOVE */
+    /* Register as an eve sub-module. */
+    OutputRegisterTxSubModule(LOGGER_JSON_TEMPLATE_RUST, "eve-log",
+        "JsonTemplateRustLog", "eve-log.template-rust",
+        OutputTemplateLogInitSub, ALPROTO_TEMPLATE_RUST, JsonTemplateLogger,
+        JsonTemplateLogThreadInit, JsonTemplateLogThreadDeinit, NULL);
+
+    SCLogNotice("Template JSON logger registered.");
+}
+
+#else /* No JSON support. */
+
+void JsonTemplateRustLogRegister(void)
+{
+}
+
+#endif /* HAVE_LIBJANSSON */
diff --git a/src/output-json-template-rust.h b/src/output-json-template-rust.h
new file mode 100644 (file)
index 0000000..c988e14
--- /dev/null
@@ -0,0 +1,29 @@
+/* Copyright (C) 2018 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * 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
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author FirstName LastName <name@domain>
+ */
+
+#ifndef __OUTPUT_JSON_TEMPLATE_RUST_H__
+#define __OUTPUT_JSON_TEMPLATE_RUST_H__
+
+void JsonTemplateRustLogRegister(void);
+
+#endif /* __OUTPUT_JSON_TEMPLATE_RUST_H__ */
index e27c24fa3f5eeb2095ced5f099cf09edbfd1f8de..6d96b6724262985b6a7c0044fff80b2de878ad4f 100644 (file)
@@ -75,6 +75,7 @@
 #include "output-json-krb5.h"
 #include "output-json-dhcp.h"
 #include "output-json-template.h"
+#include "output-json-template-rust.h"
 #include "output-lua.h"
 #include "output-json-dnp3.h"
 #include "output-json-metadata.h"
@@ -1106,4 +1107,6 @@ void OutputRegisterLoggers(void)
     JsonDHCPLogRegister();
     /* Template JSON logger. */
     JsonTemplateLogRegister();
+    /* Template Rust JSON logger. */
+    JsonTemplateRustLogRegister();
 }
index e8dc0ed8fc82a803fc852e5d9c375b5f95fc86e5..153fb3645d5dec98142122d7cf406fb7d8317938 100644 (file)
@@ -419,6 +419,7 @@ typedef enum {
     LOGGER_JSON_IKEV2,
     LOGGER_JSON_KRB5,
     LOGGER_JSON_DHCP,
+    LOGGER_JSON_TEMPLATE_RUST,
     LOGGER_JSON_TEMPLATE,
 
     LOGGER_ALERT_DEBUG,
index 2e83cefd26ef34b4b413fb842a151405876945b2..e70de4effda8554c091f3feda30d44f0b4db7c0e 100644 (file)
@@ -1258,6 +1258,7 @@ const char * PacketProfileLoggertIdToString(LoggerId id)
         CASE_CODE (LOGGER_JSON_TFTP);
         CASE_CODE (LOGGER_JSON_SMTP);
         CASE_CODE (LOGGER_JSON_TLS);
+        CASE_CODE (LOGGER_JSON_TEMPLATE_RUST);
         CASE_CODE (LOGGER_JSON_TEMPLATE);
         CASE_CODE (LOGGER_TLS_STORE);
         CASE_CODE (LOGGER_TLS);