suricata
util-file.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 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  * \author Pablo Rincon <pablo.rincon.crespo@gmail.com>
23  *
24  */
25 
26 #include "suricata-common.h"
27 #include "suricata.h"
28 #include "debug.h"
29 #include "flow.h"
30 #include "stream.h"
31 #include "stream-tcp.h"
32 #include "runmodes.h"
33 #include "util-hash.h"
34 #include "util-debug.h"
35 #include "util-memcmp.h"
36 #include "util-print.h"
37 #include "app-layer-parser.h"
38 #include "util-validate.h"
39 
40 /** \brief switch to force filestore on all files
41  * regardless of the rules.
42  */
43 static int g_file_force_filestore = 0;
44 
45 /** \brief switch to force magic checks on all files
46  * regardless of the rules.
47  */
48 static int g_file_force_magic = 0;
49 
50 /** \brief switch to force md5 calculation on all files
51  * regardless of the rules.
52  */
53 static int g_file_force_md5 = 0;
54 
55 /** \brief switch to force sha1 calculation on all files
56  * regardless of the rules.
57  */
58 static int g_file_force_sha1 = 0;
59 
60 /** \brief switch to force sha256 calculation on all files
61  * regardless of the rules.
62  */
63 static int g_file_force_sha256 = 0;
64 
65 /** \brief switch to force tracking off all files
66  * regardless of the rules.
67  */
68 static int g_file_force_tracking = 0;
69 
70 /** \brief switch to use g_file_store_reassembly_depth
71  * to reassembly files
72  */
73 static int g_file_store_enable = 0;
74 
75 /** \brief stream_config.reassembly_depth equivalent
76  * for files
77  */
78 static uint32_t g_file_store_reassembly_depth = 0;
79 
80 /* prototypes */
81 static void FileFree(File *);
82 #ifdef HAVE_NSS
83 static void FileEndSha256(File *ff);
84 #endif
85 
87 {
88  g_file_force_filestore = 1;
89 }
90 
92 {
93  g_file_force_magic = 1;
94 }
95 
97 {
98  g_file_force_md5 = 1;
99 }
100 
102 {
103  g_file_force_sha1 = 1;
104 }
105 
107 {
108  g_file_force_sha256 = 1;
109 }
110 
112 {
113  return g_file_force_filestore;
114 }
115 
116 void FileReassemblyDepthEnable(uint32_t size)
117 {
118  g_file_store_enable = 1;
119  g_file_store_reassembly_depth = size;
120 }
121 
122 uint32_t FileReassemblyDepth(void)
123 {
124  if (g_file_store_enable == 1)
125  return g_file_store_reassembly_depth;
126  else
128 }
129 
130 int FileForceMagic(void)
131 {
132  return g_file_force_magic;
133 }
134 
135 int FileForceMd5(void)
136 {
137  return g_file_force_md5;
138 }
139 
140 int FileForceSha1(void)
141 {
142  return g_file_force_sha1;
143 }
144 
146 {
147  return g_file_force_sha256;
148 }
149 
151 {
152  g_file_force_tracking = 1;
153 }
154 
155 /**
156  * \brief Function to parse forced file hashing configuration.
157  */
159 {
160  BUG_ON(conf == NULL);
161 
162  ConfNode *forcehash_node = NULL;
163 
164  /* legacy option */
165  const char *force_md5 = ConfNodeLookupChildValue(conf, "force-md5");
166  if (force_md5 != NULL) {
167  SCLogWarning(SC_ERR_DEPRECATED_CONF, "deprecated 'force-md5' option "
168  "found. Please use 'force-hash: [md5]' instead");
169 
170  if (ConfValIsTrue(force_md5)) {
171 #ifdef HAVE_NSS
173  SCLogInfo("forcing md5 calculation for logged files");
174 #else
175  SCLogInfo("md5 calculation requires linking against libnss");
176 #endif
177  }
178  }
179 
180  if (conf != NULL)
181  forcehash_node = ConfNodeLookupChild(conf, "force-hash");
182 
183  if (forcehash_node != NULL) {
184  ConfNode *field = NULL;
185 
186  TAILQ_FOREACH(field, &forcehash_node->head, next) {
187  if (strcasecmp("md5", field->val) == 0) {
188 #ifdef HAVE_NSS
190  SCLogConfig("forcing md5 calculation for logged or stored files");
191 #else
192  SCLogInfo("md5 calculation requires linking against libnss");
193 #endif
194  }
195 
196  if (strcasecmp("sha1", field->val) == 0) {
197 #ifdef HAVE_NSS
199  SCLogConfig("forcing sha1 calculation for logged or stored files");
200 #else
201  SCLogInfo("sha1 calculation requires linking against libnss");
202 #endif
203  }
204 
205  if (strcasecmp("sha256", field->val) == 0) {
206 #ifdef HAVE_NSS
208  SCLogConfig("forcing sha256 calculation for logged or stored files");
209 #else
210  SCLogInfo("sha256 calculation requires linking against libnss");
211 #endif
212  }
213  }
214  }
215 }
216 
217 uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction)
218 {
219  uint16_t flags = 0;
220 
221  if (direction == STREAM_TOSERVER) {
222  if (flow->file_flags & FLOWFILE_NO_STORE_TS) {
223  flags |= FILE_NOSTORE;
224  }
225 
226  if (flow->file_flags & FLOWFILE_NO_MAGIC_TS) {
227  flags |= FILE_NOMAGIC;
228  }
229 
230  if (flow->file_flags & FLOWFILE_NO_MD5_TS) {
231  flags |= FILE_NOMD5;
232  }
233 
234  if (flow->file_flags & FLOWFILE_NO_SHA1_TS) {
235  flags |= FILE_NOSHA1;
236  }
237 
238  if (flow->file_flags & FLOWFILE_NO_SHA256_TS) {
239  flags |= FILE_NOSHA256;
240  }
241  } else {
242  if (flow->file_flags & FLOWFILE_NO_STORE_TC) {
243  flags |= FILE_NOSTORE;
244  }
245 
246  if (flow->file_flags & FLOWFILE_NO_MAGIC_TC) {
247  flags |= FILE_NOMAGIC;
248  }
249 
250  if (flow->file_flags & FLOWFILE_NO_MD5_TC) {
251  flags |= FILE_NOMD5;
252  }
253 
254  if (flow->file_flags & FLOWFILE_NO_SHA1_TC) {
255  flags |= FILE_NOSHA1;
256  }
257 
258  if (flow->file_flags & FLOWFILE_NO_SHA256_TC) {
259  flags |= FILE_NOSHA256;
260  }
261  }
262  return flags;
263 }
264 
265 static int FileMagicSize(void)
266 {
267  /** \todo make this size configurable */
268  return 512;
269 }
270 
271 /**
272  * \brief get the size of the file data
273  *
274  * This doesn't reflect how much of the file we have in memory, just the
275  * total size of filedata so far.
276  */
277 uint64_t FileDataSize(const File *file)
278 {
279  if (file != NULL && file->sb != NULL) {
280  SCLogDebug("returning %"PRIu64,
281  file->sb->stream_offset + file->sb->buf_offset);
282  return file->sb->stream_offset + file->sb->buf_offset;
283  }
284  SCLogDebug("returning 0 (default)");
285  return 0;
286 }
287 
288 /**
289  * \brief get the size of the file
290  *
291  * This doesn't reflect how much of the file we have in memory, just the
292  * total size of file so far.
293  */
294 uint64_t FileTrackedSize(const File *file)
295 {
296  if (file != NULL) {
297  return file->size;
298  }
299  return 0;
300 }
301 
302 static int FilePruneFile(File *file)
303 {
304  SCEnter();
305 #ifdef HAVE_MAGIC
306  if (!(file->flags & FILE_NOMAGIC)) {
307  /* need magic but haven't set it yet, bail out */
308  if (file->magic == NULL)
309  SCReturnInt(0);
310  else
311  SCLogDebug("file->magic %s", file->magic);
312  } else {
313  SCLogDebug("file->flags & FILE_NOMAGIC == true");
314  }
315 #endif
316  uint64_t left_edge = file->content_stored;
317  if (file->flags & FILE_NOSTORE) {
318  left_edge = FileDataSize(file);
319  }
320  if (file->flags & FILE_USE_DETECT) {
321  left_edge = MIN(left_edge, file->content_inspected);
322  }
323 
324  if (left_edge) {
325  StreamingBufferSlideToOffset(file->sb, left_edge);
326  }
327 
328  if (left_edge != FileDataSize(file)) {
329  SCReturnInt(0);
330  }
331 
332  SCLogDebug("file->state %d. Is >= FILE_STATE_CLOSED: %s", file->state, (file->state >= FILE_STATE_CLOSED) ? "yes" : "no");
333 
334  /* file is done when state is closed+, logging/storing is done (if any) */
335  if (file->state >= FILE_STATE_CLOSED &&
336  (!RunModeOutputFileEnabled() || (file->flags & FILE_LOGGED)) &&
338  {
339  SCReturnInt(1);
340  } else {
341  SCReturnInt(0);
342  }
343 }
344 
346 {
347  File *file = ffc->head;
348  File *prev = NULL;
349 
350  while (file) {
351  if (FilePruneFile(file) == 0) {
352  prev = file;
353  file = file->next;
354  continue;
355  }
356 
357  SCLogDebug("removing file %p", file);
358 
359  File *file_next = file->next;
360 
361  if (prev)
362  prev->next = file_next;
363  /* update head and tail */
364  if (file == ffc->head)
365  ffc->head = file_next;
366  if (file == ffc->tail)
367  ffc->tail = prev;
368 
369  FileFree(file);
370  file = file_next;
371  }
372 }
373 
374 /**
375  * \brief allocate a FileContainer
376  *
377  * \retval new newly allocated FileContainer
378  * \retval NULL error
379  */
381 {
382  FileContainer *new = SCMalloc(sizeof(FileContainer));
383  if (unlikely(new == NULL)) {
384  SCLogError(SC_ERR_MEM_ALLOC, "Error allocating mem");
385  return NULL;
386  }
387  memset(new, 0, sizeof(FileContainer));
388  new->head = new->tail = NULL;
389  return new;
390 }
391 
392 /**
393  * \brief Recycle a FileContainer
394  *
395  * \param ffc FileContainer
396  */
398 {
399  if (ffc == NULL)
400  return;
401 
402  File *cur = ffc->head;
403  File *next = NULL;
404  for (;cur != NULL; cur = next) {
405  next = cur->next;
406  FileFree(cur);
407  }
408  ffc->head = ffc->tail = NULL;
409 }
410 
411 /**
412  * \brief Free a FileContainer
413  *
414  * \param ffc FileContainer
415  */
417 {
418  if (ffc == NULL)
419  return;
420 
421  File *ptr = ffc->head;
422  File *next = NULL;
423  for (;ptr != NULL; ptr = next) {
424  next = ptr->next;
425  FileFree(ptr);
426  }
427  ffc->head = ffc->tail = NULL;
428  SCFree(ffc);
429 }
430 
431 /**
432  * \brief Alloc a new File
433  *
434  * \param name character array containing the name (not a string)
435  * \param name_len length in bytes of the name
436  *
437  * \retval new File object or NULL on error
438  */
439 static File *FileAlloc(const uint8_t *name, uint16_t name_len)
440 {
441  File *new = SCMalloc(sizeof(File));
442  if (unlikely(new == NULL)) {
443  SCLogError(SC_ERR_MEM_ALLOC, "Error allocating mem");
444  return NULL;
445  }
446  memset(new, 0, sizeof(File));
447 
448  new->name = SCMalloc(name_len);
449  if (new->name == NULL) {
450  SCFree(new);
451  return NULL;
452  }
453 
454  new->name_len = name_len;
455  memcpy(new->name, name, name_len);
456 
457  return new;
458 }
459 
460 static void FileFree(File *ff)
461 {
462  if (ff == NULL)
463  return;
464 
465  if (ff->name != NULL)
466  SCFree(ff->name);
467 #ifdef HAVE_MAGIC
468  /* magic returned by libmagic is strdup'd by MagicLookup. */
469  if (ff->magic != NULL)
470  SCFree(ff->magic);
471 #endif
472  if (ff->sb != NULL) {
473  StreamingBufferFree(ff->sb);
474  }
475 
476 #ifdef HAVE_NSS
477  if (ff->md5_ctx)
478  HASH_Destroy(ff->md5_ctx);
479  if (ff->sha1_ctx)
480  HASH_Destroy(ff->sha1_ctx);
481  if (ff->sha256_ctx)
482  HASH_Destroy(ff->sha256_ctx);
483 #endif
484  SCFree(ff);
485 }
486 
488 {
489  if (ffc->head == NULL || ffc->tail == NULL) {
490  ffc->head = ffc->tail = ff;
491  } else {
492  ffc->tail->next = ff;
493  ffc->tail = ff;
494  }
495 }
496 
497 /**
498  * \brief Tag a file for storing
499  *
500  * \param ff The file to store
501  */
502 int FileStore(File *ff)
503 {
504  ff->flags |= FILE_STORE;
505  SCReturnInt(0);
506 }
507 
508 /**
509  * \brief Set the TX id for a file
510  *
511  * \param ff The file to store
512  * \param txid the tx id
513  */
514 int FileSetTx(File *ff, uint64_t txid)
515 {
516  SCLogDebug("ff %p txid %"PRIu64, ff, txid);
517  if (ff != NULL)
518  ff->txid = txid;
519  SCReturnInt(0);
520 }
521 
523 {
524  if (ffc && ffc->tail) {
525  (void)FileSetTx(ffc->tail, tx_id);
526  }
527 }
528 
529 /**
530  * \brief check if we have stored enough
531  *
532  * \param ff file
533  *
534  * \retval 0 limit not reached yet
535  * \retval 1 limit reached
536  */
537 static int FileStoreNoStoreCheck(File *ff)
538 {
539  SCEnter();
540 
541  if (ff == NULL) {
542  SCReturnInt(0);
543  }
544 
545  if (ff->flags & FILE_NOSTORE) {
546  if (ff->state == FILE_STATE_OPENED &&
547  FileDataSize(ff) >= (uint64_t)FileMagicSize())
548  {
549  SCReturnInt(1);
550  }
551  }
552 
553  SCReturnInt(0);
554 }
555 
556 static int AppendData(File *file, const uint8_t *data, uint32_t data_len)
557 {
558  if (StreamingBufferAppendNoTrack(file->sb, data, data_len) != 0) {
559  SCReturnInt(-1);
560  }
561 
562 #ifdef HAVE_NSS
563  if (file->md5_ctx) {
564  HASH_Update(file->md5_ctx, data, data_len);
565  }
566  if (file->sha1_ctx) {
567  HASH_Update(file->sha1_ctx, data, data_len);
568  }
569  if (file->sha256_ctx) {
570  HASH_Update(file->sha256_ctx, data, data_len);
571  }
572 #endif
573  SCReturnInt(0);
574 }
575 
576 /** \internal
577  * \brief Store/handle a chunk of file data in the File structure
578  *
579  * \param ff the file
580  * \param data data chunk
581  * \param data_len data chunk len
582  *
583  * \retval 0 ok
584  * \retval -1 error
585  * \retval -2 no store for this file
586  */
587 static int FileAppendDataDo(File *ff, const uint8_t *data, uint32_t data_len)
588 {
589  SCEnter();
590 #ifdef DEBUG_VALIDATION
591  BUG_ON(ff == NULL);
592 #endif
593 
594  ff->size += data_len;
595 
596  if (ff->state != FILE_STATE_OPENED) {
597  if (ff->flags & FILE_NOSTORE) {
598  SCReturnInt(-2);
599  }
600  SCReturnInt(-1);
601  }
602 
603  if (FileStoreNoStoreCheck(ff) == 1) {
604 #ifdef HAVE_NSS
605  int hash_done = 0;
606  /* no storage but forced hashing */
607  if (ff->md5_ctx) {
608  HASH_Update(ff->md5_ctx, data, data_len);
609  hash_done = 1;
610  }
611  if (ff->sha1_ctx) {
612  HASH_Update(ff->sha1_ctx, data, data_len);
613  hash_done = 1;
614  }
615  if (ff->sha256_ctx) {
616  HASH_Update(ff->sha256_ctx, data, data_len);
617  hash_done = 1;
618  }
619 
620  if (hash_done)
621  SCReturnInt(0);
622 #endif
623  if (g_file_force_tracking || (!(ff->flags & FILE_NOTRACK)))
624  SCReturnInt(0);
625 
627  SCLogDebug("flowfile state transitioned to FILE_STATE_TRUNCATED");
628  SCReturnInt(-2);
629  }
630 
631  SCLogDebug("appending %"PRIu32" bytes", data_len);
632 
633  int r = AppendData(ff, data, data_len);
634  if (r != 0) {
635  ff->state = FILE_STATE_ERROR;
636  SCReturnInt(r);
637  }
638 
639  SCReturnInt(0);
640 }
641 
642 /**
643  * \brief Store/handle a chunk of file data in the File structure
644  * The last file in the FileContainer will be used.
645  *
646  * \param ffc FileContainer used to append to
647  * \param data data chunk
648  * \param data_len data chunk len
649  *
650  * \retval 0 ok
651  * \retval -1 error
652  * \retval -2 no store for this file
653  */
654 int FileAppendData(FileContainer *ffc, const uint8_t *data, uint32_t data_len)
655 {
656  SCEnter();
657 
658  if (ffc == NULL || ffc->tail == NULL || data == NULL || data_len == 0) {
659  SCReturnInt(-1);
660  }
661  int r = FileAppendDataDo(ffc->tail, data, data_len);
662  SCReturnInt(r);
663 }
664 
665 /**
666  * \brief Store/handle a chunk of file data in the File structure
667  * The file with 'track_id' in the FileContainer will be used.
668  *
669  * \param ffc FileContainer used to append to
670  * \param track_id id to lookup the file
671  * \param data data chunk
672  * \param data_len data chunk len
673  *
674  * \retval 0 ok
675  * \retval -1 error
676  * \retval -2 no store for this file
677  */
678 int FileAppendDataById(FileContainer *ffc, uint32_t track_id,
679  const uint8_t *data, uint32_t data_len)
680 {
681  SCEnter();
682 
683  if (ffc == NULL || ffc->tail == NULL || data == NULL || data_len == 0) {
684  SCReturnInt(-1);
685  }
686  File *ff = ffc->head;
687  for ( ; ff != NULL; ff = ff->next) {
688  if (track_id == ff->file_track_id) {
689  int r = FileAppendDataDo(ff, data, data_len);
690  SCReturnInt(r);
691  }
692  }
693  SCReturnInt(-1);
694 }
695 
696 /**
697  * \brief Store/handle a chunk of file data in the File structure
698  * The file with 'track_id' in the FileContainer will be used.
699  *
700  * \param ffc FileContainer used to append to
701  * \param track_id id to lookup the file
702  * \param data data chunk
703  * \param data_len data chunk len
704  *
705  * \retval 0 ok
706  * \retval -1 error
707  * \retval -2 no store for this file
708  */
709 int FileAppendGAPById(FileContainer *ffc, uint32_t track_id,
710  const uint8_t *data, uint32_t data_len)
711 {
712  SCEnter();
713 
714  if (ffc == NULL || ffc->tail == NULL || data == NULL || data_len == 0) {
715  SCReturnInt(-1);
716  }
717  File *ff = ffc->head;
718  for ( ; ff != NULL; ff = ff->next) {
719  if (track_id == ff->file_track_id) {
720  ff->flags |= FILE_HAS_GAPS;
723  SCLogDebug("FILE_HAS_GAPS set");
724 
725  int r = FileAppendDataDo(ff, data, data_len);
726  SCReturnInt(r);
727  }
728  }
729  SCReturnInt(-1);
730 }
731 
732 /**
733  * \brief Open a new File
734  *
735  * \param ffc flow container
736  * \param sbcfg buffer config
737  * \param name filename character array
738  * \param name_len filename len
739  * \param data initial data
740  * \param data_len initial data len
741  * \param flags open flags
742  *
743  * \retval ff flowfile object
744  *
745  * \note filename is not a string, so it's not nul terminated.
746  */
748  const uint8_t *name, uint16_t name_len,
749  const uint8_t *data, uint32_t data_len, uint16_t flags)
750 {
751  SCEnter();
752 
753  //PrintRawDataFp(stdout, name, name_len);
754 
755  File *ff = FileAlloc(name, name_len);
756  if (ff == NULL) {
757  SCReturnPtr(NULL, "File");
758  }
759 
760  ff->sb = StreamingBufferInit(sbcfg);
761  if (ff->sb == NULL) {
762  FileFree(ff);
763  SCReturnPtr(NULL, "File");
764  }
765  SCLogDebug("ff->sb %p", ff->sb);
766 
767  if (flags & FILE_STORE || g_file_force_filestore) {
768  FileStore(ff);
769  } else if (flags & FILE_NOSTORE) {
770  SCLogDebug("not storing this file");
771  ff->flags |= FILE_NOSTORE;
772  }
773  if (flags & FILE_NOMAGIC) {
774  SCLogDebug("not doing magic for this file");
775  ff->flags |= FILE_NOMAGIC;
776  }
777  if (flags & FILE_NOMD5) {
778  SCLogDebug("not doing md5 for this file");
779  ff->flags |= FILE_NOMD5;
780  }
781  if (flags & FILE_NOSHA1) {
782  SCLogDebug("not doing sha1 for this file");
783  ff->flags |= FILE_NOSHA1;
784  }
785  if (flags & FILE_NOSHA256) {
786  SCLogDebug("not doing sha256 for this file");
787  ff->flags |= FILE_NOSHA256;
788  }
789  if (flags & FILE_USE_DETECT) {
790  SCLogDebug("considering content_inspect tracker when pruning");
791  ff->flags |= FILE_USE_DETECT;
792  }
793 
794 #ifdef HAVE_NSS
795  if (!(ff->flags & FILE_NOMD5) || g_file_force_md5) {
796  ff->md5_ctx = HASH_Create(HASH_AlgMD5);
797  if (ff->md5_ctx != NULL) {
798  HASH_Begin(ff->md5_ctx);
799  }
800  }
801  if (!(ff->flags & FILE_NOSHA1) || g_file_force_sha1) {
802  ff->sha1_ctx = HASH_Create(HASH_AlgSHA1);
803  if (ff->sha1_ctx != NULL) {
804  HASH_Begin(ff->sha1_ctx);
805  }
806  }
807  if (!(ff->flags & FILE_NOSHA256) || g_file_force_sha256) {
808  ff->sha256_ctx = HASH_Create(HASH_AlgSHA256);
809  if (ff->sha256_ctx != NULL) {
810  HASH_Begin(ff->sha256_ctx);
811  }
812  }
813 #endif
814 
815  ff->state = FILE_STATE_OPENED;
816  SCLogDebug("flowfile state transitioned to FILE_STATE_OPENED");
817 
818  ff->fd = -1;
819 
820  FileContainerAdd(ffc, ff);
821 
822  if (data != NULL) {
823  ff->size += data_len;
824  if (AppendData(ff, data, data_len) != 0) {
825  ff->state = FILE_STATE_ERROR;
826  SCReturnPtr(NULL, "File");
827  }
828  SCLogDebug("file size is now %"PRIu64, FileTrackedSize(ff));
829  }
830 
831  SCReturnPtr(ff, "File");
832 }
833 
834 /**
835  * \retval 0 ok
836  * \retval -1 failed */
838  uint32_t track_id, const uint8_t *name, uint16_t name_len,
839  const uint8_t *data, uint32_t data_len, uint16_t flags)
840 {
841  File *ff = FileOpenFile(ffc, sbcfg, name, name_len, data, data_len, flags);
842  if (ff == NULL)
843  return -1;
844 
845  ff->file_track_id = track_id;
846  ff->flags |= FILE_USE_TRACKID;
847  return 0;
848 }
849 
850 int FileCloseFilePtr(File *ff, const uint8_t *data,
851  uint32_t data_len, uint16_t flags)
852 {
853  SCEnter();
854 
855  if (ff == NULL) {
856  SCReturnInt(-1);
857  }
858 
859  if (ff->state != FILE_STATE_OPENED) {
860  SCReturnInt(-1);
861  }
862 
863  if (data != NULL) {
864  ff->size += data_len;
865  if (ff->flags & FILE_NOSTORE) {
866 #ifdef HAVE_NSS
867  /* no storage but hashing */
868  if (ff->md5_ctx)
869  HASH_Update(ff->md5_ctx, data, data_len);
870  if (ff->sha1_ctx)
871  HASH_Update(ff->sha1_ctx, data, data_len);
872  if (ff->sha256_ctx)
873  HASH_Update(ff->sha256_ctx, data, data_len);
874 #endif
875  } else {
876  if (AppendData(ff, data, data_len) != 0) {
877  ff->state = FILE_STATE_ERROR;
878  SCReturnInt(-1);
879  }
880  }
881  }
882 
883  if ((flags & FILE_TRUNCATED) || (ff->flags & FILE_HAS_GAPS)) {
885  SCLogDebug("flowfile state transitioned to FILE_STATE_TRUNCATED");
886 
887  if (flags & FILE_NOSTORE) {
888  SCLogDebug("not storing this file");
889  ff->flags |= FILE_NOSTORE;
890  } else {
891 #ifdef HAVE_NSS
892  if (g_file_force_sha256 && ff->sha256_ctx) {
893  FileEndSha256(ff);
894  }
895 #endif
896  }
897  } else {
898  ff->state = FILE_STATE_CLOSED;
899  SCLogDebug("flowfile state transitioned to FILE_STATE_CLOSED");
900 
901 #ifdef HAVE_NSS
902  if (ff->md5_ctx) {
903  unsigned int len = 0;
904  HASH_End(ff->md5_ctx, ff->md5, &len, sizeof(ff->md5));
905  ff->flags |= FILE_MD5;
906  }
907  if (ff->sha1_ctx) {
908  unsigned int len = 0;
909  HASH_End(ff->sha1_ctx, ff->sha1, &len, sizeof(ff->sha1));
910  ff->flags |= FILE_SHA1;
911  }
912  if (ff->sha256_ctx) {
913  FileEndSha256(ff);
914  }
915 #endif
916  }
917 
918  SCReturnInt(0);
919 }
920 
921 /**
922  * \brief Close a File
923  *
924  * \param ffc the container
925  * \param data final data if any
926  * \param data_len data len if any
927  * \param flags flags
928  *
929  * \retval 0 ok
930  * \retval -1 error
931  */
932 int FileCloseFile(FileContainer *ffc, const uint8_t *data,
933  uint32_t data_len, uint16_t flags)
934 {
935  SCEnter();
936 
937  if (ffc == NULL || ffc->tail == NULL) {
938  SCReturnInt(-1);
939  }
940 
941  if (FileCloseFilePtr(ffc->tail, data, data_len, flags) == -1) {
942  SCReturnInt(-1);
943  }
944 
945  SCReturnInt(0);
946 }
947 
948 int FileCloseFileById(FileContainer *ffc, uint32_t track_id,
949  const uint8_t *data, uint32_t data_len, uint16_t flags)
950 {
951  SCEnter();
952 
953  if (ffc == NULL || ffc->tail == NULL) {
954  SCReturnInt(-1);
955  }
956 
957  File *ff = ffc->head;
958  for ( ; ff != NULL; ff = ff->next) {
959  if (track_id == ff->file_track_id) {
960  int r = FileCloseFilePtr(ff, data, data_len, flags);
961  SCReturnInt(r);
962  }
963  }
964  SCReturnInt(-1);
965 }
966 
967 /**
968  * \brief disable file storage for a flow
969  *
970  * \param f *LOCKED* flow
971  * \param direction flow direction
972  */
973 void FileDisableStoring(Flow *f, uint8_t direction)
974 {
975  File *ptr = NULL;
976 
977  SCEnter();
978 
980 
981  if (direction == STREAM_TOSERVER)
983  else
985 
986  FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
987  if (ffc != NULL) {
988  for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
989  /* if we're already storing, we'll continue */
990  if (!(ptr->flags & FILE_STORE)) {
991  SCLogDebug("not storing this file");
992  ptr->flags |= FILE_NOSTORE;
993  }
994  }
995  }
996  SCReturn;
997 }
998 
999 /**
1000  * \brief disable file magic lookups for this flow
1001  *
1002  * \param f *LOCKED* flow
1003  * \param direction flow direction
1004  */
1005 void FileDisableMagic(Flow *f, uint8_t direction)
1006 {
1007  File *ptr = NULL;
1008 
1009  SCEnter();
1010 
1012 
1013  if (direction == STREAM_TOSERVER)
1015  else
1017 
1018  FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
1019  if (ffc != NULL) {
1020  for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
1021  SCLogDebug("disabling magic for file %p from direction %s",
1022  ptr, direction == STREAM_TOSERVER ? "toserver":"toclient");
1023  ptr->flags |= FILE_NOMAGIC;
1024  }
1025  }
1026 
1027  SCReturn;
1028 }
1029 
1030 /**
1031  * \brief disable file md5 calc for this flow
1032  *
1033  * \param f *LOCKED* flow
1034  * \param direction flow direction
1035  */
1036 void FileDisableMd5(Flow *f, uint8_t direction)
1037 {
1038  File *ptr = NULL;
1039 
1040  SCEnter();
1041 
1043 
1044  if (direction == STREAM_TOSERVER)
1046  else
1048 
1049  FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
1050  if (ffc != NULL) {
1051  for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
1052  SCLogDebug("disabling md5 for file %p from direction %s",
1053  ptr, direction == STREAM_TOSERVER ? "toserver":"toclient");
1054  ptr->flags |= FILE_NOMD5;
1055 
1056 #ifdef HAVE_NSS
1057  /* destroy any ctx we may have so far */
1058  if (ptr->md5_ctx != NULL) {
1059  HASH_Destroy(ptr->md5_ctx);
1060  ptr->md5_ctx = NULL;
1061  }
1062 #endif
1063  }
1064  }
1065 
1066  SCReturn;
1067 }
1068 
1069 /**
1070  * \brief disable file sha1 calc for this flow
1071  *
1072  * \param f *LOCKED* flow
1073  * \param direction flow direction
1074 */
1075 void FileDisableSha1(Flow *f, uint8_t direction)
1076 {
1077  File *ptr = NULL;
1078 
1079  SCEnter();
1080 
1082 
1083  if (direction == STREAM_TOSERVER)
1085  else
1087 
1088  FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
1089  if (ffc != NULL) {
1090  for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
1091  SCLogDebug("disabling sha1 for file %p from direction %s",
1092  ptr, direction == STREAM_TOSERVER ? "toserver":"toclient");
1093  ptr->flags |= FILE_NOSHA1;
1094 
1095 #ifdef HAVE_NSS
1096  /* destroy any ctx we may have so far */
1097  if (ptr->sha1_ctx != NULL) {
1098  HASH_Destroy(ptr->sha1_ctx);
1099  ptr->sha1_ctx = NULL;
1100  }
1101 #endif
1102  }
1103  }
1104 
1105  SCReturn;
1106 }
1107 
1108 /**
1109  * \brief disable file sha256 calc for this flow
1110  *
1111  * \param f *LOCKED* flow
1112  * \param direction flow direction
1113  */
1114 void FileDisableSha256(Flow *f, uint8_t direction)
1115 {
1116  File *ptr = NULL;
1117 
1118  SCEnter();
1119 
1121 
1122  if (direction == STREAM_TOSERVER)
1124  else
1126 
1127  FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
1128  if (ffc != NULL) {
1129  for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
1130  SCLogDebug("disabling sha256 for file %p from direction %s",
1131  ptr, direction == STREAM_TOSERVER ? "toserver":"toclient");
1132  ptr->flags |= FILE_NOSHA256;
1133 
1134 #ifdef HAVE_NSS
1135  /* destroy any ctx we may have so far */
1136  if (ptr->sha256_ctx != NULL) {
1137  HASH_Destroy(ptr->sha256_ctx);
1138  ptr->sha256_ctx = NULL;
1139  }
1140 #endif
1141  }
1142  }
1143 
1144  SCReturn;
1145 }
1146 
1147 /**
1148  * \brief disable file size tracking for this flow
1149  *
1150  * \param f *LOCKED* flow
1151  * \param direction flow direction
1152  */
1153 void FileDisableFilesize(Flow *f, uint8_t direction)
1154 {
1155  File *ptr = NULL;
1156 
1157  SCEnter();
1158 
1160 
1161  if (direction == STREAM_TOSERVER)
1163  else
1165 
1166  FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
1167  if (ffc != NULL) {
1168  for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
1169  SCLogDebug("disabling size tracking for file %p from direction %s",
1170  ptr, direction == STREAM_TOSERVER ? "toserver":"toclient");
1171  ptr->flags |= FILE_NOTRACK;
1172  }
1173  }
1174 
1175  SCReturn;
1176 }
1177 
1178 
1179 /**
1180  * \brief set no store flag, close file if needed
1181  *
1182  * \param ff file
1183  */
1184 static void FileDisableStoringForFile(File *ff)
1185 {
1186  SCEnter();
1187 
1188  if (ff == NULL) {
1189  SCReturn;
1190  }
1191 
1192  SCLogDebug("not storing this file");
1193  ff->flags |= FILE_NOSTORE;
1194 
1195  if (ff->state == FILE_STATE_OPENED && FileDataSize(ff) >= (uint64_t)FileMagicSize()) {
1196  if (g_file_force_md5 == 0 && g_file_force_sha1 == 0 && g_file_force_sha256 == 0
1197  && g_file_force_tracking == 0) {
1198  (void)FileCloseFilePtr(ff, NULL, 0,
1200  }
1201  }
1202 }
1203 
1204 /**
1205  * \brief disable file storing for files in a transaction
1206  *
1207  * \param f *LOCKED* flow
1208  * \param direction flow direction
1209  * \param tx_id transaction id
1210  */
1211 void FileDisableStoringForTransaction(Flow *f, uint8_t direction, uint64_t tx_id)
1212 {
1213  File *ptr = NULL;
1214 
1216 
1217  SCEnter();
1218 
1219  FileContainer *ffc = AppLayerParserGetFiles(f->proto, f->alproto, f->alstate, direction);
1220  if (ffc != NULL) {
1221  for (ptr = ffc->head; ptr != NULL; ptr = ptr->next) {
1222  if (ptr->txid == tx_id) {
1223  if (ptr->flags & FILE_STORE) {
1224  /* weird, already storing -- let it continue*/
1225  SCLogDebug("file is already being stored");
1226  } else {
1227  FileDisableStoringForFile(ptr);
1228  }
1229  }
1230  }
1231  }
1232 
1233  SCReturn;
1234 }
1235 
1236 /**
1237  * \brief flag a file with id "file_id" to be stored.
1238  *
1239  * \param fc file store
1240  * \param file_id the file's id
1241  */
1242 void FileStoreFileById(FileContainer *fc, uint32_t file_id)
1243 {
1244  File *ptr = NULL;
1245 
1246  SCEnter();
1247 
1248  if (fc != NULL) {
1249  for (ptr = fc->head; ptr != NULL; ptr = ptr->next) {
1250  if (ptr->file_store_id == file_id) {
1251  FileStore(ptr);
1252  }
1253  }
1254  }
1255 }
1256 
1258 {
1259  File *ptr = NULL;
1260 
1261  SCEnter();
1262 
1263  if (fc != NULL) {
1264  for (ptr = fc->head; ptr != NULL; ptr = ptr->next) {
1265  if (ptr->txid == tx_id) {
1266  FileStore(ptr);
1267  }
1268  }
1269  }
1270 }
1271 
1273 {
1274  File *ptr = NULL;
1275 
1276  SCEnter();
1277 
1278  if (fc != NULL) {
1279  for (ptr = fc->head; ptr != NULL; ptr = ptr->next) {
1280  FileStore(ptr);
1281  }
1282  }
1283 }
1284 
1286 {
1287  File *ptr = NULL;
1288 
1289  SCEnter();
1290 
1291  if (fc != NULL) {
1292  for (ptr = fc->head; ptr != NULL; ptr = ptr->next) {
1293  if (ptr->state == FILE_STATE_OPENED) {
1294  FileCloseFilePtr(ptr, NULL, 0, FILE_TRUNCATED);
1295  }
1296  }
1297  }
1298 }
1299 
1300 /**
1301  * \brief Finish the SHA256 calculation.
1302  */
1303 #ifdef HAVE_NSS
1304 static void FileEndSha256(File *ff)
1305 {
1306  if (!(ff->flags & FILE_SHA256) && ff->sha256_ctx) {
1307  unsigned int len = 0;
1308  HASH_End(ff->sha256_ctx, ff->sha256, &len, sizeof(ff->sha256));
1309  ff->flags |= FILE_SHA256;
1310  }
1311 }
1312 #endif
uint16_t flags
void FileDisableStoringForTransaction(Flow *f, uint8_t direction, uint64_t tx_id)
disable file storing for files in a transaction
Definition: util-file.c:1211
#define SCLogDebug(...)
Definition: util-debug.h:335
StreamingBuffer * StreamingBufferInit(const StreamingBufferConfig *cfg)
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
struct HtpBodyChunk_ * next
int FileForceSha256(void)
Definition: util-file.c:145
TcpStreamCnf stream_config
Definition: stream-tcp.h:106
#define BUG_ON(x)
uint8_t proto
Definition: flow.h:346
FileContainer * FileContainerAlloc(void)
allocate a FileContainer
Definition: util-file.c:380
#define FLOWFILE_NO_SHA256_TS
Definition: flow.h:123
int RunModeOutputFileEnabled(void)
Definition: runmodes.c:490
#define unlikely(expr)
Definition: util-optimize.h:35
#define FILE_SHA256
Definition: util-file.h:43
uint64_t size
Definition: util-file.h:91
void FileDisableFilesize(Flow *f, uint8_t direction)
disable file size tracking for this flow
Definition: util-file.c:1153
void FileDisableSha1(Flow *f, uint8_t direction)
disable file sha1 calc for this flow
Definition: util-file.c:1075
void FileDisableMd5(Flow *f, uint8_t direction)
disable file md5 calc for this flow
Definition: util-file.c:1036
uint64_t FileDataSize(const File *file)
get the size of the file data
Definition: util-file.c:277
#define FILE_NOTRACK
Definition: util-file.h:48
#define MIN(x, y)
File * FileOpenFile(FileContainer *ffc, const StreamingBufferConfig *sbcfg, 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:747
uint64_t content_inspected
Definition: util-file.h:88
#define FILE_STORE
Definition: util-file.h:46
#define FILE_NOMAGIC
Definition: util-file.h:37
int fd
Definition: util-file.h:73
char * val
Definition: conf.h:34
void FileContainerSetTx(FileContainer *ffc, uint64_t tx_id)
Definition: util-file.c:522
ConfNode * ConfNodeLookupChild(const ConfNode *node, const char *name)
Lookup a child configuration node by name.
Definition: conf.c:815
struct File_ * next
Definition: util-file.h:79
int FileCloseFileById(FileContainer *ffc, uint32_t track_id, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:948
void FileReassemblyDepthEnable(uint32_t size)
Definition: util-file.c:116
StreamingBuffer * sb
Definition: util-file.h:68
void FileForceSha1Enable(void)
Definition: util-file.c:101
int FileForceMd5(void)
Definition: util-file.c:135
#define FILE_NOSHA1
Definition: util-file.h:40
void FileForceTrackingEnable(void)
Definition: util-file.c:150
#define FLOWFILE_NO_MAGIC_TS
Definition: flow.h:108
uint32_t FileReassemblyDepth(void)
Definition: util-file.c:122
void * alstate
Definition: flow.h:436
#define FLOWFILE_NO_MD5_TS
Definition: flow.h:115
void StreamingBufferFree(StreamingBuffer *sb)
#define FILE_LOGGED
Definition: util-file.h:44
#define FLOWFILE_NO_MAGIC_TC
Definition: flow.h:109
#define FILE_USE_TRACKID
Definition: util-file.h:50
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:843
int FileStore(File *ff)
Tag a file for storing.
Definition: util-file.c:502
uint16_t flags
Definition: util-file.h:65
void FileStoreAllFilesForTx(FileContainer *fc, uint64_t tx_id)
Definition: util-file.c:1257
#define FLOWFILE_NO_SIZE_TS
Definition: flow.h:127
void FileStoreFileById(FileContainer *fc, uint32_t file_id)
flag a file with id "file_id" to be stored.
Definition: util-file.c:1242
void FileDisableMagic(Flow *f, uint8_t direction)
disable file magic lookups for this flow
Definition: util-file.c:1005
void StreamingBufferSlideToOffset(StreamingBuffer *sb, uint64_t offset)
slide to absolute offset
#define FILE_HAS_GAPS
Definition: util-file.h:51
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
File * tail
Definition: util-file.h:96
void FileForceHashParseCfg(ConfNode *conf)
Function to parse forced file hashing configuration.
Definition: util-file.c:158
void FileContainerFree(FileContainer *ffc)
Free a FileContainer.
Definition: util-file.c:416
void FileForceMagicEnable(void)
Definition: util-file.c:91
#define DEBUG_ASSERT_FLOW_LOCKED(f)
uint64_t txid
Definition: util-file.h:69
#define SCEnter(...)
Definition: util-debug.h:337
#define FLOWFILE_NO_MD5_TC
Definition: flow.h:116
void FileForceFilestoreEnable(void)
Definition: util-file.c:86
File * head
Definition: util-file.h:95
void FileDisableStoring(Flow *f, uint8_t direction)
disable file storage for a flow
Definition: util-file.c:973
#define SCReturnInt(x)
Definition: util-debug.h:341
#define FILE_MD5
Definition: util-file.h:39
void FilePrune(FileContainer *ffc)
Definition: util-file.c:345
void FileStoreAllFiles(FileContainer *fc)
Definition: util-file.c:1272
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
uint32_t reassembly_depth
Definition: stream-tcp.h:60
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:566
#define FILE_SHA1
Definition: util-file.h:41
int FileForceFilestore(void)
Definition: util-file.c:111
int FileForceMagic(void)
Definition: util-file.c:130
#define FILE_STORED
Definition: util-file.h:47
Definition: conf.h:32
#define FILE_NOSHA256
Definition: util-file.h:42
#define SCMalloc(a)
Definition: util-mem.h:174
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:709
#define FILE_USE_DETECT
Definition: util-file.h:49
void FileContainerAdd(FileContainer *ffc, File *ff)
Definition: util-file.c:487
void FileForceSha256Enable(void)
Definition: util-file.c:106
int RunModeOutputFiledataEnabled(void)
Definition: runmodes.c:495
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
void FileContainerRecycle(FileContainer *ffc)
Recycle a FileContainer.
Definition: util-file.c:397
#define SCFree(a)
Definition: util-mem.h:236
#define FLOWFILE_NO_SHA1_TS
Definition: flow.h:119
#define FLOWFILE_NO_STORE_TS
Definition: flow.h:112
uint16_t tx_id
#define FLOWFILE_NO_SIZE_TC
Definition: flow.h:128
#define STREAM_TOSERVER
Definition: stream.h:31
#define SCReturnPtr(x, type)
Definition: util-debug.h:353
FileState state
Definition: util-file.h:67
int FileOpenFileWithId(FileContainer *ffc, const StreamingBufferConfig *sbcfg, uint32_t track_id, const uint8_t *name, uint16_t name_len, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:837
int StreamingBufferAppendNoTrack(StreamingBuffer *sb, const uint8_t *data, uint32_t data_len)
add data w/o tracking a segment
int FileCloseFile(FileContainer *ffc, const uint8_t *data, uint32_t data_len, uint16_t flags)
Close a File.
Definition: util-file.c:932
int FileSetTx(File *ff, uint64_t txid)
Set the TX id for a file.
Definition: util-file.c:514
uint16_t FileFlowToFlags(const Flow *flow, uint8_t direction)
Definition: util-file.c:217
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
#define FILE_TRUNCATED
Definition: util-file.h:36
int FileForceSha1(void)
Definition: util-file.c:140
int FileAppendDataById(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:678
uint64_t FileTrackedSize(const File *file)
get the size of the file
Definition: util-file.c:294
void FileForceMd5Enable(void)
Definition: util-file.c:96
#define SCReturn
Definition: util-debug.h:339
#define FLOWFILE_NO_SHA1_TC
Definition: flow.h:120
uint8_t len
void FileDisableSha256(Flow *f, uint8_t direction)
disable file sha256 calc for this flow
Definition: util-file.c:1114
uint16_t file_flags
Definition: flow.h:379
AppProto alproto
application level protocol
Definition: flow.h:407
FileContainer * AppLayerParserGetFiles(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t direction)
int FileAppendData(FileContainer *ffc, const uint8_t *data, uint32_t data_len)
Store/handle a chunk of file data in the File structure The last file in the FileContainer will be us...
Definition: util-file.c:654
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
#define FLOWFILE_NO_SHA256_TC
Definition: flow.h:124
#define FILE_NOSTORE
Definition: util-file.h:45
#define FLOWFILE_NO_STORE_TC
Definition: flow.h:113
int FileCloseFilePtr(File *ff, const uint8_t *data, uint32_t data_len, uint16_t flags)
Definition: util-file.c:850
Flow data structure.
Definition: flow.h:327
uint8_t * name
Definition: util-file.h:75
void FileTruncateAllOpenFiles(FileContainer *fc)
Definition: util-file.c:1285
#define FILE_NOMD5
Definition: util-file.h:38