summaryrefslogtreecommitdiffstats
path: root/src/output-filedata.c
blob: a9890769a7b3b25dcdb7142a70fa49fc36fdb707 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* Copyright (C) 2007-2022 Open Information Security Foundation
 *
 * You can copy, redistribute or modify this Program under the terms of
 * the GNU General Public License version 2 as published by the Free
 * Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/**
 * \file
 *
 * \author Victor Julien <victor@inliniac.net>
 *
 * AppLayer Filedata Logger Output registration functions
 */

#include "suricata-common.h"
#include "output.h"
#include "output-filedata.h"
#include "app-layer-parser.h"
#include "detect-filemagic.h"
#include "conf.h"
#include "util-profiling.h"
#include "util-validate.h"
#include "util-magic.h"
#include "util-path.h"

bool g_filedata_logger_enabled = false;

/* logger instance, a module + a output ctx,
 * it's perfectly valid that have multiple instances of the same
 * log module (e.g. http.log) with different output ctx'. */
typedef struct OutputFiledataLogger_ {
    FiledataLogger LogFunc;
    OutputCtx *output_ctx;
    struct OutputFiledataLogger_ *next;
    const char *name;
    LoggerId logger_id;
    ThreadInitFunc ThreadInit;
    ThreadDeinitFunc ThreadDeinit;
    ThreadExitPrintStatsFunc ThreadExitPrintStats;
} OutputFiledataLogger;

static OutputFiledataLogger *list = NULL;

int OutputRegisterFiledataLogger(LoggerId id, const char *name,
    FiledataLogger LogFunc, OutputCtx *output_ctx, ThreadInitFunc ThreadInit,
    ThreadDeinitFunc ThreadDeinit,
    ThreadExitPrintStatsFunc ThreadExitPrintStats)
{
    OutputFiledataLogger *op = SCMalloc(sizeof(*op));
    if (op == NULL)
        return -1;
    memset(op, 0x00, sizeof(*op));

    op->LogFunc = LogFunc;
    op->output_ctx = output_ctx;
    op->name = name;
    op->logger_id = id;
    op->ThreadInit = ThreadInit;
    op->ThreadDeinit = ThreadDeinit;
    op->ThreadExitPrintStats = ThreadExitPrintStats;

    if (list == NULL)
        list = op;
    else {
        OutputFiledataLogger *t = list;
        while (t->next)
            t = t->next;
        t->next = op;
    }

    SCLogDebug("OutputRegisterFiledataLogger happy");
    g_filedata_logger_enabled = true;
    return 0;
}

SC_ATOMIC_DECLARE(unsigned int, g_file_store_id);

static int CallLoggers(ThreadVars *tv, OutputLoggerThreadStore *store_list, Packet *p, File *ff,
        void *tx, const uint64_t tx_id, const uint8_t *data, uint32_t data_len, uint8_t flags,
        uint8_t dir)
{
    OutputFiledataLogger *logger = list;
    OutputLoggerThreadStore *store = store_list;
    int file_logged = 0;

    while (logger && store) {
        DEBUG_VALIDATE_BUG_ON(logger->LogFunc == NULL);

        SCLogDebug("logger %p", logger);
        PACKET_PROFILING_LOGGER_START(p, logger->logger_id);
        logger->LogFunc(tv, store->thread_data, (const Packet *)p, ff, tx, tx_id, data, data_len,
                flags, dir);
        PACKET_PROFILING_LOGGER_END(p, logger->logger_id);

        file_logged = 1;

        logger = logger->next;
        store = store->next;

        DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
        DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
    }
    return file_logged;
}

static void CloseFile(const Packet *p, Flow *f, File *file, void *txv)
{
    DEBUG_VALIDATE_BUG_ON((file->flags & FILE_STORED) != 0);

    AppLayerTxData *txd = AppLayerParserGetTxData(f->proto, f->alproto, txv);
    if (txd) {
        BUG_ON(f->alproto == ALPROTO_SMB && txd->files_logged != 0);
        txd->files_stored++;
    }
    file->flags |= FILE_STORED;
}

void OutputFiledataLogFfc(ThreadVars *tv, OutputFiledataLoggerThreadData *td, Packet *p,
        AppLayerGetFileState files, void *txv, const uint64_t tx_id, AppLayerTxData *txd,
        const uint8_t call_flags, const bool file_close, const bool file_trunc, const uint8_t dir)
{
    SCLogDebug("ffc %p", files.fc);

    OutputLoggerThreadStore *store = td->store;
    for (File *ff = files.fc->head; ff != NULL; ff = ff->next) {
        FileApplyTxFlags(txd, dir, ff);
        FilePrintFlags(ff);

        uint8_t file_flags = call_flags;
#ifdef HAVE_MAGIC
        if (FileForceMagic() && ff->magic == NULL) {
            FilemagicThreadLookup(&td->magic_ctx, ff);
        }
#endif
        if (ff->flags & FILE_STORED) {
            continue;
        }

        if (!(ff->flags & FILE_STORE)) {
            continue;
        }

        /* if file_store_id == 0, this is the first store of this file */
        if (ff->file_store_id == 0) {
            /* new file */
            ff->file_store_id = SC_ATOMIC_ADD(g_file_store_id, 1);
            file_flags |= OUTPUT_FILEDATA_FLAG_OPEN;
        }

        /* if we have no data chunks left to log, we should still
         * close the logger(s) */
        if (FileDataSize(ff) == ff->content_stored && (file_trunc || file_close)) {
            if (ff->state < FILE_STATE_CLOSED) {
                FileCloseFilePtr(ff, files.cfg, NULL, 0, FILE_TRUNCATED);
            }
            file_flags |= OUTPUT_FILEDATA_FLAG_CLOSE;
            CallLoggers(tv, store, p, ff, txv, tx_id, NULL, 0, file_flags, dir);
            CloseFile(p, p->flow, ff, txv);
            continue;
        }

        /* if file needs to be closed or truncated, inform
         * loggers */
        if ((file_close || file_trunc) && ff->state < FILE_STATE_CLOSED) {
            FileCloseFilePtr(ff, files.cfg, NULL, 0, FILE_TRUNCATED);
        }

        /* tell the logger we're closing up */
        if (ff->state >= FILE_STATE_CLOSED)
            file_flags |= OUTPUT_FILEDATA_FLAG_CLOSE;

        /* do the actual logging */
        const uint8_t *data = NULL;
        uint32_t data_len = 0;

        StreamingBufferGetDataAtOffset(ff->sb, &data, &data_len, ff->content_stored);

        const int file_logged =
                CallLoggers(tv, store, p, ff, txv, tx_id, data, data_len, file_flags, dir);
        if (file_logged) {
            ff->content_stored += data_len;

            /* all done */
            if (file_flags & OUTPUT_FILEDATA_FLAG_CLOSE) {
                CloseFile(p, p->flow, ff, txv);
            }
        }
    }
}

/** \brief thread init for the filedata logger
 *  This will run the thread init functions for the individual registered
 *  loggers */
TmEcode OutputFiledataLogThreadInit(ThreadVars *tv, OutputFiledataLoggerThreadData **data)
{
    OutputFiledataLoggerThreadData *td = SCMalloc(sizeof(*td));
    if (td == NULL)
        return TM_ECODE_FAILED;
    memset(td, 0x00, sizeof(*td));
    *data = td;

#ifdef HAVE_MAGIC
    td->magic_ctx = MagicInitContext();
    if (td->magic_ctx == NULL) {
        SCFree(td);
        return TM_ECODE_FAILED;
    }
#endif

    SCLogDebug("OutputFiledataLogThreadInit happy (*data %p)", *data);

    OutputFiledataLogger *logger = list;
    while (logger) {
        if (logger->ThreadInit) {
            void *retptr = NULL;
            if (logger->ThreadInit(tv, (void *)logger->output_ctx, &retptr) == TM_ECODE_OK) {
                OutputLoggerThreadStore *ts = SCMalloc(sizeof(*ts));
                /* todo */ BUG_ON(ts == NULL);
                memset(ts, 0x00, sizeof(*ts));

                /* store thread handle */
                ts->thread_data = retptr;

                if (td->store == NULL) {
                    td->store = ts;
                } else {
                    OutputLoggerThreadStore *tmp = td->store;
                    while (tmp->next != NULL)
                        tmp = tmp->next;
                    tmp->next = ts;
                }

                SCLogDebug("%s is now set up", logger->name);
            }
        }

        logger = logger->next;
    }
    return TM_ECODE_OK;
}

TmEcode OutputFiledataLogThreadDeinit(
        ThreadVars *tv, OutputFiledataLoggerThreadData *op_thread_data)
{
    OutputLoggerThreadStore *store = op_thread_data->store;
    OutputFiledataLogger *logger = list;

    while (logger && store) {
        if (logger->ThreadDeinit) {
            logger->ThreadDeinit(tv, store->thread_data);
        }

        OutputLoggerThreadStore *next_store = store->next;
        SCFree(store);
        store = next_store;
        logger = logger->next;
    }

#ifdef HAVE_MAGIC
    MagicDeinitContext(op_thread_data->magic_ctx);
#endif

    SCFree(op_thread_data);
    return TM_ECODE_OK;
}

void OutputFiledataLoggerRegister(void)
{
    SC_ATOMIC_INIT(g_file_store_id);
    SC_ATOMIC_SET(g_file_store_id, 1);
}

void OutputFiledataShutdown(void)
{
    OutputFiledataLogger *logger = list;
    while (logger) {
        OutputFiledataLogger *next_logger = logger->next;
        SCFree(logger);
        logger = next_logger;
    }

    list = NULL;
}