2011-11-19 21:25:44 +01:00
/*
* attachment . c , SJ
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <time.h>
# include <sys/types.h>
# include <sys/mman.h>
# include <sys/socket.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include <syslog.h>
# include <piler.h>
2013-01-28 12:10:39 +01:00
int store_attachments ( struct session_data * sdata , struct _state * state , struct __data * data , struct __config * cfg ) {
2011-11-19 21:25:44 +01:00
uint64 id = 0 ;
2012-01-29 09:02:21 +01:00
int i , rc = 1 , found , affected_rows ;
2011-11-28 14:21:14 +01:00
MYSQL_BIND bind [ 7 ] ;
unsigned long len [ 7 ] ;
2011-11-23 12:24:21 +01:00
2013-01-28 12:10:39 +01:00
if ( prepare_a_mysql_statement ( sdata , & ( data - > stmt_insert_into_attachment_table ) , SQL_PREPARED_STMT_INSERT_INTO_ATTACHMENT_TABLE ) = = ERR ) return rc ;
if ( prepare_a_mysql_statement ( sdata , & ( data - > stmt_get_attachment_id_by_signature ) , SQL_PREPARED_STMT_GET_ATTACHMENT_ID_BY_SIGNATURE ) = = ERR ) return rc ;
2011-11-23 12:24:21 +01:00
2011-11-22 14:25:51 +01:00
for ( i = 1 ; i < = state - > n_attachments ; i + + ) {
2011-11-19 21:25:44 +01:00
found = 0 ;
id = 0 ;
2012-11-02 22:17:21 +01:00
if ( state - > attachments [ i ] . size > 0 ) {
2011-11-19 21:25:44 +01:00
2013-01-28 12:10:39 +01:00
memset ( bind , 0 , sizeof ( bind ) ) ;
2011-11-19 21:25:44 +01:00
2013-01-28 12:10:39 +01:00
bind [ 0 ] . buffer_type = MYSQL_TYPE_STRING ;
bind [ 0 ] . buffer = state - > attachments [ i ] . digest ;
bind [ 0 ] . is_null = 0 ;
len [ 0 ] = strlen ( state - > attachments [ i ] . digest ) ; bind [ 0 ] . length = & len [ 0 ] ;
2011-11-19 21:25:44 +01:00
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_bind_param ( data - > stmt_get_attachment_id_by_signature , bind ) ) {
syslog ( LOG_PRIORITY , " %s: %s.mysql_stmt_bind_param() error for get attachment id: %s " , sdata - > ttmpfile , SQL_ATTACHMENT_TABLE , mysql_stmt_error ( data - > stmt_get_attachment_id_by_signature ) ) ;
goto NOT_FOUND ;
}
if ( mysql_stmt_execute ( data - > stmt_get_attachment_id_by_signature ) ) {
syslog ( LOG_PRIORITY , " %s get attachment id execute error: *%s* " , sdata - > ttmpfile , mysql_error ( & ( sdata - > mysql ) ) ) ;
goto NOT_FOUND ;
}
memset ( bind , 0 , sizeof ( bind ) ) ;
bind [ 0 ] . buffer_type = MYSQL_TYPE_LONGLONG ;
bind [ 0 ] . buffer = ( char * ) & id ;
bind [ 0 ] . is_null = 0 ;
bind [ 0 ] . length = 0 ;
if ( mysql_stmt_bind_result ( data - > stmt_get_attachment_id_by_signature , bind ) ) {
syslog ( LOG_PRIORITY , " %s: %s.mysql_stmt_bind_result() error: %s " , sdata - > ttmpfile , SQL_ATTACHMENT_TABLE , mysql_stmt_error ( data - > stmt_get_attachment_id_by_signature ) ) ;
goto NOT_FOUND ;
2011-11-19 21:25:44 +01:00
}
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_store_result ( data - > stmt_get_attachment_id_by_signature ) ) {
goto NOT_FOUND ;
}
if ( ! mysql_stmt_fetch ( data - > stmt_get_attachment_id_by_signature ) ) {
found = 1 ;
}
NOT_FOUND :
2011-11-19 21:25:44 +01:00
if ( found = = 0 ) {
if ( store_file ( sdata , state - > attachments [ i ] . internalname , 0 , 0 , cfg ) = = 0 ) {
syslog ( LOG_PRIORITY , " %s: error storing attachment: %s " , sdata - > ttmpfile , state - > attachments [ i ] . internalname ) ;
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-11-19 21:25:44 +01:00
}
}
2011-11-23 12:24:21 +01:00
memset ( bind , 0 , sizeof ( bind ) ) ;
bind [ 0 ] . buffer_type = MYSQL_TYPE_STRING ;
bind [ 0 ] . buffer = sdata - > ttmpfile ;
bind [ 0 ] . is_null = 0 ;
len [ 0 ] = strlen ( sdata - > ttmpfile ) ; bind [ 0 ] . length = & len [ 0 ] ;
bind [ 1 ] . buffer_type = MYSQL_TYPE_LONG ;
bind [ 1 ] . buffer = ( char * ) & i ;
bind [ 1 ] . is_null = 0 ;
bind [ 1 ] . length = 0 ;
bind [ 2 ] . buffer_type = MYSQL_TYPE_STRING ;
bind [ 2 ] . buffer = state - > attachments [ i ] . digest ;
bind [ 2 ] . is_null = 0 ;
len [ 2 ] = strlen ( state - > attachments [ i ] . digest ) ; bind [ 2 ] . length = & len [ 2 ] ;
bind [ 3 ] . buffer_type = MYSQL_TYPE_STRING ;
2011-11-28 14:21:14 +01:00
bind [ 3 ] . buffer = state - > attachments [ i ] . filename ;
2011-11-23 12:24:21 +01:00
bind [ 3 ] . is_null = 0 ;
2011-11-28 14:21:14 +01:00
len [ 3 ] = strlen ( state - > attachments [ i ] . filename ) ; bind [ 3 ] . length = & len [ 3 ] ;
2011-11-23 12:24:21 +01:00
2011-11-28 14:21:14 +01:00
bind [ 4 ] . buffer_type = MYSQL_TYPE_STRING ;
bind [ 4 ] . buffer = state - > attachments [ i ] . type ;
2011-11-23 12:24:21 +01:00
bind [ 4 ] . is_null = 0 ;
2011-12-30 15:52:59 +01:00
len [ 4 ] = strlen ( state - > attachments [ i ] . type ) ; bind [ 4 ] . length = & len [ 4 ] ;
2011-11-23 12:24:21 +01:00
2011-11-28 14:21:14 +01:00
bind [ 5 ] . buffer_type = MYSQL_TYPE_LONG ;
bind [ 5 ] . buffer = ( char * ) & ( state - > attachments [ i ] . size ) ;
2011-11-23 12:24:21 +01:00
bind [ 5 ] . is_null = 0 ;
bind [ 5 ] . length = 0 ;
2011-11-28 14:21:14 +01:00
bind [ 6 ] . buffer_type = MYSQL_TYPE_LONGLONG ;
bind [ 6 ] . buffer = ( char * ) & id ;
bind [ 6 ] . is_null = 0 ;
bind [ 6 ] . length = 0 ;
2011-11-23 12:24:21 +01:00
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_bind_param ( data - > stmt_insert_into_attachment_table , bind ) ) {
syslog ( LOG_PRIORITY , " %s: %s.mysql_stmt_bind_param() error: %s " , sdata - > ttmpfile , SQL_ATTACHMENT_TABLE , mysql_stmt_error ( data - > stmt_insert_into_attachment_table ) ) ;
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-11-23 12:24:21 +01:00
}
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_execute ( data - > stmt_insert_into_attachment_table ) ) {
2011-11-19 21:25:44 +01:00
syslog ( LOG_PRIORITY , " %s attachment sql error: *%s* " , sdata - > ttmpfile , mysql_error ( & ( sdata - > mysql ) ) ) ;
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-11-19 21:25:44 +01:00
}
2013-01-28 12:10:39 +01:00
affected_rows = mysql_stmt_affected_rows ( data - > stmt_insert_into_attachment_table ) ;
2011-11-23 14:37:17 +01:00
if ( affected_rows ! = 1 ) {
syslog ( LOG_PRIORITY , " %s attachment sql error: affected rows: %d " , sdata - > ttmpfile , affected_rows ) ;
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-11-23 14:37:17 +01:00
}
2011-11-19 21:25:44 +01:00
}
else {
syslog ( LOG_PRIORITY , " %s: skipping attachment (serial: %d, size: %d, digest: %s) " , sdata - > ttmpfile , i , state - > attachments [ i ] . size , state - > attachments [ i ] . digest ) ;
}
}
2012-01-29 09:02:21 +01:00
rc = 0 ;
CLOSE :
2013-01-28 12:10:39 +01:00
mysql_stmt_close ( data - > stmt_insert_into_attachment_table ) ;
mysql_stmt_close ( data - > stmt_get_attachment_id_by_signature ) ;
2012-01-29 09:02:21 +01:00
return rc ;
2011-11-19 21:25:44 +01:00
}
2011-12-28 15:30:53 +01:00
2013-01-28 12:10:39 +01:00
int query_attachment_pointers ( struct session_data * sdata , struct __data * data , uint64 ptr , char * piler_id , int * id , struct __config * cfg ) {
2011-12-28 15:30:53 +01:00
int rc = 0 ;
MYSQL_BIND bind [ 2 ] ;
my_bool is_null [ 2 ] ;
unsigned long len = 0 ;
2013-01-28 12:10:39 +01:00
if ( prepare_a_mysql_statement ( sdata , & ( data - > stmt_get_attachment_pointer ) , SQL_PREPARED_STMT_GET_ATTACHMENT_POINTER ) = = ERR ) goto ENDE ;
2011-12-28 15:30:53 +01:00
memset ( bind , 0 , sizeof ( bind ) ) ;
bind [ 0 ] . buffer_type = MYSQL_TYPE_LONGLONG ;
bind [ 0 ] . buffer = ( char * ) & ptr ;
bind [ 0 ] . is_null = 0 ;
len = sizeof ( uint64 ) ; bind [ 0 ] . length = & len ;
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_bind_param ( data - > stmt_get_attachment_pointer , bind ) ) {
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-12-28 15:30:53 +01:00
}
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_execute ( data - > stmt_get_attachment_pointer ) ) {
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-12-28 15:30:53 +01:00
}
memset ( bind , 0 , sizeof ( bind ) ) ;
bind [ 0 ] . buffer_type = MYSQL_TYPE_STRING ;
bind [ 0 ] . buffer = piler_id ;
bind [ 0 ] . buffer_length = RND_STR_LEN ;
bind [ 0 ] . is_null = & is_null [ 0 ] ;
bind [ 0 ] . length = & len ;
bind [ 1 ] . buffer_type = MYSQL_TYPE_LONG ;
bind [ 1 ] . buffer = ( char * ) id ;
bind [ 1 ] . is_null = 0 ;
bind [ 1 ] . length = 0 ;
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_bind_result ( data - > stmt_get_attachment_pointer , bind ) ) {
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-12-28 15:30:53 +01:00
}
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_store_result ( data - > stmt_get_attachment_pointer ) ) {
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-12-28 15:30:53 +01:00
}
2013-01-28 12:10:39 +01:00
if ( ! mysql_stmt_fetch ( data - > stmt_get_attachment_pointer ) ) {
2011-12-28 15:30:53 +01:00
if ( is_null [ 0 ] = = 0 ) {
rc = 1 ;
}
}
2012-01-29 09:02:21 +01:00
CLOSE :
2013-01-28 12:10:39 +01:00
mysql_stmt_close ( data - > stmt_get_attachment_pointer ) ;
2011-12-28 15:30:53 +01:00
ENDE :
return rc ;
}
2013-01-28 12:10:39 +01:00
int query_attachments ( struct session_data * sdata , struct __data * data , struct ptr_array * ptr_arr , struct __config * cfg ) {
2011-12-28 15:30:53 +01:00
int i , rc , id , attachments = 0 ;
uint64 ptr ;
MYSQL_BIND bind [ 2 ] ;
my_bool is_null [ 2 ] ;
unsigned long len = 0 ;
for ( i = 0 ; i < MAX_ATTACHMENTS ; i + + ) memset ( ( char * ) & ptr_arr [ i ] , 0 , sizeof ( struct ptr_array ) ) ;
2013-01-28 12:10:39 +01:00
if ( prepare_a_mysql_statement ( sdata , & ( data - > stmt_query_attachment ) , SQL_PREPARED_STMT_QUERY_ATTACHMENT ) = = ERR ) goto ENDE ;
2011-12-28 15:30:53 +01:00
memset ( bind , 0 , sizeof ( bind ) ) ;
bind [ 0 ] . buffer_type = MYSQL_TYPE_STRING ;
bind [ 0 ] . buffer = sdata - > ttmpfile ;
bind [ 0 ] . is_null = 0 ;
len = strlen ( sdata - > ttmpfile ) ; bind [ 0 ] . length = & len ;
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_bind_param ( data - > stmt_query_attachment , bind ) ) {
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-12-28 15:30:53 +01:00
}
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_execute ( data - > stmt_query_attachment ) ) {
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-12-28 15:30:53 +01:00
}
memset ( bind , 0 , sizeof ( bind ) ) ;
bind [ 0 ] . buffer_type = MYSQL_TYPE_LONG ;
bind [ 0 ] . buffer = ( char * ) & id ;
bind [ 0 ] . is_null = & is_null [ 0 ] ;
bind [ 0 ] . length = 0 ;
bind [ 1 ] . buffer_type = MYSQL_TYPE_LONGLONG ;
bind [ 1 ] . buffer = ( char * ) & ptr ;
bind [ 1 ] . is_null = & is_null [ 1 ] ;
bind [ 1 ] . length = 0 ;
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_bind_result ( data - > stmt_query_attachment , bind ) ) {
syslog ( LOG_PRIORITY , " %s: %s.mysql_stmt_bind_result() error: %s " , sdata - > ttmpfile , SQL_METADATA_TABLE , mysql_stmt_error ( data - > stmt_query_attachment ) ) ;
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-12-28 15:30:53 +01:00
}
2013-01-28 12:10:39 +01:00
if ( mysql_stmt_store_result ( data - > stmt_query_attachment ) ) {
syslog ( LOG_PRIORITY , " %s: %s.mysql_stmt_store_result() error: %s " , sdata - > ttmpfile , SQL_METADATA_TABLE , mysql_stmt_error ( data - > stmt_query_attachment ) ) ;
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-12-28 15:30:53 +01:00
}
2013-01-28 12:10:39 +01:00
while ( ! mysql_stmt_fetch ( data - > stmt_query_attachment ) ) {
2011-12-28 15:30:53 +01:00
if ( id > 0 & & id < MAX_ATTACHMENTS ) {
if ( ptr > 0 ) {
ptr_arr [ id ] . ptr = ptr ;
2013-01-28 12:10:39 +01:00
rc = query_attachment_pointers ( sdata , data , ptr , & ( ptr_arr [ id ] . piler_id [ 0 ] ) , & ( ptr_arr [ id ] . attachment_id ) , cfg ) ;
2011-12-28 15:30:53 +01:00
if ( ! rc ) {
attachments = - 1 ;
2012-01-29 09:02:21 +01:00
goto CLOSE ;
2011-12-28 15:30:53 +01:00
}
}
else {
snprintf ( ptr_arr [ id ] . piler_id , sizeof ( ptr_arr [ id ] . piler_id ) - 1 , " %s " , sdata - > ttmpfile ) ;
ptr_arr [ id ] . attachment_id = id ;
}
attachments + + ;
}
}
2012-01-29 09:02:21 +01:00
CLOSE :
2013-01-28 12:10:39 +01:00
mysql_stmt_close ( data - > stmt_query_attachment ) ;
2011-12-28 15:30:53 +01:00
ENDE :
return attachments ;
}