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 #include "rust-nfs-log-gen.h"
51 
52 json_t *JsonNFSAddMetadataRPC(const Flow *f, uint64_t tx_id)
53 {
54  NFSState *state = FlowGetAppState(f);
55  if (state) {
56  NFSTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_NFS, state, tx_id);
57  if (tx) {
58  return rs_rpc_log_json_response(tx);
59  }
60  }
61 
62  return NULL;
63 }
64 
65 json_t *JsonNFSAddMetadata(const Flow *f, uint64_t tx_id)
66 {
67  NFSState *state = FlowGetAppState(f);
68  if (state) {
69  NFSTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_NFS, state, tx_id);
70  if (tx) {
71  return rs_nfs_log_json_response(state, tx);
72  }
73  }
74 
75  return NULL;
76 }
77 
78 static int JsonNFSLogger(ThreadVars *tv, void *thread_data,
79  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
80 {
81  NFSTransaction *nfstx = tx;
82  OutputJsonThreadCtx *thread = thread_data;
83 
84  if (rs_nfs_tx_logging_is_filtered(state, nfstx))
85  return TM_ECODE_OK;
86 
87  json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "nfs");
88  if (unlikely(js == NULL)) {
89  return TM_ECODE_FAILED;
90  }
91 
92  JsonAddCommonOptions(&thread->ctx->cfg, p, f, js);
93 
94  json_t *rpcjs = rs_rpc_log_json_response(tx);
95  if (unlikely(rpcjs == NULL)) {
96  goto error;
97  }
98  json_object_set_new(js, "rpc", rpcjs);
99 
100  json_t *nfsjs = rs_nfs_log_json_response(state, tx);
101  if (unlikely(nfsjs == NULL)) {
102  goto error;
103  }
104  json_object_set_new(js, "nfs", nfsjs);
105 
106  MemBufferReset(thread->buffer);
107  OutputJSONBuffer(js, thread->ctx->file_ctx, &thread->buffer);
108 
109  json_decref(js);
110  return TM_ECODE_OK;
111 
112 error:
113  json_decref(js);
114  return TM_ECODE_FAILED;
115 }
116 
117 static OutputInitResult NFSLogInitSub(ConfNode *conf,
118  OutputCtx *parent_ctx)
119 {
122  return OutputJsonLogInitSub(conf, parent_ctx);
123 }
124 
126 {
127  /* Register as an eve sub-module. */
128  OutputRegisterTxSubModule(LOGGER_JSON_NFS, "eve-log", "JsonNFSLog",
129  "eve-log.nfs", NFSLogInitSub, ALPROTO_NFS,
130  JsonNFSLogger, JsonLogThreadInit,
131  JsonLogThreadDeinit, NULL);
132 
133  SCLogDebug("NFS JSON logger registered.");
134 }
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
#define SCLogDebug(...)
Definition: util-debug.h:335
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
OutputJsonCtx * ctx
Definition: output-json.h:86
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
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:390
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
struct NFSTransaction_ NFSTransaction
Definition: rust.h:55
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
json_t * JsonNFSAddMetadataRPC(const Flow *f, uint64_t tx_id)
OutputJsonCommonSettings cfg
Definition: output-json.h:81
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
LogFileCtx * file_ctx
Definition: output-json.h:79
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
json_t * JsonNFSAddMetadata(const Flow *f, uint64_t tx_id)
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:402
Definition: conf.h:32
uint16_t tx_id
MemBuffer * buffer
Definition: output-json.h:87
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1068
Per thread variable structure.
Definition: threadvars.h:57
void JsonNFSLogRegister(void)
Flow data structure.
Definition: flow.h:325