@ -97,25 +97,15 @@ error:
}
/**
* \ brief match the specified filestore
*
* \ param t pointer to thread vars
* \ param det_ctx pointer to the pattern matcher thread
* \ param p pointer to the current packet
* \ param m pointer to the sigmatch that we will cast into DetectFilestoreData
*
* \ retval 0 no match
* \ retval 1 match
*
* \ todo when we start supporting more protocols , the logic in this function
* needs to be put behind a api .
* \ brief apply the post match filestore with options
*/
int DetectFilestoreMatch ( ThreadVars * t , DetectEngineThreadCtx * det_ctx , Flow * f ,
uint 8_t flags , void * state , Signature * s , SigMatch * m )
static int FilestorePostMatchWithOptions ( Packet * p , Flow * f , DetectFilestoreData * filestore , FileContainer * fc ,
uint16_t file_id , uint16_t tx_id )
{
SCEnter ( ) ;
DetectFilestoreData * filestore = m - > ctx ;
if ( filestore ! = NULL ) {
if ( filestore = = NULL ) {
SCReturnInt ( 0 ) ;
}
int this_file = 0 ;
int this_tx = 0 ;
int this_flow = 0 ;
@ -143,9 +133,9 @@ int DetectFilestoreMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Flow *f
case FILESTORE_SCOPE_DEFAULT :
if ( rule_dir ) {
this_file = 1 ;
} else if ( flags & STREAM _TOCLIENT & & toclient_dir ) {
} else if ( p - > flowflags & FLOW_PKT _TOCLIENT & & toclient_dir ) {
this_file = 1 ;
} else if ( flags & STREAM _TOSERVER & & toserver_dir ) {
} else if ( p - > flowflags & FLOW_PKT _TOSERVER & & toserver_dir ) {
this_file = 1 ;
}
break ;
@ -158,23 +148,21 @@ int DetectFilestoreMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Flow *f
}
if ( this_file ) {
File * file = ( File * ) state ;
FileStore ( file ) ;
FileStoreFileById ( fc , file_id ) ;
} else if ( this_tx ) {
/* flag tx all files will be stored */
if ( f - > alproto = = ALPROTO_HTTP & & f - > alstate ! = NULL ) {
HtpState * htp_state = f - > alstate ;
if ( toserver_dir ) {
htp_state - > flags | = HTP_FLAG_STORE_FILES_TX_TS ;
FileStoreAllFilesForTx ( htp_state - > files_ts , det_ctx - > tx_id ) ;
FileStoreAllFilesForTx ( htp_state - > files_ts , tx_id ) ;
}
if ( toclient_dir ) {
htp_state - > flags | = HTP_FLAG_STORE_FILES_TX_TC ;
FileStoreAllFilesForTx ( htp_state - > files_tc , det_ctx - > tx_id ) ;
FileStoreAllFilesForTx ( htp_state - > files_tc , tx_id ) ;
}
htp_state - > store_tx_id = det_ctx - > tx_id ;
htp_state - > store_tx_id = tx_id ;
}
} else if ( this_flow ) {
/* flag flow all files will be stored */
if ( f - > alproto = = ALPROTO_HTTP & & f - > alstate ! = NULL ) {
@ -189,13 +177,99 @@ int DetectFilestoreMatch (ThreadVars *t, DetectEngineThreadCtx *det_ctx, Flow *f
}
}
} else {
File * file = ( File * ) state ;
FileStore ( file ) ;
FileStoreFileById ( fc , file_id ) ;
}
SCReturnInt ( 0 ) ;
}
/**
* \ brief post - match function for filestore
*
* The match function for filestore records store candidates in the det_ctx .
* When we are sure all parts of the signature matched , we run this function
* to finalize the filestore .
*/
int DetectFilestorePostMatch ( ThreadVars * t , DetectEngineThreadCtx * det_ctx , Packet * p ) {
uint8_t flags = 0 ;
SCEnter ( ) ;
if ( det_ctx - > filestore_cnt = = 0 ) {
SCReturnInt ( 0 ) ;
}
if ( det_ctx - > filestore_sm = = NULL | | p - > flow = = NULL ) {
# ifndef DEBUG
SCReturnInt ( 0 ) ;
# else
BUG_ON ( 1 ) ;
# endif
}
if ( p - > flowflags & FLOW_PKT_TOCLIENT )
flags | = STREAM_TOCLIENT ;
else
flags | = STREAM_TOSERVER ;
SCMutexLock ( & p - > flow - > m ) ;
FileContainer * ffc = AppLayerGetFilesFromFlow ( p - > flow , flags ) ;
/* filestore for single files only */
if ( det_ctx - > filestore_sm - > ctx = = NULL ) {
uint16_t u ;
for ( u = 0 ; u < det_ctx - > filestore_cnt ; u + + ) {
FileStoreFileById ( ffc , det_ctx - > filestore [ u ] . file_id ) ;
}
} else {
File * file = ( File * ) state ;
FileStore ( file ) ;
DetectFilestoreData * filestore = det_ctx - > filestore_sm - > ctx ;
uint16_t u ;
for ( u = 0 ; u < det_ctx - > filestore_cnt ; u + + ) {
FilestorePostMatchWithOptions ( p , p - > flow , filestore , ffc ,
det_ctx - > filestore [ u ] . file_id , det_ctx - > filestore [ u ] . tx_id ) ;
}
}
SCMutexUnlock ( & p - > flow - > m ) ;
SCReturnInt ( 0 ) ;
}
/**
* \ brief match the specified filestore
*
* \ param t pointer to thread vars
* \ param det_ctx pointer to the pattern matcher thread
* \ param p pointer to the current packet
* \ param m pointer to the sigmatch that we will cast into DetectFilestoreData
*
* \ retval 0 no match
* \ retval 1 match
*
* \ todo when we start supporting more protocols , the logic in this function
* needs to be put behind a api .
*/
int DetectFilestoreMatch ( ThreadVars * t , DetectEngineThreadCtx * det_ctx , Flow * f ,
uint8_t flags , void * state , Signature * s , SigMatch * m )
{
SCEnter ( ) ;
if ( det_ctx - > filestore_cnt > DETECT_FILESTORE_MAX ) {
SCReturnInt ( 1 ) ;
}
File * file = ( File * ) state ;
det_ctx - > filestore [ det_ctx - > filestore_cnt ] . file_id = file - > file_id ;
det_ctx - > filestore [ det_ctx - > filestore_cnt ] . tx_id = det_ctx - > tx_id ;
SCLogDebug ( " %u, file %u, tx %u " , det_ctx - > filestore_cnt ,
det_ctx - > filestore [ det_ctx - > filestore_cnt ] . file_id ,
det_ctx - > filestore [ det_ctx - > filestore_cnt ] . tx_id ) ;
det_ctx - > filestore_cnt + + ;
det_ctx - > filestore_sm = m ;
SCReturnInt ( 1 ) ;
}