suricata
output-json-pgsql.c
Go to the documentation of this file.
1 /* Copyright (C) 2022-2024 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 Juliana Fajardini <jufajardini@oisf.net>
22  *
23  * Implement JSON/eve logging for app-layer Pgsql.
24  */
25 
26 #include "suricata-common.h"
27 #include "detect.h"
28 #include "pkt-var.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 
35 #include "util-unittest.h"
36 #include "util-buffer.h"
37 #include "util-debug.h"
38 #include "util-byte.h"
39 
40 #include "output.h"
41 #include "output-json.h"
42 
43 #include "app-layer.h"
44 #include "app-layer-parser.h"
45 
46 #include "output-json-pgsql.h"
47 #include "rust.h"
48 
49 #define PGSQL_LOG_PASSWORDS BIT_U32(0)
50 #define PGSQL_DEFAULTS (PGSQL_LOG_PASSWORDS)
51 
52 typedef struct OutputPgsqlCtx_ {
53  uint32_t flags;
56 
57 typedef struct LogPgsqlLogThread_ {
61 
62 bool JsonPgsqlAddMetadata(void *vtx, JsonBuilder *jb)
63 {
64  return rs_pgsql_logger(vtx, PGSQL_DEFAULTS, jb);
65 }
66 
67 static int JsonPgsqlLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state,
68  void *txptr, uint64_t tx_id)
69 {
70  LogPgsqlLogThread *thread = thread_data;
71  SCLogDebug("Logging pgsql transaction %" PRIu64 ".", tx_id);
72 
73  JsonBuilder *jb =
74  CreateEveHeader(p, LOG_DIR_FLOW, "pgsql", NULL, thread->pgsqllog_ctx->eve_ctx);
75  if (unlikely(jb == NULL)) {
76  return TM_ECODE_FAILED;
77  }
78 
79  jb_open_object(jb, "pgsql");
80  if (!rs_pgsql_logger(txptr, thread->pgsqllog_ctx->flags, jb)) {
81  goto error;
82  }
83  jb_close(jb);
84 
85  OutputJsonBuilderBuffer(jb, thread->ctx);
86  jb_free(jb);
87 
88  return TM_ECODE_OK;
89 
90 error:
91  jb_free(jb);
92  return TM_ECODE_FAILED;
93 }
94 
95 static void OutputPgsqlLogDeInitCtxSub(OutputCtx *output_ctx)
96 {
97  OutputPgsqlCtx *pgsqllog_ctx = (OutputPgsqlCtx *)output_ctx->data;
98  SCFree(pgsqllog_ctx);
99  SCFree(output_ctx);
100 }
101 
102 static void JsonPgsqlLogParseConfig(ConfNode *conf, OutputPgsqlCtx *pgsqllog_ctx)
103 {
104  pgsqllog_ctx->flags = ~0U;
105 
106  const char *query = ConfNodeLookupChildValue(conf, "passwords");
107  if (query != NULL) {
108  if (ConfValIsTrue(query)) {
109  pgsqllog_ctx->flags |= PGSQL_LOG_PASSWORDS;
110  } else {
111  pgsqllog_ctx->flags &= ~PGSQL_LOG_PASSWORDS;
112  }
113  } else {
114  pgsqllog_ctx->flags &= ~PGSQL_LOG_PASSWORDS;
115  }
116 }
117 
118 static OutputInitResult OutputPgsqlLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
119 {
120  OutputInitResult result = { NULL, false };
121  OutputJsonCtx *ojc = parent_ctx->data;
122 
123  OutputPgsqlCtx *pgsql_ctx = SCCalloc(1, sizeof(OutputPgsqlCtx));
124  if (unlikely(pgsql_ctx == NULL))
125  return result;
126 
127  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
128  if (unlikely(output_ctx == NULL)) {
129  SCFree(pgsql_ctx);
130  return result;
131  }
132 
133  pgsql_ctx->eve_ctx = ojc;
134 
135  output_ctx->data = pgsql_ctx;
136  output_ctx->DeInit = OutputPgsqlLogDeInitCtxSub;
137 
138  JsonPgsqlLogParseConfig(conf, pgsql_ctx);
139 
141 
142  SCLogDebug("PostgreSQL log sub-module initialized.");
143 
144  result.ctx = output_ctx;
145  result.ok = true;
146  return result;
147 }
148 
149 static TmEcode JsonPgsqlLogThreadInit(ThreadVars *t, const void *initdata, void **data)
150 {
151  LogPgsqlLogThread *thread = SCCalloc(1, sizeof(LogPgsqlLogThread));
152  if (unlikely(thread == NULL)) {
153  return TM_ECODE_FAILED;
154  }
155 
156  if (initdata == NULL) {
157  SCLogDebug("Error getting context for EveLogPgsql. \"initdata\" is NULL.");
158  goto error_exit;
159  }
160 
161  thread->pgsqllog_ctx = ((OutputCtx *)initdata)->data;
162  thread->ctx = CreateEveThreadCtx(t, thread->pgsqllog_ctx->eve_ctx);
163  if (!thread->ctx) {
164  goto error_exit;
165  }
166  *data = (void *)thread;
167 
168  return TM_ECODE_OK;
169 
170 error_exit:
171  SCFree(thread);
172  return TM_ECODE_FAILED;
173 }
174 
175 static TmEcode JsonPgsqlLogThreadDeinit(ThreadVars *t, void *data)
176 {
177  LogPgsqlLogThread *thread = (LogPgsqlLogThread *)data;
178  if (thread == NULL) {
179  return TM_ECODE_OK;
180  }
181  FreeEveThreadCtx(thread->ctx);
182  SCFree(thread);
183  return TM_ECODE_OK;
184 }
185 
187 {
188  /* PGSQL_START_REMOVE */
189  if (ConfGetNode("app-layer.protocols.pgsql") == NULL) {
190  SCLogDebug("Disabling Pgsql eve-logger");
191  return;
192  }
193  /* PGSQL_END_REMOVE */
194  /* Register as an eve sub-module. */
195  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonPgsqlLog", "eve-log.pgsql",
196  OutputPgsqlLogInitSub, ALPROTO_PGSQL, JsonPgsqlLogger, JsonPgsqlLogThreadInit,
197  JsonPgsqlLogThreadDeinit, NULL);
198 
199  SCLogDebug("PostgreSQL JSON logger registered.");
200 }
util-byte.h
tm-threads.h
PGSQL_LOG_PASSWORDS
#define PGSQL_LOG_PASSWORDS
Definition: output-json-pgsql.c:49
JsonPgsqlLogRegister
void JsonPgsqlLogRegister(void)
Definition: output-json-pgsql.c:186
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
threads.h
OutputJsonCtx_
Definition: output-json.h:79
Flow_
Flow data structure.
Definition: flow.h:351
output-json-pgsql.h
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:928
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
rust.h
LogPgsqlLogThread_::pgsqllog_ctx
OutputPgsqlCtx * pgsqllog_ctx
Definition: output-json-pgsql.c:58
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
util-unittest.h
JsonPgsqlAddMetadata
bool JsonPgsqlAddMetadata(void *vtx, JsonBuilder *jb)
Definition: output-json-pgsql.c:62
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:537
OutputPgsqlCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-pgsql.c:54
OutputCtx_::data
void * data
Definition: tm-modules.h:88
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:85
OutputJsonThreadCtx_
Definition: output-json.h:87
LogPgsqlLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-pgsql.c:59
util-debug.h
LogPgsqlLogThread_
Definition: output-json-pgsql.c:57
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:469
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:787
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
LogPgsqlLogThread
struct LogPgsqlLogThread_ LogPgsqlLogThread
app-layer-parser.h
Packet_
Definition: decode.h:437
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:83
OutputPgsqlCtx_
Definition: output-json-pgsql.c:52
OutputPgsqlCtx_::flags
uint32_t flags
Definition: output-json-pgsql.c:53
OutputInitResult_
Definition: output.h:46
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:91
ALPROTO_PGSQL
@ ALPROTO_PGSQL
Definition: app-layer-protos.h:57
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:404
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:38
PGSQL_DEFAULTS
#define PGSQL_DEFAULTS
Definition: output-json-pgsql.c:50
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:467
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
OutputPgsqlCtx
struct OutputPgsqlCtx_ OutputPgsqlCtx
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
output.h
app-layer.h
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:814