suricata
output-json-krb5.c
Go to the documentation of this file.
1 /* Copyright (C) 2018-2020 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 Pierre Chifflier <chifflier@wzdftpd.net>
22  *
23  * Implement JSON/eve logging app-layer KRB5.
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 "app-layer-krb5.h"
48 #include "output-json-krb5.h"
49 
50 #include "rust.h"
51 
52 typedef struct LogKRB5FileCtx_ {
56 
57 typedef struct LogKRB5LogThread_ {
62 
63 static int JsonKRB5Logger(ThreadVars *tv, void *thread_data,
64  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
65 {
66  KRB5Transaction *krb5tx = tx;
67  LogKRB5LogThread *thread = thread_data;
68 
69  JsonBuilder *jb = CreateEveHeader(p, LOG_DIR_PACKET, "krb5", NULL);
70  if (unlikely(jb == NULL)) {
71  return TM_ECODE_FAILED;
72  }
73 
74  EveAddCommonOptions(&thread->krb5log_ctx->cfg, p, f, jb);
75 
76  jb_open_object(jb, "krb5");
77  if (!rs_krb5_log_json_response(jb, state, krb5tx)) {
78  goto error;
79  }
80  jb_close(jb);
81 
82  MemBufferReset(thread->buffer);
83  OutputJsonBuilderBuffer(jb, thread->file_ctx, &thread->buffer);
84 
85  jb_free(jb);
86  return TM_ECODE_OK;
87 
88 error:
89  jb_free(jb);
90  return TM_ECODE_FAILED;
91 }
92 
93 static void OutputKRB5LogDeInitCtxSub(OutputCtx *output_ctx)
94 {
95  LogKRB5FileCtx *krb5log_ctx = (LogKRB5FileCtx *)output_ctx->data;
96  SCFree(krb5log_ctx);
97  SCFree(output_ctx);
98 }
99 
100 static OutputInitResult OutputKRB5LogInitSub(ConfNode *conf,
101  OutputCtx *parent_ctx)
102 {
103  OutputInitResult result = { NULL, false };
104  OutputJsonCtx *ajt = parent_ctx->data;
105 
106  LogKRB5FileCtx *krb5log_ctx = SCCalloc(1, sizeof(*krb5log_ctx));
107  if (unlikely(krb5log_ctx == NULL)) {
108  return result;
109  }
110  krb5log_ctx->file_ctx = ajt->file_ctx;
111  krb5log_ctx->cfg = ajt->cfg;
112 
113  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
114  if (unlikely(output_ctx == NULL)) {
115  SCFree(krb5log_ctx);
116  return result;
117  }
118  output_ctx->data = krb5log_ctx;
119  output_ctx->DeInit = OutputKRB5LogDeInitCtxSub;
120 
121  SCLogDebug("KRB5 log sub-module initialized.");
122 
125 
126  result.ctx = output_ctx;
127  result.ok = true;
128  return result;
129 }
130 
131 static TmEcode JsonKRB5LogThreadInit(ThreadVars *t, const void *initdata, void **data)
132 {
133  LogKRB5LogThread *thread = SCCalloc(1, sizeof(*thread));
134  if (unlikely(thread == NULL)) {
135  return TM_ECODE_FAILED;
136  }
137 
138  if (initdata == NULL) {
139  SCLogDebug("Error getting context for EveLogKRB5. \"initdata\" is NULL.");
140  goto error_exit;
141  }
142 
144  if (unlikely(thread->buffer == NULL)) {
145  goto error_exit;
146  }
147 
148  thread->krb5log_ctx = ((OutputCtx *)initdata)->data;
149  thread->file_ctx = LogFileEnsureExists(thread->krb5log_ctx->file_ctx, t->id);
150  if (!thread->file_ctx) {
151  goto error_exit;
152  }
153  *data = (void *)thread;
154 
155  return TM_ECODE_OK;
156 
157 error_exit:
158  if (thread->buffer != NULL) {
159  MemBufferFree(thread->buffer);
160  }
161  SCFree(thread);
162  return TM_ECODE_FAILED;
163 }
164 
165 static TmEcode JsonKRB5LogThreadDeinit(ThreadVars *t, void *data)
166 {
167  LogKRB5LogThread *thread = (LogKRB5LogThread *)data;
168  if (thread == NULL) {
169  return TM_ECODE_OK;
170  }
171  if (thread->buffer != NULL) {
172  MemBufferFree(thread->buffer);
173  }
174  SCFree(thread);
175  return TM_ECODE_OK;
176 }
177 
179 {
180  /* Register as an eve sub-module. */
181  OutputRegisterTxSubModule(LOGGER_JSON_KRB5, "eve-log", "JsonKRB5Log",
182  "eve-log.krb5", OutputKRB5LogInitSub, ALPROTO_KRB5,
183  JsonKRB5Logger, JsonKRB5LogThreadInit,
184  JsonKRB5LogThreadDeinit, NULL);
185 
186  SCLogDebug("KRB5 JSON logger registered.");
187 }
util-byte.h
tm-threads.h
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:104
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
LogKRB5LogThread_::buffer
MemBuffer * buffer
Definition: output-json-krb5.c:60
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:63
threads.h
OutputJsonCtx_
Definition: output-json.h:101
Flow_
Flow data structure.
Definition: flow.h:347
OutputJsonCommonSettings_
Definition: output-json.h:91
LogFileCtx_
Definition: util-logopenfile.h:60
output-json-krb5.h
LogKRB5LogThread_::krb5log_ctx
LogKRB5FileCtx * krb5log_ctx
Definition: output-json-krb5.c:59
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:978
rust.h
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:444
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
LogKRB5FileCtx
struct LogKRB5FileCtx_ LogKRB5FileCtx
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
ALPROTO_KRB5
@ ALPROTO_KRB5
Definition: app-layer-protos.h:50
OutputCtx_
Definition: tm-modules.h:78
LogKRB5FileCtx_
Definition: output-json-krb5.c:52
app-layer-krb5.h
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:846
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:456
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(LogFileCtx *parent_ctx, int thread_id)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:659
OutputInitResult_::ok
bool ok
Definition: output.h:45
app-layer-parser.h
ThreadVars_::id
int id
Definition: threadvars.h:87
Packet_
Definition: decode.h:414
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
MemBuffer_
Definition: util-buffer.h:27
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
LogKRB5FileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-krb5.c:53
OutputInitResult_
Definition: output.h:43
LogKRB5LogThread
struct LogKRB5LogThread_ LogKRB5LogThread
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:39
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
KRB5Transaction
struct KRB5Transaction_ KRB5Transaction
Definition: app-layer-krb5.h:32
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
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:409
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:102
LogKRB5LogThread_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-krb5.c:58
LogKRB5FileCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-krb5.c:54
LogKRB5LogThread_
Definition: output-json-krb5.c:57
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
LOGGER_JSON_KRB5
@ LOGGER_JSON_KRB5
Definition: suricata-common.h:461
JsonKRB5LogRegister
void JsonKRB5LogRegister(void)
Definition: output-json-krb5.c:178
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h