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 #ifdef HAVE_NSS
29 #include <sechash.h>
30 #endif
31 
32 #include "conf.h"
33 
34 #include "util-streaming-buffer.h"
35 
36 #define FILE_TRUNCATED BIT_U16(0)
37 #define FILE_NOMAGIC BIT_U16(1)
38 #define FILE_NOMD5 BIT_U16(2)
39 #define FILE_MD5 BIT_U16(3)
40 #define FILE_NOSHA1 BIT_U16(4)
41 #define FILE_SHA1 BIT_U16(5)
42 #define FILE_NOSHA256 BIT_U16(6)
43 #define FILE_SHA256 BIT_U16(7)
44 #define FILE_LOGGED BIT_U16(8)
45 #define FILE_NOSTORE BIT_U16(9)
46 #define FILE_STORE BIT_U16(10)
47 #define FILE_STORED BIT_U16(11)
48 #define FILE_NOTRACK BIT_U16(12) /**< track size of file */
49 #define FILE_USE_DETECT BIT_U16(13) /**< use content_inspected tracker */
50 #define FILE_USE_TRACKID BIT_U16(14) /**< File::file_track_id field is in use */
51 #define FILE_HAS_GAPS BIT_U16(15)
52 
53 typedef enum FileState_ {
54  FILE_STATE_NONE = 0, /**< no state */
55  FILE_STATE_OPENED, /**< flow file is opened */
56  FILE_STATE_CLOSED, /**< flow file is completed,
57  there will be no more data. */
58  FILE_STATE_TRUNCATED, /**< flow file is not complete, but
59  there will be no more data. */
60  FILE_STATE_ERROR, /**< file is in an error state */
62 } FileState;
63 
64 typedef struct File_ {
65  uint16_t flags;
66  uint16_t name_len;
69  uint64_t txid; /**< tx this file is part of */
70  uint32_t file_track_id; /**< id used by protocol parser. Optional
71  * only used if FILE_USE_TRACKID flag set */
72  uint32_t file_store_id; /**< id used in store file name file.<id> */
73  int fd; /**< file descriptor for filestore, not
74  open if equal to -1 */
75  uint8_t *name;
76 #ifdef HAVE_MAGIC
77  char *magic;
78 #endif
79  struct File_ *next;
80 #ifdef HAVE_NSS
81  HASHContext *md5_ctx;
82  uint8_t md5[MD5_LENGTH];
83  HASHContext *sha1_ctx;
84  uint8_t sha1[SHA1_LENGTH];
85  HASHContext *sha256_ctx;
86  uint8_t sha256[SHA256_LENGTH];
87 #endif
88  uint64_t content_inspected; /**< used in pruning if FILE_USE_DETECT
89  * flag is set */
90  uint64_t content_stored;
91  uint64_t size;
92  uint64_t start;
93  uint64_t end;
94 
95  uint32_t *sid; /* signature id of a rule that triggered the filestore event */
96  uint32_t sid_cnt;
97  uint32_t sid_max;
98 } File;
99 
100 typedef struct FileContainer_ {
103 } FileContainer;
104 
107 
109 
111 
112 /**
113  * \brief Open a new File
114  *
115  * \param ffc flow container
116  * \param sbcfg buffer config
117  * \param name filename character array
118  * \param name_len filename len
119  * \param data initial data
120  * \param data_len initial data len
121  * \param flags open flags
122  *
123  * \retval ff flowfile object
124  *
125  * \note filename is not a string, so it's not nul terminated.
126  *
127  * If flags contains the FILE_USE_DETECT bit, the pruning code will
128  * consider not just the content_stored tracker, but also content_inspected.
129  * It's the responsibility of the API user to make sure this tracker is
130  * properly updated.
131  */
133  uint32_t track_id, const uint8_t *name, uint16_t name_len,
134  const uint8_t *data, uint32_t data_len, uint16_t flags);
135 
136 /**
137  * \brief Close a File
138  *
139  * \param ffc the container
140  * \param data final data if any
141  * \param data_len data len if any
142  * \param flags flags
143  *
144  * \retval 0 ok
145  * \retval -1 error
146  */
147 int FileCloseFile(FileContainer *, const uint8_t *data, uint32_t data_len,
148  uint16_t flags);
149 int FileCloseFileById(FileContainer *, uint32_t track_id,
150  const uint8_t *data, uint32_t data_len, uint16_t flags);
151 int FileCloseFilePtr(File *ff, const uint8_t *data,
152  uint32_t data_len, uint16_t flags);
153 
154 /**
155  * \brief Store a chunk of file data in the flow. The open "flowfile"
156  * will be used.
157  *
158  * \param ffc the container
159  * \param data data chunk
160  * \param data_len data chunk len
161  *
162  * \retval 0 ok
163  * \retval -1 error
164  */
165 int FileAppendData(FileContainer *, const uint8_t *data, uint32_t data_len);
166 int FileAppendDataById(FileContainer *, uint32_t track_id,
167  const uint8_t *data, uint32_t data_len);
168 int FileAppendGAPById(FileContainer *ffc, uint32_t track_id,
169  const uint8_t *data, uint32_t data_len);
170 
171 /**
172  * \brief Sets the offset range for a file.
173  *
174  * \param ffc the container
175  * \param start start offset
176  * \param end end offset
177  *
178  * \retval 0 ok
179  * \retval -1 error
180  */
181 int FileSetRange(FileContainer *, uint64_t start, uint64_t end);
182 
183 /**
184  * \brief Tag a file for storing
185  *
186  * \param ff The file to store
187  */
188 int FileStore(File *);
189 
190 /**
191  * \brief Set the TX id for a file
192  *
193  * \param ff The file to store
194  * \param txid the tx id
195  */
196 int FileSetTx(File *, uint64_t txid);
197 void FileContainerSetTx(FileContainer *ffc, uint64_t tx_id);
198 
199 /**
200  * \brief disable file storage for a flow
201  *
202  * \param f *LOCKED* flow
203  */
204 void FileDisableStoring(struct Flow_ *, uint8_t);
205 
206 void FileDisableFilesize(Flow *f, uint8_t direction);
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 FileDisableMagic(Flow *f, uint8_t);
225 void FileForceMagicEnable(void);
226 int FileForceMagic(void);
227 
228 void FileDisableMd5(Flow *f, uint8_t);
229 void FileForceMd5Enable(void);
230 int FileForceMd5(void);
231 
232 void FileDisableSha1(Flow *f, uint8_t);
233 void FileForceSha1Enable(void);
234 int FileForceSha1(void);
235 
236 void FileDisableSha256(Flow *f, uint8_t);
237 void FileForceSha256Enable(void);
238 int FileForceSha256(void);
239 
241 
242 void FileForceTrackingEnable(void);
243 
245 void FileStoreAllFilesForTx(FileContainer *, uint64_t);
246 void FileStoreFileById(FileContainer *fc, uint32_t);
247 
249 
250 uint64_t FileDataSize(const File *file);
251 uint64_t FileTrackedSize(const File *file);
252 
253 uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction);
254 
255 #endif /* __UTIL_FILE_H__ */
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:868
int FileForceMagic(void)
Definition: util-file.c:130
void FileContainerAdd(FileContainer *, File *)
Definition: util-file.c:496
void FileForceSha256Enable(void)
Definition: util-file.c:106
void FileForceTrackingEnable(void)
Definition: util-file.c:150
void FileStoreAllFiles(FileContainer *)
Definition: util-file.c:1303
uint64_t size
Definition: util-file.h:91
void FilePrune(FileContainer *ffc)
Definition: util-file.c:345
uint32_t sid_cnt
Definition: util-file.h:96
uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction)
Definition: util-file.c:217
int FileForceSha1(void)
Definition: util-file.c:140
int FileForceFilestore(void)
Definition: util-file.c:111
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 &#39;track_id&#39; in the FileContainer...
Definition: util-file.c:718
void FileDisableSha256(Flow *f, uint8_t)
disable file sha256 calc for this flow
Definition: util-file.c:1145
void FileTruncateAllOpenFiles(FileContainer *)
Definition: util-file.c:1316
uint64_t content_inspected
Definition: util-file.h:88
int fd
Definition: util-file.h:73
FileState_
Definition: util-file.h:53
struct File_ * next
Definition: util-file.h:79
void FileContainerFree(FileContainer *)
Free a FileContainer.
Definition: util-file.c:416
void FileForceFilestoreEnable(void)
Definition: util-file.c:86
struct File_ File
int FileSetRange(FileContainer *, uint64_t start, uint64_t end)
Sets the offset range for a file.
Definition: util-file.c:751
struct FileContainer_ FileContainer
StreamingBuffer * sb
Definition: util-file.h:68
FileContainer * FileContainerAlloc(void)
allocate a FileContainer
Definition: util-file.c:380
uint64_t start
Definition: util-file.h:92
uint16_t flags
Definition: util-file.h:65
void FileContainerRecycle(FileContainer *)
Recycle a FileContainer.
Definition: util-file.c:397
uint64_t end
Definition: util-file.h:93
uint32_t * sid
Definition: util-file.h:95
void FileForceMd5Enable(void)
Definition: util-file.c:96
uint32_t sid_max
Definition: util-file.h:97
uint64_t txid
Definition: util-file.h:69
void FileStoreAllFilesForTx(FileContainer *, uint64_t)
Definition: util-file.c:1288
void FileForceHashParseCfg(ConfNode *)
Function to parse forced file hashing configuration.
Definition: util-file.c:158
uint16_t name_len
Definition: util-file.h:66
int FileStore(File *)
Tag a file for storing.
Definition: util-file.c:511
void FileDisableStoring(struct Flow_ *, uint8_t)
disable file storage for a flow
Definition: util-file.c:1004
int FileCloseFilePtr(File *ff, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:881
int FileCloseFileById(FileContainer *, uint32_t track_id, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:979
void FileDisableFilesize(Flow *f, uint8_t direction)
disable file size tracking for this flow
Definition: util-file.c:1184
uint64_t FileTrackedSize(const File *file)
get the size of the file
Definition: util-file.c:294
Definition: conf.h:32
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 &#39;track_id&#39; in the FileContainer...
Definition: util-file.c:687
void FlowFileDisableStoringForTransaction(struct Flow_ *f, uint64_t tx_id)
void FileDisableSha1(Flow *f, uint8_t)
disable file sha1 calc for this flow
Definition: util-file.c:1106
int FileForceSha256(void)
Definition: util-file.c:145
void FileForceMagicEnable(void)
Definition: util-file.c:91
void FileDisableMd5(Flow *f, uint8_t)
disable file md5 calc for this flow
Definition: util-file.c:1067
uint16_t tx_id
void FileForceSha1Enable(void)
Definition: util-file.c:101
uint32_t FileReassemblyDepth(void)
Definition: util-file.c:122
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:277
FileState state
Definition: util-file.h:67
uint32_t file_store_id
Definition: util-file.h:72
uint64_t content_stored
Definition: util-file.h:90
uint32_t file_track_id
Definition: util-file.h:70
void FileDisableStoringForTransaction(Flow *f, uint8_t direction, uint64_t tx_id)
disable file storing for a transaction
Definition: util-file.c:1242
void FileStoreFileById(FileContainer *fc, uint32_t)
flag a file with id "file_id" to be stored.
Definition: util-file.c:1273
void FileContainerSetTx(FileContainer *ffc, uint64_t tx_id)
Definition: util-file.c:531
void FileDisableMagic(Flow *f, uint8_t)
disable file magic lookups for this flow
Definition: util-file.c:1036
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:663
void FileReassemblyDepthEnable(uint32_t size)
Definition: util-file.c:116
int FileForceMd5(void)
Definition: util-file.c:135
int FileCloseFile(FileContainer *, const uint8_t *data, uint32_t data_len, uint16_t flags)
Close a File.
Definition: util-file.c:963
enum FileState_ FileState
#define SHA1_LENGTH
Definition: util-crypt.h:45
Flow data structure.
Definition: flow.h:325
uint8_t * name
Definition: util-file.h:75
int FileSetTx(File *, uint64_t txid)
Set the TX id for a file.
Definition: util-file.c:523