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 #ifdef HAVE_RUST
50 #ifdef HAVE_LIBJANSSON
51 #include "rust.h"
52 #include "rust-nfs-log-gen.h"
53 
54 json_t *JsonNFSAddMetadataRPC(const Flow *f, uint64_t tx_id)
55 {
56  NFSState *state = FlowGetAppState(f);
57  if (state) {
58  NFSTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_NFS, state, tx_id);
59  if (tx) {
60  return rs_rpc_log_json_response(tx);
61  }
62  }
63 
64  return NULL;
65 }
66 
67 json_t *JsonNFSAddMetadata(const Flow *f, uint64_t tx_id)
68 {
69  NFSState *state = FlowGetAppState(f);
70  if (state) {
71  NFSTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_NFS, state, tx_id);
72  if (tx) {
73  return rs_nfs_log_json_response(state, tx);
74  }
75  }
76 
77  return NULL;
78 }
79 
80 static int JsonNFSLogger(ThreadVars *tv, void *thread_data,
81  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
82 {
83  NFSTransaction *nfstx = tx;
84  OutputJsonThreadCtx *thread = thread_data;
85 
86  if (rs_nfs_tx_logging_is_filtered(state, nfstx))
87  return TM_ECODE_OK;
88 
89  json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "nfs");
90  if (unlikely(js == NULL)) {
91  return TM_ECODE_FAILED;
92  }
93 
94  JsonAddCommonOptions(&thread->ctx->cfg, p, f, js);
95 
96  json_t *rpcjs = rs_rpc_log_json_response(tx);
97  if (unlikely(rpcjs == NULL)) {
98  goto error;
99  }
100  json_object_set_new(js, "rpc", rpcjs);
101 
102  json_t *nfsjs = rs_nfs_log_json_response(state, tx);
103  if (unlikely(nfsjs == NULL)) {
104  goto error;
105  }
106  json_object_set_new(js, "nfs", nfsjs);
107 
108  MemBufferReset(thread->buffer);
109  OutputJSONBuffer(js, thread->ctx->file_ctx, &thread->buffer);
110 
111  json_decref(js);
112  return TM_ECODE_OK;
113 
114 error:
115  json_decref(js);
116  return TM_ECODE_FAILED;
117 }
118 
119 static OutputInitResult NFSLogInitSub(ConfNode *conf,
120  OutputCtx *parent_ctx)
121 {
124  return OutputJsonLogInitSub(conf, parent_ctx);
125 }
126 
127 void JsonNFSLogRegister(void)
128 {
129  /* Register as an eve sub-module. */
130  OutputRegisterTxSubModule(LOGGER_JSON_NFS, "eve-log", "JsonNFSLog",
131  "eve-log.nfs", NFSLogInitSub, ALPROTO_NFS,
132  JsonNFSLogger, JsonLogThreadInit,
133  JsonLogThreadDeinit, NULL);
134 
135  SCLogDebug("NFS JSON logger registered.");
136 }
137 
138 #else /* No JSON support. */
139 
140 void JsonNFSLogRegister(void)
141 {
142 }
143 
144 #endif /* HAVE_LIBJANSSON */
145 
146 #else /* no rust */
147 
149 {
150 }
151 
152 #endif /* HAVE_RUST */
#define SCLogDebug(...)
Definition: util-debug.h:335
uint8_t proto
Definition: flow.h:344
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
struct NFState_ NFSState
Definition: rust.h:54
struct NFSTransaction_ NFSTransaction
Definition: rust.h:55
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
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:400
Definition: conf.h:32
uint16_t tx_id
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1065
Per thread variable structure.
Definition: threadvars.h:57
void JsonNFSLogRegister(void)
Flow data structure.
Definition: flow.h:325