suricata
util-file.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2011 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  */
24 
25 #ifndef __UTIL_FILE_H__
26 #define __UTIL_FILE_H__
27 
28 #include "conf.h"
29 
30 #include "util-streaming-buffer.h"
31 
32 /* Hack: Pulling rust.h to get the SCSha256 causes all sorts of problems with
33  * header include orders, which is something we'll have to resolve as we provide
34  * more functionality via Rust. But this lets me continue with replacing nss
35  * without fighting the headers at this time. */
36 typedef struct SCSha256 SCSha256;
37 #define SC_SHA256_LEN 32
38 
39 typedef struct SCSha1 SCSha1;
40 #define SC_SHA1_LEN 20
41 
42 typedef struct SCMd5 SCMd5;
43 #define SC_MD5_LEN 16
44 
45 #define FILE_TRUNCATED BIT_U16(0)
46 #define FILE_NOMAGIC BIT_U16(1)
47 #define FILE_NOMD5 BIT_U16(2)
48 #define FILE_MD5 BIT_U16(3)
49 #define FILE_NOSHA1 BIT_U16(4)
50 #define FILE_SHA1 BIT_U16(5)
51 #define FILE_NOSHA256 BIT_U16(6)
52 #define FILE_SHA256 BIT_U16(7)
53 #define FILE_LOGGED BIT_U16(8)
54 #define FILE_NOSTORE BIT_U16(9)
55 #define FILE_STORE BIT_U16(10)
56 #define FILE_STORED BIT_U16(11)
57 #define FILE_NOTRACK BIT_U16(12) /**< track size of file */
58 #define FILE_USE_DETECT BIT_U16(13) /**< use content_inspected tracker */
59 #define FILE_HAS_GAPS BIT_U16(15)
60 
61 typedef enum FileState_ {
62  FILE_STATE_NONE = 0, /**< no state */
63  FILE_STATE_OPENED, /**< flow file is opened */
64  FILE_STATE_CLOSED, /**< flow file is completed,
65  there will be no more data. */
66  FILE_STATE_TRUNCATED, /**< flow file is not complete, but
67  there will be no more data. */
68  FILE_STATE_ERROR, /**< file is in an error state */
71 
72 typedef struct File_ {
73  uint16_t flags;
74  uint16_t name_len;
77  uint64_t txid; /**< tx this file is part of */
78  uint32_t file_track_id; /**< id used by protocol parser */
79  uint32_t file_store_id; /**< id used in store file name file.<id> */
80  int fd; /**< file descriptor for filestore, not
81  open if equal to -1 */
82  uint8_t *name;
83 #ifdef HAVE_MAGIC
84  char *magic;
85 #endif
86  struct File_ *next;
88  uint8_t md5[SC_MD5_LEN];
90  uint8_t sha1[SC_SHA1_LEN];
93  uint64_t content_inspected; /**< used in pruning if FILE_USE_DETECT
94  * flag is set */
95  uint64_t content_stored;
96  uint64_t size;
97  uint32_t inspect_window;
98  uint32_t inspect_min_size;
99  uint64_t start;
100  uint64_t end;
101 
102  uint32_t *sid; /* signature id of a rule that triggered the filestore event */
103  uint32_t sid_cnt;
104  uint32_t sid_max;
106 
107 typedef struct FileContainer_ {
111 
114 
116 
118 
119 /**
120  * \brief Open a new File
121  *
122  * \param ffc flow container
123  * \param sbcfg buffer config
124  * \param name filename character array
125  * \param name_len filename len
126  * \param data initial data
127  * \param data_len initial data len
128  * \param flags open flags
129  *
130  * \retval ff flowfile object
131  *
132  * \note filename is not a string, so it's not nul terminated.
133  *
134  * If flags contains the FILE_USE_DETECT bit, the pruning code will
135  * consider not just the content_stored tracker, but also content_inspected.
136  * It's the responsibility of the API user to make sure this tracker is
137  * properly updated.
138  */
140  uint32_t track_id, const uint8_t *name, uint16_t name_len,
141  const uint8_t *data, uint32_t data_len, uint16_t flags);
142 
143 /**
144  * \brief Close a File
145  *
146  * \param ffc the container
147  * \param data final data if any
148  * \param data_len data len if any
149  * \param flags flags
150  *
151  * \retval 0 ok
152  * \retval -1 error
153  */
154 int FileCloseFile(FileContainer *, const uint8_t *data, uint32_t data_len,
155  uint16_t flags);
156 int FileCloseFileById(FileContainer *, uint32_t track_id,
157  const uint8_t *data, uint32_t data_len, uint16_t flags);
158 int FileCloseFilePtr(File *ff, const uint8_t *data,
159  uint32_t data_len, uint16_t flags);
160 
161 /**
162  * \brief Store a chunk of file data in the flow. The open "flowfile"
163  * will be used.
164  *
165  * \param ffc the container
166  * \param data data chunk
167  * \param data_len data chunk len
168  *
169  * \retval 0 ok
170  * \retval -1 error
171  */
172 int FileAppendData(FileContainer *, const uint8_t *data, uint32_t data_len);
173 int FileAppendDataById(FileContainer *, uint32_t track_id,
174  const uint8_t *data, uint32_t data_len);
175 int FileAppendGAPById(FileContainer *ffc, uint32_t track_id,
176  const uint8_t *data, uint32_t data_len);
177 
178 void FileSetInspectSizes(File *file, const uint32_t win, const uint32_t min);
179 
180 /**
181  * \brief Sets the offset range for a file.
182  *
183  * \param ffc the container
184  * \param start start offset
185  * \param end end offset
186  *
187  * \retval 0 ok
188  * \retval -1 error
189  */
190 int FileSetRange(FileContainer *, uint64_t start, uint64_t end);
191 
192 /**
193  * \brief Tag a file for storing
194  *
195  * \param ff The file to store
196  */
197 int FileStore(File *);
198 
199 /**
200  * \brief Set the TX id for a file
201  *
202  * \param ff The file to store
203  * \param txid the tx id
204  */
205 int FileSetTx(File *, uint64_t txid);
206 void FileContainerSetTx(FileContainer *ffc, uint64_t tx_id);
207 
208 /**
209  * \brief disable file storing for a transaction
210  *
211  * \param f flow
212  * \param tx_id transaction id
213  */
214 void FileDisableStoringForTransaction(Flow *f, uint8_t direction, uint64_t tx_id);
215 
216 void FlowFileDisableStoringForTransaction(struct Flow_ *f, uint64_t tx_id);
217 void FilePrune(FileContainer *ffc);
218 
219 void FileForceFilestoreEnable(void);
220 int FileForceFilestore(void);
221 void FileReassemblyDepthEnable(uint32_t size);
222 uint32_t FileReassemblyDepth(void);
223 
224 void FileForceMagicEnable(void);
225 int FileForceMagic(void);
226 
227 void FileForceMd5Enable(void);
228 int FileForceMd5(void);
229 
230 void FileForceSha1Enable(void);
231 int FileForceSha1(void);
232 
233 void FileForceSha256Enable(void);
234 int FileForceSha256(void);
235 
236 void FileUpdateFlowFileFlags(Flow *f, uint16_t set_file_flags, uint8_t direction);
237 
239 
240 void FileForceTrackingEnable(void);
241 
243 void FileStoreAllFilesForTx(FileContainer *, uint64_t);
244 void FileStoreFileById(FileContainer *fc, uint32_t);
245 
247 
248 uint64_t FileDataSize(const File *file);
249 uint64_t FileTrackedSize(const File *file);
250 
251 uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction);
252 
253 #endif /* __UTIL_FILE_H__ */
FileFlowToFlags
uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction)
Definition: util-file.c:234
SCSha1
struct SCSha1 SCSha1
Definition: util-file.h:39
FileForceMagicEnable
void FileForceMagicEnable(void)
Definition: util-file.c:99
FileContainer_
Definition: util-file.h:107
FileCloseFile
int FileCloseFile(FileContainer *, const uint8_t *data, uint32_t data_len, uint16_t flags)
Close a File.
Definition: util-file.c:1008
FileState_
FileState_
Definition: util-file.h:61
FileReassemblyDepth
uint32_t FileReassemblyDepth(void)
Definition: util-file.c:134
File_::inspect_min_size
uint32_t inspect_min_size
Definition: util-file.h:98
File_::size
uint64_t size
Definition: util-file.h:96
FileCloseFileById
int FileCloseFileById(FileContainer *, uint32_t track_id, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:1024
SC_SHA256_LEN
#define SC_SHA256_LEN
Definition: util-file.h:37
FileDisableStoringForTransaction
void FileDisableStoringForTransaction(Flow *f, uint8_t direction, uint64_t tx_id)
disable file storing for a transaction
Definition: util-file.c:1146
FileOpenFileWithId
int FileOpenFileWithId(FileContainer *, const StreamingBufferConfig *, uint32_t track_id, const uint8_t *name, uint16_t name_len, const uint8_t *data, uint32_t data_len, uint16_t flags)
Open a new File.
Definition: util-file.c:920
FILE_STATE_OPENED
@ FILE_STATE_OPENED
Definition: util-file.h:63
Flow_
Flow data structure.
Definition: flow.h:347
File_::state
FileState state
Definition: util-file.h:75
FileStore
int FileStore(File *)
Tag a file for storing.
Definition: util-file.c:555
SC_SHA1_LEN
#define SC_SHA1_LEN
Definition: util-file.h:40
File_::file_store_id
uint32_t file_store_id
Definition: util-file.h:79
FileTruncateAllOpenFiles
void FileTruncateAllOpenFiles(FileContainer *)
Definition: util-file.c:1220
FileForceSha1Enable
void FileForceSha1Enable(void)
Definition: util-file.c:111
FileAppendData
int FileAppendData(FileContainer *, const uint8_t *data, uint32_t data_len)
Store a chunk of file data in the flow. The open "flowfile" will be used.
Definition: util-file.c:720
FileReassemblyDepthEnable
void FileReassemblyDepthEnable(uint32_t size)
Definition: util-file.c:128
FileContainer_::tail
File * tail
Definition: util-file.h:109
FileContainer
struct FileContainer_ FileContainer
FILE_STATE_TRUNCATED
@ FILE_STATE_TRUNCATED
Definition: util-file.h:66
FileForceMagic
int FileForceMagic(void)
Definition: util-file.c:142
File_::sha1
uint8_t sha1[SC_SHA1_LEN]
Definition: util-file.h:90
SCSha256
struct SCSha256 SCSha256
Definition: util-file.h:36
FileForceTrackingEnable
void FileForceTrackingEnable(void)
Definition: util-file.c:162
File_::sb
StreamingBuffer * sb
Definition: util-file.h:76
File_::name_len
uint16_t name_len
Definition: util-file.h:74
FileForceSha1
int FileForceSha1(void)
Definition: util-file.c:152
File_::md5
uint8_t md5[SC_MD5_LEN]
Definition: util-file.h:88
File_::file_track_id
uint32_t file_track_id
Definition: util-file.h:78
File_::end
uint64_t end
Definition: util-file.h:100
File_::fd
int fd
Definition: util-file.h:80
FileTrackedSize
uint64_t FileTrackedSize(const File *file)
get the size of the file
Definition: util-file.c:311
FileContainerFree
void FileContainerFree(FileContainer *)
Free a FileContainer.
Definition: util-file.c:462
FileForceMd5Enable
void FileForceMd5Enable(void)
Definition: util-file.c:105
FileForceFilestore
int FileForceFilestore(void)
Definition: util-file.c:123
FileSetRange
int FileSetRange(FileContainer *, uint64_t start, uint64_t end)
Sets the offset range for a file.
Definition: util-file.c:812
FileContainerSetTx
void FileContainerSetTx(FileContainer *ffc, uint64_t tx_id)
Definition: util-file.c:575
File_::sid_max
uint32_t sid_max
Definition: util-file.h:104
FileContainer_::head
File * head
Definition: util-file.h:108
FileAppendDataById
int FileAppendDataById(FileContainer *, uint32_t track_id, const uint8_t *data, uint32_t data_len)
Store/handle a chunk of file data in the File structure The file with 'track_id' in the FileContainer...
Definition: util-file.c:744
FileAppendGAPById
int FileAppendGAPById(FileContainer *ffc, uint32_t track_id, const uint8_t *data, uint32_t data_len)
Store/handle a chunk of file data in the File structure The file with 'track_id' in the FileContainer...
Definition: util-file.c:775
FilePrune
void FilePrune(FileContainer *ffc)
Definition: util-file.c:391
File_::sha256_ctx
SCSha256 * sha256_ctx
Definition: util-file.h:91
conf.h
FILE_STATE_MAX
@ FILE_STATE_MAX
Definition: util-file.h:69
File_::name
uint8_t * name
Definition: util-file.h:82
FileSetInspectSizes
void FileSetInspectSizes(File *file, const uint32_t win, const uint32_t min)
Definition: util-file.c:796
File_::sid
uint32_t * sid
Definition: util-file.h:102
File_::sid_cnt
uint32_t sid_cnt
Definition: util-file.h:103
StreamingBuffer_
Definition: util-streaming-buffer.h:95
FileForceFilestoreEnable
void FileForceFilestoreEnable(void)
Definition: util-file.c:93
FileStoreAllFilesForTx
void FileStoreAllFilesForTx(FileContainer *, uint64_t)
Definition: util-file.c:1192
File_::flags
uint16_t flags
Definition: util-file.h:73
File_::content_inspected
uint64_t content_inspected
Definition: util-file.h:93
FILE_STATE_CLOSED
@ FILE_STATE_CLOSED
Definition: util-file.h:64
File_
Definition: util-file.h:72
File_::content_stored
uint64_t content_stored
Definition: util-file.h:95
flags
uint8_t flags
Definition: decode-gre.h:0
util-streaming-buffer.h
FileForceMd5
int FileForceMd5(void)
Definition: util-file.c:147
File_::next
struct File_ * next
Definition: util-file.h:86
FileStoreAllFiles
void FileStoreAllFiles(FileContainer *)
Definition: util-file.c:1207
FileContainerAdd
void FileContainerAdd(FileContainer *, File *)
Definition: util-file.c:540
File_::sha256
uint8_t sha256[SC_SHA256_LEN]
Definition: util-file.h:92
File_::start
uint64_t start
Definition: util-file.h:99
FlowFileDisableStoringForTransaction
void FlowFileDisableStoringForTransaction(struct Flow_ *f, uint64_t tx_id)
FileDataSize
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:294
StreamingBufferConfig_
Definition: util-streaming-buffer.h:67
FileForceSha256Enable
void FileForceSha256Enable(void)
Definition: util-file.c:117
FileCloseFilePtr
int FileCloseFilePtr(File *ff, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:932
FileSetTx
int FileSetTx(File *, uint64_t txid)
Set the TX id for a file.
Definition: util-file.c:567
ConfNode_
Definition: conf.h:32
File
struct File_ File
File_::md5_ctx
SCMd5 * md5_ctx
Definition: util-file.h:87
FileContainerRecycle
void FileContainerRecycle(FileContainer *)
Recycle a FileContainer.
Definition: util-file.c:443
FileForceSha256
int FileForceSha256(void)
Definition: util-file.c:157
File_::inspect_window
uint32_t inspect_window
Definition: util-file.h:97
FileForceHashParseCfg
void FileForceHashParseCfg(ConfNode *)
Function to parse forced file hashing configuration.
Definition: util-file.c:171
FileContainerAlloc
FileContainer * FileContainerAlloc(void)
allocate a FileContainer
Definition: util-file.c:426
FileState
enum FileState_ FileState
File_::txid
uint64_t txid
Definition: util-file.h:77
FileStoreFileById
void FileStoreFileById(FileContainer *fc, uint32_t)
flag a file with id "file_id" to be stored.
Definition: util-file.c:1177
SC_MD5_LEN
#define SC_MD5_LEN
Definition: util-file.h:43
FileUpdateFlowFileFlags
void FileUpdateFlowFileFlags(Flow *f, uint16_t set_file_flags, uint8_t direction)
set a flow's file flags
Definition: util-file.c:1049
FILE_STATE_ERROR
@ FILE_STATE_ERROR
Definition: util-file.h:68
SCMd5
struct SCMd5 SCMd5
Definition: util-file.h:42
FILE_STATE_NONE
@ FILE_STATE_NONE
Definition: util-file.h:62
File_::sha1_ctx
SCSha1 * sha1_ctx
Definition: util-file.h:89