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>
|
|
|
|
|
|
|
|
|
2015-11-21 23:06:47 +01:00
|
|
|
int store_attachments(struct session_data *sdata, struct parser_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-23 12:24:21 +01:00
|
|
|
|
|
|
|
|
2015-07-24 14:28:31 +02:00
|
|
|
if(prepare_sql_statement(sdata, &(data->stmt_insert_into_attachment_table), SQL_PREPARED_STMT_INSERT_INTO_ATTACHMENT_TABLE, cfg) == ERR) return rc;
|
|
|
|
if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_id_by_signature), SQL_PREPARED_STMT_GET_ATTACHMENT_ID_BY_SIGNATURE, cfg) == 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-05-05 11:57:14 +02:00
|
|
|
p_bind_init(data);
|
2011-11-19 21:25:44 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
data->sql[data->pos] = state->attachments[i].digest; data->type[data->pos] = TYPE_STRING; data->pos++;
|
2014-09-10 14:55:07 +02:00
|
|
|
data->sql[data->pos] = (char *)&(state->attachments[i].size); data->type[data->pos] = TYPE_LONG; data->pos++;
|
2011-11-19 21:25:44 +01:00
|
|
|
|
2015-12-18 22:38:52 +01:00
|
|
|
if(p_exec_query(sdata, data->stmt_get_attachment_id_by_signature, data) == OK){
|
2013-01-28 12:10:39 +01:00
|
|
|
|
2015-12-18 22:38:52 +01:00
|
|
|
p_bind_init(data);
|
2013-01-28 12:10:39 +01:00
|
|
|
|
2015-12-18 22:38:52 +01:00
|
|
|
data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
|
2013-01-28 12:10:39 +01:00
|
|
|
|
2015-12-18 22:38:52 +01:00
|
|
|
p_store_results(sdata, data->stmt_get_attachment_id_by_signature, data);
|
|
|
|
if(p_fetch_results(data->stmt_get_attachment_id_by_signature) == OK) found = 1;
|
|
|
|
p_free_results(data->stmt_get_attachment_id_by_signature);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
p_bind_init(data);
|
2011-11-23 12:24:21 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->pos++;
|
|
|
|
data->sql[data->pos] = (char *)&i; data->type[data->pos] = TYPE_LONG; data->pos++;
|
|
|
|
data->sql[data->pos] = state->attachments[i].digest; data->type[data->pos] = TYPE_STRING; data->pos++;
|
|
|
|
data->sql[data->pos] = state->attachments[i].filename; data->type[data->pos] = TYPE_STRING; data->pos++;
|
|
|
|
data->sql[data->pos] = state->attachments[i].type; data->type[data->pos] = TYPE_STRING; data->pos++;
|
|
|
|
data->sql[data->pos] = (char *)&(state->attachments[i].size); data->type[data->pos] = TYPE_LONG; data->pos++;
|
|
|
|
data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->pos++;
|
2011-11-23 12:24:21 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
if(p_exec_query(sdata, data->stmt_insert_into_attachment_table, data) == ERR) goto CLOSE;
|
2011-11-28 14:21:14 +01:00
|
|
|
|
2011-11-23 12:24:21 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
affected_rows = p_get_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-05-05 11:57:14 +02:00
|
|
|
close_prepared_statement(data->stmt_insert_into_attachment_table);
|
|
|
|
close_prepared_statement(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;
|
|
|
|
|
2015-07-24 14:28:31 +02:00
|
|
|
if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_pointer), SQL_PREPARED_STMT_GET_ATTACHMENT_POINTER, cfg) == ERR) return rc;
|
2011-12-28 15:30:53 +01:00
|
|
|
|
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
p_bind_init(data);
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
data->sql[data->pos] = (char *)&ptr; data->type[data->pos] = TYPE_LONGLONG; data->pos++;
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2015-12-18 22:38:52 +01:00
|
|
|
if(p_exec_query(sdata, data->stmt_get_attachment_pointer, data) == OK){
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2015-12-18 22:38:52 +01:00
|
|
|
p_bind_init(data);
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2015-12-18 22:38:52 +01:00
|
|
|
data->sql[data->pos] = piler_id; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = RND_STR_LEN; data->pos++;
|
|
|
|
data->sql[data->pos] = (char *)id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++;
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2015-12-18 22:38:52 +01:00
|
|
|
p_store_results(sdata, data->stmt_get_attachment_pointer, data);
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2015-12-18 22:38:52 +01:00
|
|
|
if(p_fetch_results(data->stmt_get_attachment_pointer) == OK) rc = 1;
|
|
|
|
p_free_results(data->stmt_get_attachment_pointer);
|
|
|
|
}
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
close_prepared_statement(data->stmt_get_attachment_pointer);
|
2011-12-28 15:30:53 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
for(i=0; i<MAX_ATTACHMENTS; i++) memset((char*)&ptr_arr[i], 0, sizeof(struct ptr_array));
|
|
|
|
|
2015-12-18 22:38:52 +01:00
|
|
|
if(prepare_sql_statement(sdata, &(data->stmt_query_attachment), SQL_PREPARED_STMT_QUERY_ATTACHMENT, cfg) == ERR) return attachments;
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
p_bind_init(data);
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->pos++;
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
if(p_exec_query(sdata, data->stmt_query_attachment, data) == ERR) goto CLOSE;
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
p_bind_init(data);
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++;
|
|
|
|
data->sql[data->pos] = (char *)&ptr; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
p_store_results(sdata, data->stmt_query_attachment, data);
|
2011-12-28 15:30:53 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
while(p_fetch_results(data->stmt_query_attachment) == OK){
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
p_free_results(data->stmt_query_attachment);
|
|
|
|
|
2012-01-29 09:02:21 +01:00
|
|
|
CLOSE:
|
2013-05-05 11:57:14 +02:00
|
|
|
close_prepared_statement(data->stmt_query_attachment);
|
2011-12-28 15:30:53 +01:00
|
|
|
|
|
|
|
return attachments;
|
|
|
|
}
|
|
|
|
|
|
|
|
|