suricata
output-json-nfs.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 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 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Implement JSON/eve logging app-layer NFS.
24  */
25 
26 #include "suricata-common.h"
27 #include "debug.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "threadvars.h"
34 #include "tm-threads.h"
35 
36 #include "util-unittest.h"
37 #include "util-buffer.h"
38 #include "util-debug.h"
39 #include "util-byte.h"
40 
41 #include "output.h"
42 #include "output-json.h"
43 
44 #include "app-layer.h"
45 #include "app-layer-parser.h"
46 
47 #include "output-json-nfs.h"
48 
49 #include "rust.h"
50 
51 json_t *JsonNFSAddMetadataRPC(const Flow *f, uint64_t tx_id)
52 {
53  NFSState *state = FlowGetAppState(f);
54  if (state) {
55  NFSTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_NFS, state, tx_id);
56  if (tx) {
57  return rs_rpc_log_json_response(tx);
58  }
59  }
60 
61  return NULL;
62 }
63 
64 json_t *JsonNFSAddMetadata(const Flow *f, uint64_t tx_id)
65 {
66  NFSState *state = FlowGetAppState(f);
67  if (state) {
68  NFSTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_NFS, state, tx_id);
69  if (tx) {
70  return rs_nfs_log_json_response(state, tx);
71  }
72  }
73 
74  return NULL;
75 }
76 
77 static int JsonNFSLogger(ThreadVars *tv, void *thread_data,
78  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
79 {
80  NFSTransaction *nfstx = tx;
81  OutputJsonThreadCtx *thread = thread_data;
82 
83  if (rs_nfs_tx_logging_is_filtered(state, nfstx))
84  return TM_ECODE_OK;
85 
86  json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "nfs");
87  if (unlikely(js == NULL)) {
88  return TM_ECODE_FAILED;
89  }
90 
91  JsonAddCommonOptions(&thread->ctx->cfg, p, f, js);
92 
93  json_t *rpcjs = rs_rpc_log_json_response(tx);
94  if (unlikely(rpcjs == NULL)) {
95  goto error;
96  }
97  json_object_set_new(js, "rpc", rpcjs);
98 
99  json_t *nfsjs = rs_nfs_log_json_response(state, tx);
100  if (unlikely(nfsjs == NULL)) {
101  goto error;
102  }
103  json_object_set_new(js, "nfs", nfsjs);
104 
105  MemBufferReset(thread->buffer);
106  OutputJSONBuffer(js, thread->ctx->file_ctx, &thread->buffer);
107 
108  json_decref(js);
109  return TM_ECODE_OK;
110 
111 error:
112  json_decref(js);
113  return TM_ECODE_FAILED;
114 }
115 
116 static OutputInitResult NFSLogInitSub(ConfNode *conf,
117  OutputCtx *parent_ctx)
118 {
121  return OutputJsonLogInitSub(conf, parent_ctx);
122 }
123 
125 {
126  /* Register as an eve sub-module. */
127  OutputRegisterTxSubModule(LOGGER_JSON_NFS, "eve-log", "JsonNFSLog",
128  "eve-log.nfs", NFSLogInitSub, ALPROTO_NFS,
129  JsonNFSLogger, JsonLogThreadInit,
130  JsonLogThreadDeinit, NULL);
131 
132  SCLogDebug("NFS JSON logger registered.");
133 }
util-byte.h
tm-threads.h
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:50
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:86
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:81
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:335
Flow_::proto
uint8_t proto
Definition: flow.h:361
JsonLogThreadInit
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
Definition: output-json-common.c:75
threads.h
Flow_
Flow data structure.
Definition: flow.h:342
rust.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
JsonNFSAddMetadataRPC
json_t * JsonNFSAddMetadataRPC(const Flow *f, uint64_t tx_id)
Definition: output-json-nfs.c:51
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1087
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:85
util-debug.h
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
app-layer-parser.h
Packet_
Definition: decode.h:408
conf.h
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1047
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:41
LOGGER_JSON_NFS
@ LOGGER_JSON_NFS
Definition: suricata-common.h:430
OutputJsonThreadCtx_::buffer
MemBuffer * buffer
Definition: output-json.h:87
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:37
suricata-common.h
CreateJSONHeader
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
output-json-nfs.h
JsonNFSAddMetadata
json_t * JsonNFSAddMetadata(const Flow *f, uint64_t tx_id)
Definition: output-json-nfs.c:64
JsonAddCommonOptions
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:390
OutputRegisterTxSubModule
void OutputRegisterTxSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:411
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
ConfNode_
Definition: conf.h:32
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:79
OutputJSONBuffer
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:97
ALPROTO_NFS
@ ALPROTO_NFS
Definition: app-layer-protos.h:45
debug.h
output.h
JsonNFSLogRegister
void JsonNFSLogRegister(void)
Definition: output-json-nfs.c:124
app-layer.h