initial commit using a very minimalistic db abstraction preparing to add postgresql support as well

This commit is contained in:
SJ 2013-05-05 11:57:14 +02:00
parent aa56565c2e
commit 7a267904e6
18 changed files with 722 additions and 802 deletions

View File

@ -19,12 +19,10 @@
int store_attachments(struct session_data *sdata, struct _state *state, struct __data *data, struct __config *cfg){
uint64 id=0;
int i, rc=1, found, affected_rows;
MYSQL_BIND bind[7];
unsigned long len[7];
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;
if(prepare_sql_statement(sdata, &(data->stmt_insert_into_attachment_table), SQL_PREPARED_STMT_INSERT_INTO_ATTACHMENT_TABLE) == ERR) return rc;
if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_id_by_signature), SQL_PREPARED_STMT_GET_ATTACHMENT_ID_BY_SIGNATURE) == ERR) return rc;
for(i=1; i<=state->n_attachments; i++){
@ -33,43 +31,20 @@ int store_attachments(struct session_data *sdata, struct _state *state, struct _
if(state->attachments[i].size > 0){
memset(bind, 0, sizeof(bind));
p_bind_init(data);
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];
data->sql[data->pos] = state->attachments[i].digest; data->type[data->pos] = TYPE_STRING; data->pos++;
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;
}
if(p_exec_query(sdata, data->stmt_get_attachment_id_by_signature, data) == ERR) goto NOT_FOUND;
memset(bind, 0, sizeof(bind));
p_bind_init(data);
bind[0].buffer_type = MYSQL_TYPE_LONGLONG;
bind[0].buffer = (char *)&id;
bind[0].is_null = 0;
bind[0].length = 0;
data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
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;
}
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;
}
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);
NOT_FOUND:
@ -81,56 +56,20 @@ NOT_FOUND:
}
memset(bind, 0, sizeof(bind));
p_bind_init(data);
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];
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++;
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;
bind[3].buffer = state->attachments[i].filename;
bind[3].is_null = 0;
len[3] = strlen(state->attachments[i].filename); bind[3].length = &len[3];
bind[4].buffer_type = MYSQL_TYPE_STRING;
bind[4].buffer = state->attachments[i].type;
bind[4].is_null = 0;
len[4] = strlen(state->attachments[i].type); bind[4].length = &len[4];
bind[5].buffer_type = MYSQL_TYPE_LONG;
bind[5].buffer = (char *)&(state->attachments[i].size);
bind[5].is_null = 0;
bind[5].length = 0;
bind[6].buffer_type = MYSQL_TYPE_LONGLONG;
bind[6].buffer = (char *)&id;
bind[6].is_null = 0;
bind[6].length = 0;
if(p_exec_query(sdata, data->stmt_insert_into_attachment_table, data) == ERR) goto CLOSE;
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));
goto CLOSE;
}
if(mysql_stmt_execute(data->stmt_insert_into_attachment_table)){
syslog(LOG_PRIORITY, "%s attachment sql error: *%s*", sdata->ttmpfile, mysql_error(&(sdata->mysql)));
goto CLOSE;
}
affected_rows = mysql_stmt_affected_rows(data->stmt_insert_into_attachment_table);
affected_rows = p_get_affected_rows(data->stmt_insert_into_attachment_table);
if(affected_rows != 1){
syslog(LOG_PRIORITY, "%s attachment sql error: affected rows: %d", sdata->ttmpfile, affected_rows);
goto CLOSE;
@ -146,8 +85,8 @@ NOT_FOUND:
rc = 0;
CLOSE:
mysql_stmt_close(data->stmt_insert_into_attachment_table);
mysql_stmt_close(data->stmt_get_attachment_id_by_signature);
close_prepared_statement(data->stmt_insert_into_attachment_table);
close_prepared_statement(data->stmt_get_attachment_id_by_signature);
return rc;
}
@ -155,64 +94,30 @@ CLOSE:
int query_attachment_pointers(struct session_data *sdata, struct __data *data, uint64 ptr, char *piler_id, int *id, struct __config *cfg){
int rc=0;
MYSQL_BIND bind[2];
my_bool is_null[2];
unsigned long len=0;
if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_pointer), SQL_PREPARED_STMT_GET_ATTACHMENT_POINTER) == ERR) return rc;
if(prepare_a_mysql_statement(sdata, &(data->stmt_get_attachment_pointer), SQL_PREPARED_STMT_GET_ATTACHMENT_POINTER) == ERR) goto ENDE;
p_bind_init(data);
memset(bind, 0, sizeof(bind));
data->sql[data->pos] = (char *)&ptr; data->type[data->pos] = TYPE_LONGLONG; data->pos++;
bind[0].buffer_type = MYSQL_TYPE_LONGLONG;
bind[0].buffer = (char *)&ptr;
bind[0].is_null = 0;
len = sizeof(uint64); bind[0].length = &len;
if(p_exec_query(sdata, data->stmt_get_attachment_pointer, data) == ERR) goto ENDE;
if(mysql_stmt_bind_param(data->stmt_get_attachment_pointer, bind)){
goto CLOSE;
}
p_bind_init(data);
if(mysql_stmt_execute(data->stmt_get_attachment_pointer)){
goto CLOSE;
}
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++;
p_store_results(sdata, data->stmt_get_attachment_pointer, data);
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;
if(mysql_stmt_bind_result(data->stmt_get_attachment_pointer, bind)){
goto CLOSE;
}
if(mysql_stmt_store_result(data->stmt_get_attachment_pointer)){
goto CLOSE;
}
if(!mysql_stmt_fetch(data->stmt_get_attachment_pointer)){
if(is_null[0] == 0){
rc = 1;
}
}
CLOSE:
mysql_stmt_close(data->stmt_get_attachment_pointer);
if(p_fetch_results(data->stmt_get_attachment_pointer) == OK) rc = 1;
p_free_results(data->stmt_get_attachment_pointer);
ENDE:
close_prepared_statement(data->stmt_get_attachment_pointer);
return rc;
}
@ -221,59 +126,25 @@ ENDE:
int query_attachments(struct session_data *sdata, struct __data *data, struct ptr_array *ptr_arr, struct __config *cfg){
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));
if(prepare_a_mysql_statement(sdata, &(data->stmt_query_attachment), SQL_PREPARED_STMT_QUERY_ATTACHMENT) == ERR) goto ENDE;
if(prepare_sql_statement(sdata, &(data->stmt_query_attachment), SQL_PREPARED_STMT_QUERY_ATTACHMENT) == ERR) goto ENDE;
p_bind_init(data);
memset(bind, 0, sizeof(bind));
data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->pos++;
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;
if(p_exec_query(sdata, data->stmt_query_attachment, data) == ERR) goto CLOSE;
if(mysql_stmt_bind_param(data->stmt_query_attachment, bind)){
goto CLOSE;
}
p_bind_init(data);
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++;
if(mysql_stmt_execute(data->stmt_query_attachment)){
goto CLOSE;
}
p_store_results(sdata, data->stmt_query_attachment, data);
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;
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));
goto CLOSE;
}
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));
goto CLOSE;
}
while(!mysql_stmt_fetch(data->stmt_query_attachment)){
while(p_fetch_results(data->stmt_query_attachment) == OK){
if(id > 0 && id < MAX_ATTACHMENTS){
if(ptr > 0){
@ -293,8 +164,10 @@ int query_attachments(struct session_data *sdata, struct __data *data, struct pt
}
}
p_free_results(data->stmt_query_attachment);
CLOSE:
mysql_stmt_close(data->stmt_query_attachment);
close_prepared_statement(data->stmt_query_attachment);
ENDE:

View File

@ -14,7 +14,7 @@
#define VERSION "0.1.24-master-branch"
#define BUILD 793
#define BUILD 811
#define HOSTID "mailarchiver"
@ -89,6 +89,7 @@
#define SQL_MESSAGES_VIEW "v_messages"
#define SQL_ATTACHMENTS_VIEW "v_attachment"
#define SQL_PREPARED_STMT_GET_DOMAINS "SELECT `domain` FROM `" SQL_DOMAIN_TABLE "`"
#define SQL_PREPARED_STMT_GET_META_ID_BY_MESSAGE_ID "SELECT id FROM " SQL_METADATA_TABLE " WHERE message_id=?"
#define SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE "INSERT INTO " SQL_RECIPIENT_TABLE " (`id`,`to`,`todomain`) VALUES(?,?,?)"
#define SQL_PREPARED_STMT_INSERT_INTO_SPHINX_TABLE "INSERT INTO " SQL_SPHINX_TABLE " (`id`, `from`, `to`, `fromdomain`, `todomain`, `subject`, `body`, `arrived`, `sent`, `size`, `direction`, `folder`, `attachments`, `attachment_types`) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

View File

@ -11,7 +11,7 @@
#include <piler.h>
struct __counters loadCounters(struct session_data *sdata, struct __config *cfg){
struct __counters load_counters(struct session_data *sdata, struct __data *data, struct __config *cfg){
char buf[SMALLBUFSIZE];
struct __counters counters;
@ -19,26 +19,28 @@ struct __counters loadCounters(struct session_data *sdata, struct __config *cfg)
snprintf(buf, SMALLBUFSIZE-1, "SELECT `rcvd`, `virus`, `duplicate`, `ignore`, `size` FROM `%s`", SQL_COUNTER_TABLE);
#ifdef NEED_MYSQL
MYSQL_RES *res;
MYSQL_ROW row;
if(mysql_real_query(&(sdata->mysql), buf, strlen(buf)) == 0){
res = mysql_store_result(&(sdata->mysql));
if(res != NULL){
row = mysql_fetch_row(res);
if(row){
counters.c_rcvd = strtoull(row[0], NULL, 10);
counters.c_virus = strtoull(row[1], NULL, 10);
counters.c_duplicate = strtoull(row[2], NULL, 10);
counters.c_ignore = strtoull(row[3], NULL, 10);
counters.c_size = strtoull(row[4], NULL, 10);
}
mysql_free_result(res);
}
if(prepare_sql_statement(sdata, &(data->stmt_generic), buf) == ERR) return counters;
p_bind_init(data);
if(p_exec_query(sdata, data->stmt_generic, data) == OK){
p_bind_init(data);
data->sql[data->pos] = (char *)&counters.c_rcvd; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
data->sql[data->pos] = (char *)&counters.c_virus; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
data->sql[data->pos] = (char *)&counters.c_duplicate; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
data->sql[data->pos] = (char *)&counters.c_ignore; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
data->sql[data->pos] = (char *)&counters.c_size; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
p_store_results(sdata, data->stmt_generic, data);
p_fetch_results(data->stmt_generic);
p_free_results(data->stmt_generic);
}
#endif
close_prepared_statement(data->stmt_generic);
return counters;
}
@ -87,14 +89,14 @@ void update_counters(struct session_data *sdata, struct __data *data, struct __c
//if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: update counters: %s", sdata->ttmpfile, buf);
goto EXEC_SQL;
p_query(sdata, buf);
}
}
}
else {
c = loadCounters(sdata, cfg);
c = load_counters(sdata, data, cfg);
snprintf(buf, SMALLBUFSIZE-1, "%ld", sdata->now); memcached_add(&(data->memc), MEMCACHED_COUNTERS_LAST_UPDATE, strlen(MEMCACHED_COUNTERS_LAST_UPDATE), buf, strlen(buf), 0, 0);
@ -109,16 +111,7 @@ void update_counters(struct session_data *sdata, struct __data *data, struct __c
else {
#endif
snprintf(buf, SMALLBUFSIZE-1, "UPDATE `%s` SET `rcvd`=`rcvd`+%llu, `virus`=`virus`+%llu, `duplicate`=`duplicate`+%llu, `ignore`=`ignore`+%llu, `size`=`size`+%llu", SQL_COUNTER_TABLE, counters->c_rcvd, counters->c_virus, counters->c_duplicate, counters->c_ignore, counters->c_size);
//if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: update counters: %s", sdata->ttmpfile, buf);
#ifdef HAVE_MEMCACHED
EXEC_SQL:
#endif
#ifdef NEED_MYSQL
mysql_real_query(&(sdata->mysql), buf, strlen(buf));
#endif
p_query(sdata, buf);
#ifdef HAVE_MEMCACHED
}

View File

@ -55,6 +55,13 @@
#define READY 1
#define BUSY 2
#define MAX_SQL_VARS 20
#define TYPE_UNDEF 0
#define TYPE_TINY 1
#define TYPE_LONG 2
#define TYPE_LONGLONG 3
#define TYPE_STRING 4
#define MAXCHILDREN 64
@ -247,6 +254,7 @@ struct __data {
char mydomains[MAXBUFSIZE];
#ifdef NEED_MYSQL
MYSQL_STMT *stmt_generic;
MYSQL_STMT *stmt_get_meta_id_by_message_id;
MYSQL_STMT *stmt_insert_into_rcpt_table;
MYSQL_STMT *stmt_insert_into_sphinx_table;
@ -258,8 +266,15 @@ struct __data {
MYSQL_STMT *stmt_get_folder_id;
MYSQL_STMT *stmt_insert_into_folder_table;
MYSQL_STMT *stmt_update_metadata_reference;
MYSQL_STMT *stmt_select_from_meta_table;
MYSQL_STMT *stmt_select_non_referenced_attachments;
#endif
char *sql[MAX_SQL_VARS];
int type[MAX_SQL_VARS];
int len[MAX_SQL_VARS];
int pos;
#ifdef HAVE_TRE
struct rule *archiving_rules;
struct rule *retention_rules;

View File

@ -86,14 +86,12 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da
if(rule){
printf("discarding %s by archiving policy: %s\n", filename, rule);
rc = OK;
goto ENDE;
}
else {
make_digests(sdata, cfg);
rc = process_message(sdata, &state, data, cfg);
}
make_digests(sdata, cfg);
rc = process_message(sdata, &state, data, cfg);
ENDE:
unlink(sdata->tmpframe);
if(strcmp(filename, "-") == 0) unlink(sdata->ttmpfile);
@ -123,54 +121,23 @@ ENDE:
unsigned long get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id){
unsigned long id=0;
MYSQL_BIND bind[2];
unsigned long len[2];
if(prepare_a_mysql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_ID) == ERR) goto ENDE;
if(prepare_sql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_ID) == ERR) return id;
memset(bind, 0, sizeof(bind));
p_bind_init(data);
data->sql[data->pos] = foldername; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = (char *)&parent_id; data->type[data->pos] = TYPE_LONG; data->pos++;
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = foldername;
bind[0].is_null = 0;
len[0] = strlen(foldername); bind[0].length = &len[0];
if(p_exec_query(sdata, data->stmt_get_folder_id, data) == OK){
bind[1].buffer_type = MYSQL_TYPE_LONG;
bind[1].buffer = (char *)&parent_id;
bind[1].is_null = 0;
bind[1].length = 0;
data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++;
if(mysql_stmt_bind_param(data->stmt_get_folder_id, bind)){
goto CLOSE;
p_store_results(sdata, data->stmt_get_folder_id, data);
p_fetch_results(data->stmt_get_folder_id);
p_free_results(data->stmt_get_folder_id);
}
if(mysql_stmt_execute(data->stmt_get_folder_id)){
goto CLOSE;
}
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_LONG;
bind[0].buffer = (char *)&id;
bind[0].is_null = 0;
bind[0].length = 0;
if(mysql_stmt_bind_result(data->stmt_get_folder_id, bind)){
goto CLOSE;
}
if(mysql_stmt_store_result(data->stmt_get_folder_id)){
goto CLOSE;
}
mysql_stmt_fetch(data->stmt_get_folder_id);
CLOSE:
mysql_stmt_close(data->stmt_get_folder_id);
ENDE:
close_prepared_statement(data->stmt_get_folder_id);
return id;
}
@ -178,41 +145,18 @@ ENDE:
unsigned long add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id){
unsigned long id=0;
MYSQL_BIND bind[2];
unsigned long len[2];
if(prepare_sql_statement(sdata, &(data->stmt_insert_into_folder_table), SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE) == ERR) return id;
if(prepare_a_mysql_statement(sdata, &(data->stmt_insert_into_folder_table), SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = foldername; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = (char *)&parent_id; data->type[data->pos] = TYPE_LONG; data->pos++;
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = foldername;
bind[0].is_null = 0;
len[0] = strlen(foldername); bind[0].length = &len[0];
bind[1].buffer_type = MYSQL_TYPE_LONG;
bind[1].buffer = (char *)&parent_id;
bind[1].is_null = 0;
bind[1].length = 0;
if(mysql_stmt_bind_param(data->stmt_insert_into_folder_table, bind)){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_bind_param() error: %s", sdata->ttmpfile, SQL_FOLDER_TABLE, mysql_stmt_error(data->stmt_insert_into_folder_table));
goto CLOSE;
if(p_exec_query(sdata, data->stmt_insert_into_folder_table, data) == OK){
id = p_get_insert_id(data->stmt_insert_into_folder_table);
}
if(mysql_stmt_execute(data->stmt_insert_into_folder_table)){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_execute error: *%s*", sdata->ttmpfile, SQL_RECIPIENT_TABLE, mysql_error(&(sdata->mysql)));
goto CLOSE;
}
id = mysql_stmt_insert_id(data->stmt_insert_into_folder_table);
CLOSE:
mysql_stmt_close(data->stmt_insert_into_folder_table);
ENDE:
close_prepared_statement(data->stmt_get_folder_id);
return id;
}

View File

@ -21,14 +21,11 @@ int store_index_data(struct session_data *sdata, struct _state *state, struct __
int rc=ERR;
char *subj;
MYSQL_BIND bind[14];
unsigned long len[14];
subj = state->b_subject;
if(*subj == ' ') subj++;
if(prepare_a_mysql_statement(sdata, &(data->stmt_insert_into_sphinx_table), SQL_PREPARED_STMT_INSERT_INTO_SPHINX_TABLE) == ERR) return rc;
if(prepare_sql_statement(sdata, &(data->stmt_insert_into_sphinx_table), SQL_PREPARED_STMT_INSERT_INTO_SPHINX_TABLE) == ERR) return rc;
fix_email_address_for_sphinx(state->b_from);
@ -37,94 +34,27 @@ int store_index_data(struct session_data *sdata, struct _state *state, struct __
fix_email_address_for_sphinx(state->b_to_domain);
memset(bind, 0, sizeof(bind));
p_bind_init(data);
bind[0].buffer_type = MYSQL_TYPE_LONGLONG;
bind[0].buffer = (char *)&id;
bind[0].is_null = 0;
bind[0].length = 0;
bind[1].buffer_type = MYSQL_TYPE_STRING;
bind[1].buffer = state->b_from;
bind[1].is_null = 0;
len[1] = strlen(state->b_from); bind[1].length = &len[1];
bind[2].buffer_type = MYSQL_TYPE_STRING;
bind[2].buffer = state->b_to;
bind[2].is_null = 0;
len[2] = strlen(state->b_to); bind[2].length = &len[2];
bind[3].buffer_type = MYSQL_TYPE_STRING;
bind[3].buffer = state->b_from_domain;
bind[3].is_null = 0;
len[3] = strlen(state->b_from_domain); bind[3].length = &len[3];
bind[4].buffer_type = MYSQL_TYPE_STRING;
bind[4].buffer = state->b_to_domain;
bind[4].is_null = 0;
len[4] = strlen(state->b_to_domain); bind[4].length = &len[4];
bind[5].buffer_type = MYSQL_TYPE_STRING;
bind[5].buffer = subj;
bind[5].is_null = 0;
len[5] = strlen(subj); bind[5].length = &len[5];
bind[6].buffer_type = MYSQL_TYPE_STRING;
bind[6].buffer = state->b_body;
bind[6].is_null = 0;
len[6] = strlen(state->b_body); bind[6].length = &len[6];
bind[7].buffer_type = MYSQL_TYPE_LONG;
bind[7].buffer = (char *)&sdata->now;
bind[7].is_null = 0;
bind[7].length = 0;
bind[8].buffer_type = MYSQL_TYPE_LONG;
bind[8].buffer = (char *)&sdata->sent;
bind[8].is_null = 0;
bind[8].length = 0;
bind[9].buffer_type = MYSQL_TYPE_LONG;
bind[9].buffer = (char *)&sdata->tot_len;
bind[9].is_null = 0;
bind[9].length = 0;
bind[10].buffer_type = MYSQL_TYPE_LONG;
bind[10].buffer = (char *)&sdata->direction;
bind[10].is_null = 0;
bind[10].length = 0;
bind[11].buffer_type = MYSQL_TYPE_LONG;
bind[11].buffer = (char *)&data->folder;
bind[11].is_null = 0;
bind[11].length = 0;
bind[12].buffer_type = MYSQL_TYPE_LONG;
bind[12].buffer = (char *)&state->n_attachments;
bind[12].is_null = 0;
bind[12].length = 0;
bind[13].buffer_type = MYSQL_TYPE_STRING;
bind[13].buffer = sdata->attachments;
bind[13].is_null = 0;
len[13] = strlen(sdata->attachments); bind[13].length = &len[13];
data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->pos++;
data->sql[data->pos] = state->b_from; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = state->b_to; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = state->b_from_domain; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = state->b_to_domain; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = subj; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = state->b_body; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = (char *)&sdata->now; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&sdata->sent; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&sdata->tot_len; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&sdata->direction; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&data->folder; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&state->n_attachments; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = sdata->attachments; data->type[data->pos] = TYPE_STRING; data->pos++;
if(mysql_stmt_bind_param(data->stmt_insert_into_sphinx_table, bind)){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_bind_param() error: %s", sdata->ttmpfile, SQL_SPHINX_TABLE, mysql_stmt_error(data->stmt_insert_into_sphinx_table));
goto CLOSE;
}
if(p_exec_query(sdata, data->stmt_insert_into_sphinx_table, data) == OK) rc = OK;
if(mysql_stmt_execute(data->stmt_insert_into_sphinx_table)){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_execute error: *%s*", sdata->ttmpfile, SQL_SPHINX_TABLE, mysql_error(&(sdata->mysql)));
goto CLOSE;
}
rc = OK;
CLOSE:
mysql_stmt_close(data->stmt_insert_into_sphinx_table);
close_prepared_statement(data->stmt_insert_into_sphinx_table);
return rc;
@ -132,46 +62,25 @@ CLOSE:
uint64 get_metaid_by_messageid(struct session_data *sdata, struct __data *data, char *message_id, struct __config *cfg){
unsigned long len=0;
uint64 id=0;
MYSQL_BIND bind[1];
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = message_id;
bind[0].is_null = 0;
len = strlen(message_id); bind[0].length = &len;
if(mysql_stmt_bind_param(data->stmt_get_meta_id_by_message_id, bind)){
goto CLOSE;
}
if(mysql_stmt_execute(data->stmt_get_meta_id_by_message_id)){
goto CLOSE;
}
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_meta_id_by_message_id, bind)){
goto CLOSE;
}
p_bind_init(data);
data->sql[data->pos] = message_id; data->type[data->pos] = TYPE_STRING; data->pos++;
if(p_exec_query(sdata, data->stmt_get_meta_id_by_message_id, data) == ERR) goto CLOSE;
if(mysql_stmt_store_result(data->stmt_get_meta_id_by_message_id)){
goto CLOSE;
}
p_bind_init(data);
data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
mysql_stmt_fetch(data->stmt_get_meta_id_by_message_id);
p_store_results(sdata, data->stmt_get_meta_id_by_message_id, data);
p_fetch_results(data->stmt_get_meta_id_by_message_id);
p_free_results(data->stmt_get_meta_id_by_message_id);
CLOSE:
return id;
}
@ -180,10 +89,6 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to,
int ret=OK, n=0;
char *p, *q, puf[SMALLBUFSIZE];
MYSQL_BIND bind[3];
unsigned long len[3];
p = to;
do {
p = split_str(p, " ", puf, sizeof(puf)-1);
@ -193,32 +98,14 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to,
if(q && strlen(q) > 3 && does_it_seem_like_an_email_address(puf) == 1){
q++;
memset(bind, 0, sizeof(bind));
p_bind_init(data);
bind[0].buffer_type = MYSQL_TYPE_LONGLONG;
bind[0].buffer = (char *)&id;
bind[0].is_null = 0;
bind[0].length = 0;
bind[1].buffer_type = MYSQL_TYPE_STRING;
bind[1].buffer = &puf[0];
bind[1].is_null = 0;
len[1] = strlen(puf); bind[1].length = &len[1];
bind[2].buffer_type = MYSQL_TYPE_STRING;
bind[2].buffer = q;
bind[2].is_null = 0;
len[2] = strlen(q); bind[2].length = &len[2];
if(mysql_stmt_bind_param(data->stmt_insert_into_rcpt_table, bind)){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_bind_param() error: %s", sdata->ttmpfile, SQL_RECIPIENT_TABLE, mysql_stmt_error(data->stmt_insert_into_rcpt_table));
ret = ERR;
goto CLOSE;
}
data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->pos++;
data->sql[data->pos] = &puf[0]; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = q; data->type[data->pos] = TYPE_STRING; data->pos++;
if(mysql_stmt_execute(data->stmt_insert_into_rcpt_table) && log_errors == 1){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_execute error: *%s*", sdata->ttmpfile, SQL_RECIPIENT_TABLE, mysql_error(&(sdata->mysql)));
if(p_exec_query(sdata, data->stmt_insert_into_rcpt_table, data) == ERR){
ret = ERR;
}
else n++;
@ -227,7 +114,6 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to,
} while(p);
CLOSE:
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: added %d recipients", sdata->ttmpfile, n);
return ret;
@ -235,33 +121,18 @@ CLOSE:
int update_metadata_reference(struct session_data *sdata, struct _state *state, struct __data *data, char *ref, struct __config *cfg){
int rc, ret = ERR;
MYSQL_BIND bind[2];
unsigned long len[2];
int ret = ERR;
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = ref;
bind[0].is_null = 0;
len[0] = strlen(ref); bind[0].length = &len[0];
p_bind_init(data);
bind[1].buffer_type = MYSQL_TYPE_STRING;
bind[1].buffer = state->reference;
bind[1].is_null = 0;
len[1] = strlen(state->reference); bind[1].length = &len[1];
data->sql[data->pos] = ref; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = state->reference; data->type[data->pos] = TYPE_STRING; data->pos++;
if(mysql_stmt_bind_param(data->stmt_update_metadata_reference, bind)){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_bind_param() error: %s", sdata->ttmpfile, SQL_METADATA_TABLE, mysql_stmt_error(data->stmt_update_metadata_reference));
goto CLOSE;
}
if(p_exec_query(sdata, data->stmt_update_metadata_reference, data) == OK) ret = OK;
rc = mysql_stmt_execute(data->stmt_update_metadata_reference);
if(rc == 0) ret = OK;
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: updated meta reference for '%s', rc=%d", sdata->ttmpfile, state->reference, ret);
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: updated meta reference for '%s', rc=%d", sdata->ttmpfile, state->reference, rc);
CLOSE:
return ret;
}
@ -269,11 +140,7 @@ CLOSE:
int store_meta_data(struct session_data *sdata, struct _state *state, struct __data *data, struct __config *cfg){
int rc, ret=ERR;
char *subj, *p, s[MAXBUFSIZE], s2[SMALLBUFSIZE], vcode[2*DIGEST_LENGTH+1], ref[2*DIGEST_LENGTH+1];
MYSQL_BIND bind[17];
unsigned long len[17];
my_ulonglong id=0;
uint64 id=0;
subj = state->b_subject;
@ -290,7 +157,7 @@ int store_meta_data(struct session_data *sdata, struct _state *state, struct __d
}
if(prepare_a_mysql_statement(sdata, &(data->stmt_insert_into_meta_table), SQL_PREPARED_STMT_INSERT_INTO_META_TABLE) == ERR) return ERR;
if(prepare_sql_statement(sdata, &(data->stmt_insert_into_meta_table), SQL_PREPARED_STMT_INSERT_INTO_META_TABLE) == ERR) return ERR;
memset(s2, 0, sizeof(s2));
@ -309,109 +176,32 @@ int store_meta_data(struct session_data *sdata, struct _state *state, struct __d
snprintf(state->b_to, SMALLBUFSIZE-1, "undisclosed-recipients@no.domain");
}
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = &s2[0];
bind[0].is_null = 0;
len[0] = strlen(s2); bind[0].length = &len[0];
p_bind_init(data);
bind[1].buffer_type = MYSQL_TYPE_STRING;
bind[1].buffer = state->b_from_domain;
bind[1].is_null = 0;
len[1] = strlen(state->b_from_domain); bind[1].length = &len[1];
data->sql[data->pos] = &s2[0]; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = state->b_from_domain; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = subj; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = (char *)&sdata->spam_message; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&sdata->now; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&sdata->sent; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&sdata->retained; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&sdata->tot_len; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&sdata->hdr_len; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&sdata->direction; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = (char *)&state->n_attachments; data->type[data->pos] = TYPE_LONG; data->pos++;
data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = state->message_id; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = &ref[0]; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = sdata->digest; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = sdata->bodydigest; data->type[data->pos] = TYPE_STRING; data->pos++;
data->sql[data->pos] = &vcode[0]; data->type[data->pos] = TYPE_STRING; data->pos++;
bind[2].buffer_type = MYSQL_TYPE_STRING;
bind[2].buffer = subj;
bind[2].is_null = 0;
len[2] = strlen(subj); bind[2].length = &len[2];
bind[3].buffer_type = MYSQL_TYPE_LONG;
bind[3].buffer = (char *)&sdata->spam_message;
bind[3].is_null = 0;
bind[3].length = 0;
bind[4].buffer_type = MYSQL_TYPE_LONG;
bind[4].buffer = (char *)&sdata->now;
bind[4].is_null = 0;
bind[4].length = 0;
bind[5].buffer_type = MYSQL_TYPE_LONG;
bind[5].buffer = (char *)&sdata->sent;
bind[5].is_null = 0;
bind[5].length = 0;
bind[6].buffer_type = MYSQL_TYPE_LONG;
bind[6].buffer = (char *)&sdata->retained;
bind[6].is_null = 0;
bind[6].length = 0;
bind[7].buffer_type = MYSQL_TYPE_LONG;
bind[7].buffer = (char *)&sdata->tot_len;
bind[7].is_null = 0;
bind[7].length = 0;
bind[8].buffer_type = MYSQL_TYPE_LONG;
bind[8].buffer = (char *)&sdata->hdr_len;
bind[8].is_null = 0;
bind[8].length = 0;
bind[9].buffer_type = MYSQL_TYPE_LONG;
bind[9].buffer = (char *)&sdata->direction;
bind[9].is_null = 0;
bind[9].length = 0;
bind[10].buffer_type = MYSQL_TYPE_LONG;
bind[10].buffer = (char *)&state->n_attachments;
bind[10].is_null = 0;
bind[10].length = 0;
bind[11].buffer_type = MYSQL_TYPE_STRING;
bind[11].buffer = sdata->ttmpfile;
bind[11].is_null = 0;
len[11] = strlen(sdata->ttmpfile); bind[11].length = &len[11];
bind[12].buffer_type = MYSQL_TYPE_STRING;
bind[12].buffer = state->message_id;
bind[12].is_null = 0;
len[12] = strlen(state->message_id); bind[12].length = &len[12];
bind[13].buffer_type = MYSQL_TYPE_STRING;
bind[13].buffer = &ref[0];
bind[13].is_null = 0;
len[13] = strlen(ref); bind[13].length = &len[13];
bind[14].buffer_type = MYSQL_TYPE_STRING;
bind[14].buffer = sdata->digest;
bind[14].is_null = 0;
len[14] = strlen(sdata->digest); bind[14].length = &len[14];
bind[15].buffer_type = MYSQL_TYPE_STRING;
bind[15].buffer = sdata->bodydigest;
bind[15].is_null = 0;
len[15] = strlen(sdata->bodydigest); bind[15].length = &len[15];
bind[16].buffer_type = MYSQL_TYPE_STRING;
bind[16].buffer = &vcode[0];
bind[16].is_null = 0;
len[16] = strlen(vcode); bind[16].length = &len[16];
if(mysql_stmt_bind_param(data->stmt_insert_into_meta_table, bind)){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_bind_param() error: %s", sdata->ttmpfile, SQL_METADATA_TABLE, mysql_stmt_error(data->stmt_insert_into_meta_table));
goto CLOSE;
}
rc = mysql_stmt_execute(data->stmt_insert_into_meta_table);
if(rc){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_execute() error: *%s*", sdata->ttmpfile, SQL_METADATA_TABLE, mysql_error(&(sdata->mysql)));
if(p_exec_query(sdata, data->stmt_insert_into_meta_table, data) == ERR){
ret = ERR_EXISTS;
}
else {
id = mysql_stmt_insert_id(data->stmt_insert_into_meta_table);
id = p_get_insert_id(data->stmt_insert_into_meta_table);
rc = store_recipients(sdata, data, state->b_to, id, 1, cfg);
@ -428,8 +218,7 @@ int store_meta_data(struct session_data *sdata, struct _state *state, struct __d
}
}
CLOSE:
mysql_stmt_close(data->stmt_insert_into_meta_table);
close_prepared_statement(data->stmt_insert_into_meta_table);
return ret;
}

View File

@ -13,18 +13,27 @@
void load_mydomains(struct session_data *sdata, struct __data *data, struct __config *cfg){
int clen=0, len=0, size=sizeof(data->mydomains);
char s[SMALLBUFSIZE];
MYSQL_RES *res;
MYSQL_ROW row;
memset(data->mydomains, 0, size);
memset(s, 0, sizeof(s));
snprintf(s, sizeof(s)-1, "SELECT `domain` FROM `%s`", SQL_DOMAIN_TABLE);
if(mysql_real_query(&(sdata->mysql), s, strlen(s)) == 0){
res = mysql_store_result(&(sdata->mysql));
if(res != NULL){
while((row = mysql_fetch_row(res))){
snprintf(s, sizeof(s)-1, "%s,", (char*)row[0]);
if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_PREPARED_STMT_GET_DOMAINS) == ERR) return;
p_bind_init(data);
if(p_exec_query(sdata, data->stmt_generic, data) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = &s[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(s)-2; data->pos++;
p_store_results(sdata, data->stmt_generic, data);
while(p_fetch_results(data->stmt_generic) == OK){
len = strlen(s);
if(clen + len + 1 < size){
@ -32,12 +41,14 @@ void load_mydomains(struct session_data *sdata, struct __data *data, struct __co
clen += len;
}
else break;
memset(s, 0, sizeof(s));
}
mysql_free_result(res);
}
p_free_results(data->stmt_generic);
}
ENDE:
close_prepared_statement(data->stmt_generic);
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "mydomains: '%s'", data->mydomains);
}

View File

@ -34,7 +34,181 @@ void close_database(struct session_data *sdata){
}
int prepare_a_mysql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s){
void p_bind_init(struct __data *data){
int i;
data->pos = 0;
for(i=0; i<MAX_SQL_VARS; i++){
data->sql[i] = NULL;
data->type[i] = TYPE_UNDEF;
data->len[i] = 0;
}
}
void p_query(struct session_data *sdata, char *s){
mysql_real_query(&(sdata->mysql), s, strlen(s));
}
int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct __data *data){
MYSQL_BIND bind[MAX_SQL_VARS];
unsigned long length[MAX_SQL_VARS];
int i, ret=ERR;
memset(bind, 0, sizeof(bind));
for(i=0; i<MAX_SQL_VARS; i++){
if(data->type[i] > TYPE_UNDEF){
switch(data->type[i]) {
case TYPE_TINY:
bind[i].buffer_type = MYSQL_TYPE_TINY;
bind[i].length = 0;
break;
case TYPE_LONG:
bind[i].buffer_type = MYSQL_TYPE_LONG;
bind[i].length = 0;
break;
case TYPE_LONGLONG:
bind[i].buffer_type = MYSQL_TYPE_LONGLONG;
bind[i].length = 0;
break;
case TYPE_STRING:
bind[i].buffer_type = MYSQL_TYPE_STRING;
length[i] = strlen(data->sql[i]);
bind[i].length = &length[i];
break;
default:
bind[i].buffer_type = 0;
bind[i].length = 0;
break;
};
bind[i].buffer = data->sql[i];
bind[i].is_null = 0;
}
else { break; }
}
if(mysql_stmt_bind_param(stmt, bind)){
syslog(LOG_PRIORITY, "%s: mysql_stmt_bind_param() error: %s", sdata->ttmpfile, mysql_stmt_error(stmt));
goto CLOSE;
}
if(mysql_stmt_execute(stmt)){
syslog(LOG_PRIORITY, "%s: mysql_stmt_execute error: *%s*", sdata->ttmpfile, mysql_error(&(sdata->mysql)));
goto CLOSE;
}
ret = OK;
CLOSE:
return ret;
}
int p_store_results(struct session_data *sdata, MYSQL_STMT *stmt, struct __data *data){
MYSQL_BIND bind[MAX_SQL_VARS];
my_bool is_null[MAX_SQL_VARS];
unsigned long length[MAX_SQL_VARS];
my_bool error[MAX_SQL_VARS];
int i, ret=ERR;
memset(bind, 0, sizeof(bind));
for(i=0; i<MAX_SQL_VARS; i++){
if(data->type[i] > TYPE_UNDEF){
switch(data->type[i]) {
case TYPE_TINY: bind[i].buffer_type = MYSQL_TYPE_TINY;
break;
case TYPE_LONG: bind[i].buffer_type = MYSQL_TYPE_LONG;
break;
case TYPE_LONGLONG:
bind[i].buffer_type = MYSQL_TYPE_LONGLONG;
break;
case TYPE_STRING:
bind[i].buffer_type = MYSQL_TYPE_STRING;
bind[i].buffer_length = data->len[i];
break;
default:
bind[i].buffer_type = 0;
break;
};
bind[i].buffer = (char *)data->sql[i];
bind[i].is_null = &is_null[i];
bind[i].length = &length[i];
bind[i].error = &error[i];
}
else { break; }
}
if(mysql_stmt_bind_result(stmt, bind)){
goto CLOSE;
}
if(mysql_stmt_store_result(stmt)){
goto CLOSE;
}
ret = OK;
CLOSE:
return ret;
}
int p_fetch_results(MYSQL_STMT *stmt){
if(mysql_stmt_fetch(stmt) == 0) return OK;
return ERR;
}
void p_free_results(MYSQL_STMT *stmt){
mysql_stmt_free_result(stmt);
}
uint64 p_get_insert_id(MYSQL_STMT *stmt){
return mysql_stmt_insert_id(stmt);
}
int p_get_affected_rows(MYSQL_STMT *stmt){
return mysql_stmt_affected_rows(stmt);
}
int prepare_sql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s){
*stmt = mysql_stmt_init(&(sdata->mysql));
if(!*stmt){
@ -68,14 +242,19 @@ int create_prepared_statements(struct session_data *sdata, struct __data *data){
data->stmt_insert_into_rcpt_table = NULL;
data->stmt_update_metadata_reference = NULL;
if(prepare_a_mysql_statement(sdata, &(data->stmt_get_meta_id_by_message_id), SQL_PREPARED_STMT_GET_META_ID_BY_MESSAGE_ID) == ERR) rc = ERR;
if(prepare_a_mysql_statement(sdata, &(data->stmt_insert_into_rcpt_table), SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE) == ERR) rc = ERR;
if(prepare_a_mysql_statement(sdata, &(data->stmt_update_metadata_reference), SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE) == ERR) rc = ERR;
if(prepare_sql_statement(sdata, &(data->stmt_get_meta_id_by_message_id), SQL_PREPARED_STMT_GET_META_ID_BY_MESSAGE_ID) == ERR) rc = ERR;
if(prepare_sql_statement(sdata, &(data->stmt_insert_into_rcpt_table), SQL_PREPARED_STMT_INSERT_INTO_RCPT_TABLE) == ERR) rc = ERR;
if(prepare_sql_statement(sdata, &(data->stmt_update_metadata_reference), SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE) == ERR) rc = ERR;
return rc;
}
void close_prepared_statement(MYSQL_STMT *stmt){
if(stmt) mysql_stmt_close(stmt);
}
void close_prepared_statements(struct __data *data){
if(data->stmt_get_meta_id_by_message_id) mysql_stmt_close(data->stmt_get_meta_id_by_message_id);
if(data->stmt_insert_into_rcpt_table) mysql_stmt_close(data->stmt_insert_into_rcpt_table);

View File

@ -322,8 +322,8 @@ void initialise_configuration(){
return;
}
load_rules(&sdata, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &(data.retention_rules), SQL_RETENTION_RULE_TABLE);
load_rules(&sdata, &data, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &data, &(data.retention_rules), SQL_RETENTION_RULE_TABLE);
load_mydomains(&sdata, &data, &cfg);

View File

@ -18,6 +18,7 @@
#include <sig.h>
#include <av.h>
#include <rules.h>
#include <sql.h>
#include <config.h>
#include <unistd.h>
@ -53,10 +54,6 @@ void update_counters(struct session_data *sdata, struct __data *data, struct __c
int retrieve_email_from_archive(struct session_data *sdata, struct __data *data, FILE *dest, struct __config *cfg);
int file_from_archive_to_network(char *filename, int sd, int tls_enable, struct __data *data, struct __config *cfg);
int open_database(struct session_data *sdata, struct __config *cfg);
void close_database(struct session_data *sdata);
int prepare_a_mysql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s);
int import_message(char *filename, struct session_data *sdata, struct __data *data, struct __config *cfg);
unsigned long get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id);
unsigned long add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id);
@ -69,8 +66,5 @@ int retrieve_file_from_archive(char *filename, int mode, char **buffer, FILE *de
void load_mydomains(struct session_data *sdata, struct __data *data, struct __config *cfg);
int create_prepared_statements(struct session_data *sdata, struct __data *data);
void close_prepared_statements(struct __data *data);
#endif /* _PILER_H */

View File

@ -136,27 +136,34 @@ int append_string_to_buffer(char **buffer, char *str){
int export_emails_matching_to_query(struct session_data *sdata, struct __data *data, char *s, struct __config *cfg){
MYSQL_RES *res;
MYSQL_ROW row;
FILE *f;
uint64 id, n=0;
char *digest=NULL, *bodydigest=NULL;
char digest[SMALLBUFSIZE], bodydigest[SMALLBUFSIZE];
char filename[SMALLBUFSIZE];
int rc=0;
rc = mysql_real_query(&(sdata->mysql), s, strlen(s));
if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return ERR;
if(rc == 0){
res = mysql_store_result(&(sdata->mysql));
if(res){
while((row = mysql_fetch_row(res))){
id = strtoull(row[0], NULL, 10);
p_bind_init(data);
if(p_exec_query(sdata, data->stmt_generic, data) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = RND_STR_LEN; data->pos++;
data->sql[data->pos] = &digest[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(digest)-2; data->pos++;
data->sql[data->pos] = &bodydigest[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(bodydigest)-2; data->pos++;
p_store_results(sdata, data->stmt_generic, data);
while(p_fetch_results(data->stmt_generic) == OK){
if(id > 0){
snprintf(sdata->ttmpfile, SMALLBUFSIZE-1, "%s", (char*)row[1]);
digest = (char*)row[2];
bodydigest = (char*)row[3];
if(dryrun == 0){
@ -187,11 +194,14 @@ int export_emails_matching_to_query(struct session_data *sdata, struct __data *d
}
}
}
mysql_free_result(res);
}
else rc = 1;
}
p_free_results(data->stmt_generic);
ENDE:
close_prepared_statement(data->stmt_generic);
printf("\n");

View File

@ -555,8 +555,8 @@ int main(int argc, char **argv){
}
load_rules(&sdata, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &(data.retention_rules), SQL_RETENTION_RULE_TABLE);
load_rules(&sdata, &data, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &data, &(data.retention_rules), SQL_RETENTION_RULE_TABLE);
load_mydomains(&sdata, &data, &cfg);

View File

@ -29,22 +29,28 @@ int dryrun = 0;
#define SQL_STMT_DELETE_FROM_ATTACHMENT_TABLE "DELETE FROM `" SQL_ATTACHMENT_TABLE "` WHERE `id` IN ("
int is_purge_allowed(struct session_data *sdata, struct __config *cfg){
int is_purge_allowed(struct session_data *sdata, struct __data *data, struct __config *cfg){
int rc=0;
MYSQL_RES *res;
MYSQL_ROW row;
if(mysql_real_query(&(sdata->mysql), SQL_STMT_SELECT_PURGE_FROM_OPTION_TABLE, strlen(SQL_STMT_SELECT_PURGE_FROM_OPTION_TABLE)) == 0){
res = mysql_store_result(&(sdata->mysql));
if(res){
row = mysql_fetch_row(res);
if(row[0]){
rc = atoi(row[0]);
}
if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_STMT_SELECT_PURGE_FROM_OPTION_TABLE) == ERR) return rc;
mysql_free_result(res);
}
}
p_bind_init(data);
if(p_exec_query(sdata, data->stmt_generic, data) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = (char *)&rc; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++;
p_store_results(sdata, data->stmt_generic, data);
p_fetch_results(data->stmt_generic);
p_free_results(data->stmt_generic);
ENDE:
close_prepared_statement(data->stmt_generic);
return rc;
}
@ -83,7 +89,7 @@ int remove_message_frame_files(char *s, char *update_meta_sql, struct session_da
if(dryrun == 1){
printf("running sql query: *%s*\n\n", update_meta_sql);
} else {
mysql_real_query(&(sdata->mysql), update_meta_sql, strlen(update_meta_sql));
p_query(sdata, update_meta_sql);
}
@ -91,12 +97,11 @@ int remove_message_frame_files(char *s, char *update_meta_sql, struct session_da
}
int remove_attachments(char *in, struct session_data *sdata, struct __config *cfg){
int remove_attachments(char *in, struct session_data *sdata, struct __data *data, struct __config *cfg){
char filename[SMALLBUFSIZE];
char *a, buf[BIGBUFSIZE-300], update_meta_sql[BIGBUFSIZE], delete_attachment_stmt[BIGBUFSIZE];
int n=0, len;
MYSQL_RES *res;
MYSQL_ROW row;
char piler_id[SMALLBUFSIZE], i[BUFLEN];
int n=0, len, attachment_id;
struct stat st;
@ -113,6 +118,8 @@ int remove_attachments(char *in, struct session_data *sdata, struct __config *cf
in[strlen(in)-1] = '\0';
snprintf(a, len-1, "%s%s", SQL_STMT_SELECT_NON_REFERENCED_ATTACHMENTS, in);
if(prepare_sql_statement(sdata, &(data->stmt_select_non_referenced_attachments), a) == ERR){ free(a); return n; }
if(dryrun == 1) printf("running sql query: *%s*\n\n", a);
@ -123,15 +130,24 @@ int remove_attachments(char *in, struct session_data *sdata, struct __config *cf
snprintf(update_meta_sql, sizeof(update_meta_sql)-1, "%s", SQL_STMT_DELETE_FROM_META_TABLE_BY_PILER_ID);
snprintf(delete_attachment_stmt, sizeof(delete_attachment_stmt)-1, "%s", SQL_STMT_DELETE_FROM_ATTACHMENT_TABLE);
if(mysql_real_query(&(sdata->mysql), a, strlen(a)) == 0){
res = mysql_store_result(&(sdata->mysql));
if(res){
while((row = mysql_fetch_row(res))){
if(!row[0]) continue;
snprintf(filename, sizeof(filename)-1, "%s/%02x/%c%c%c/%c%c/%c%c/%s.a%d", cfg->queuedir, cfg->server_id, row[0][8], row[0][9], row[0][10], row[0][RND_STR_LEN-4], row[0][RND_STR_LEN-3], row[0][RND_STR_LEN-2], row[0][RND_STR_LEN-1], row[0], atoi(row[1]));
p_bind_init(data);
if(p_exec_query(sdata, data->stmt_select_non_referenced_attachments, data) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = &piler_id[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(piler_id)-2; data->pos++;
data->sql[data->pos] = (char *)&attachment_id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++;
data->sql[data->pos] = &i[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(i)-2; data->pos++;
p_store_results(sdata, data->stmt_select_non_referenced_attachments, data);
while(p_fetch_results(data->stmt_select_non_referenced_attachments) == OK){
snprintf(filename, sizeof(filename)-1, "%s/%02x/%c%c%c/%c%c/%c%c/%s.a%d", cfg->queuedir, cfg->server_id, piler_id[8], piler_id[9], piler_id[10], piler_id[RND_STR_LEN-4], piler_id[RND_STR_LEN-3], piler_id[RND_STR_LEN-2], piler_id[RND_STR_LEN-1], piler_id, attachment_id);
if(stat(filename, &st)){
snprintf(filename, sizeof(filename)-1, "%s/%02x/%c%c/%c%c/%c%c/%s.a%d", cfg->queuedir, cfg->server_id, row[0][RND_STR_LEN-6], row[0][RND_STR_LEN-5], row[0][RND_STR_LEN-4], row[0][RND_STR_LEN-3], row[0][RND_STR_LEN-2], row[0][RND_STR_LEN-1], row[0], atoi(row[1]));
snprintf(filename, sizeof(filename)-1, "%s/%02x/%c%c/%c%c/%c%c/%s.a%d", cfg->queuedir, cfg->server_id, piler_id[RND_STR_LEN-6], piler_id[RND_STR_LEN-5], piler_id[RND_STR_LEN-4], piler_id[RND_STR_LEN-3], piler_id[RND_STR_LEN-2], piler_id[RND_STR_LEN-1], piler_id, attachment_id);
}
if(dryrun == 1){
@ -141,14 +157,14 @@ int remove_attachments(char *in, struct session_data *sdata, struct __config *cf
}
if(row[2]){
memcpy(&delete_attachment_stmt[strlen(delete_attachment_stmt)], row[2], strlen(row[2]));
if(strlen(i) > 0){
memcpy(&delete_attachment_stmt[strlen(delete_attachment_stmt)], i, strlen(i));
memcpy(&delete_attachment_stmt[strlen(delete_attachment_stmt)], ",", 1);
}
if(atoi(row[1]) == 1){
if(attachment_id == 1){
memcpy(&update_meta_sql[strlen(update_meta_sql)], row[0], strlen(row[0]));
memcpy(&update_meta_sql[strlen(update_meta_sql)], piler_id, strlen(piler_id));
memcpy(&update_meta_sql[strlen(update_meta_sql)], "','", 3);
if(strlen(buf) >= sizeof(buf)-RND_STR_LEN-2-1){
@ -164,7 +180,7 @@ int remove_attachments(char *in, struct session_data *sdata, struct __config *cf
if(dryrun == 1){
printf("delete sql: *%s*\n", delete_attachment_stmt);
} else {
mysql_real_query(&(sdata->mysql), delete_attachment_stmt, strlen(delete_attachment_stmt));
p_query(sdata, delete_attachment_stmt);
}
}
@ -176,17 +192,13 @@ int remove_attachments(char *in, struct session_data *sdata, struct __config *cf
snprintf(delete_attachment_stmt, sizeof(delete_attachment_stmt)-1, "%s", SQL_STMT_DELETE_FROM_ATTACHMENT_TABLE);
}
memcpy(&buf[strlen(buf)], row[0], strlen(row[0]));
memcpy(&buf[strlen(buf)], piler_id, strlen(piler_id));
memcpy(&buf[strlen(buf)], " ", 1);
}
}
mysql_free_result(res);
}
}
free(a);
p_free_results(data->stmt_select_non_referenced_attachments);
if(strlen(buf) > 5 && strlen(update_meta_sql) > strlen(SQL_STMT_DELETE_FROM_META_TABLE_BY_PILER_ID)+10){
@ -201,21 +213,23 @@ int remove_attachments(char *in, struct session_data *sdata, struct __config *cf
if(dryrun == 1){
printf("delete sql: *%s*\n", delete_attachment_stmt);
} else {
mysql_real_query(&(sdata->mysql), delete_attachment_stmt, strlen(delete_attachment_stmt));
p_query(sdata, delete_attachment_stmt);
}
}
ENDE:
free(a);
close_prepared_statement(data->stmt_select_non_referenced_attachments);
return n;
}
int purge_messages_without_attachment(struct session_data *sdata, struct __config *cfg){
int purge_messages_without_attachment(struct session_data *sdata, struct __data *data, struct __config *cfg){
int purged=0;
char s[SMALLBUFSIZE], buf[BIGBUFSIZE-300], update_meta_sql[BIGBUFSIZE];
MYSQL_RES *res;
MYSQL_ROW row;
char id[BUFLEN], s[SMALLBUFSIZE], buf[BIGBUFSIZE-300], update_meta_sql[BIGBUFSIZE];
memset(buf, 0, sizeof(buf));
memset(update_meta_sql, 0, sizeof(update_meta_sql));
@ -226,14 +240,23 @@ int purge_messages_without_attachment(struct session_data *sdata, struct __confi
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "purge sql: *%s*", s);
if(mysql_real_query(&(sdata->mysql), s, strlen(s)) == 0){
res = mysql_store_result(&(sdata->mysql));
if(res){
while((row = mysql_fetch_row(res))){
if(prepare_sql_statement(sdata, &(data->stmt_select_from_meta_table), s) == ERR) return purged;
if((char *)row[0] && (char *)row[1]){
p_bind_init(data);
memcpy(&update_meta_sql[strlen(update_meta_sql)], row[0], strlen(row[0]));
if(p_exec_query(sdata, data->stmt_select_from_meta_table, data) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = &id[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(id)-2; data->pos++;
data->sql[data->pos] = &s[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(s)-2; data->pos++;
p_store_results(sdata, data->stmt_select_from_meta_table, data);
while(p_fetch_results(data->stmt_select_from_meta_table) == OK){
memcpy(&update_meta_sql[strlen(update_meta_sql)], id, strlen(id));
memcpy(&update_meta_sql[strlen(update_meta_sql)], ",", 1);
if(strlen(buf) >= sizeof(buf)-RND_STR_LEN-2-1){
@ -246,30 +269,27 @@ int purge_messages_without_attachment(struct session_data *sdata, struct __confi
snprintf(update_meta_sql, sizeof(update_meta_sql)-1, "%s", SQL_STMT_DELETE_FROM_META_TABLE);
}
memcpy(&buf[strlen(buf)], row[1], strlen(row[1]));
memcpy(&buf[strlen(buf)], s, strlen(s));
memcpy(&buf[strlen(buf)], " ", 1);
}
}
mysql_free_result(res);
}
}
p_free_results(data->stmt_select_from_meta_table);
if(strlen(buf) > 5 && strlen(update_meta_sql) > strlen(SQL_STMT_DELETE_FROM_META_TABLE)+5){
purged += remove_message_frame_files(buf, update_meta_sql, sdata, cfg);
}
ENDE:
close_prepared_statement(data->stmt_select_from_meta_table);
return purged;
}
int purge_messages_with_attachments(struct session_data *sdata, struct __config *cfg){
int purge_messages_with_attachments(struct session_data *sdata, struct __data *data, struct __config *cfg){
int purged=0;
char s[SMALLBUFSIZE], idlist[BIGBUFSIZE];
MYSQL_RES *res;
MYSQL_ROW row;
memset(idlist, 0, sizeof(idlist));
@ -277,29 +297,38 @@ int purge_messages_with_attachments(struct session_data *sdata, struct __config
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "purge sql: *%s*", s);
if(mysql_real_query(&(sdata->mysql), s, strlen(s)) == 0){
res = mysql_store_result(&(sdata->mysql));
if(res){
while((row = mysql_fetch_row(res))){
if((char *)row[0]){
memcpy(&idlist[strlen(idlist)], row[0], strlen(row[0]));
if(prepare_sql_statement(sdata, &(data->stmt_select_from_meta_table), s) == ERR) return purged;
p_bind_init(data);
if(p_exec_query(sdata, data->stmt_select_from_meta_table, data) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = &s[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(s)-2; data->pos++;
p_store_results(sdata, data->stmt_select_from_meta_table, data);
while(p_fetch_results(data->stmt_select_from_meta_table) == OK){
memcpy(&idlist[strlen(idlist)], s, strlen(s));
memcpy(&idlist[strlen(idlist)], "','", 3);
}
if(strlen(idlist) >= sizeof(idlist)-2*RND_STR_LEN){
purged += remove_attachments(idlist, sdata, cfg);
purged += remove_attachments(idlist, sdata, data, cfg);
memset(idlist, 0, sizeof(idlist));
}
}
mysql_free_result(res);
}
}
p_free_results(data->stmt_select_from_meta_table);
if(strlen(idlist) > 5){
purged += remove_attachments(idlist, sdata, cfg);
purged += remove_attachments(idlist, sdata, data, cfg);
}
ENDE:
close_prepared_statement(data->stmt_select_from_meta_table);
return purged;
}
@ -308,6 +337,7 @@ int main(int argc, char **argv){
int i, purged=0;
char *configfile=CONFIG_FILE;
struct session_data sdata;
struct __data data;
struct __config cfg;
@ -343,10 +373,10 @@ int main(int argc, char **argv){
init_session_data(&sdata, &cfg);
i = is_purge_allowed(&sdata, &cfg);
i = is_purge_allowed(&sdata, &data, &cfg);
if(i == 1){
purged += purge_messages_without_attachment(&sdata, &cfg);
purged += purge_messages_with_attachments(&sdata, &cfg);
purged += purge_messages_without_attachment(&sdata, &data, &cfg);
purged += purge_messages_with_attachments(&sdata, &data, &cfg);
printf("purged: %d\n", purged);
}

View File

@ -42,34 +42,39 @@ void p_clean_exit(char *msg, int rc){
}
uint64 get_max_meta_id(struct session_data *sdata){
MYSQL_RES *res;
MYSQL_ROW row;
uint64 get_max_meta_id(struct session_data *sdata, struct __data *data){
char s[SMALLBUFSIZE];
int rc;
uint64 id=0;
snprintf(s, sizeof(s)-1, "SELECT MAX(`id`) FROM %s", SQL_METADATA_TABLE);
rc = mysql_real_query(&(sdata->mysql), s, strlen(s));
if(rc == 0){
res = mysql_store_result(&(sdata->mysql));
if(res){
row = mysql_fetch_row(res);
if(row){
id = strtoull(row[0], NULL, 10);
}
mysql_free_result(res);
}
}
if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return id;
p_bind_init(data);
if(p_exec_query(sdata, data->stmt_generic, data) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
p_store_results(sdata, data->stmt_generic, data);
p_fetch_results(data->stmt_generic);
p_free_results(data->stmt_generic);
ENDE:
close_prepared_statement(data->stmt_generic);
return id;
}
uint64 retrieve_email_by_metadata_id(struct session_data *sdata, struct __data *data, uint64 from_id, uint64 to_id, struct __config *cfg){
MYSQL_RES *res;
MYSQL_ROW row;
FILE *f;
char filename[SMALLBUFSIZE];
char s[SMALLBUFSIZE];
@ -80,16 +85,24 @@ uint64 retrieve_email_by_metadata_id(struct session_data *sdata, struct __data *
snprintf(s, sizeof(s)-1, "SELECT `id`, `piler_id`, `arrived`, `sent` FROM %s WHERE (id BETWEEN %llu AND %llu) AND `deleted`=0", SQL_METADATA_TABLE, from_id, to_id);
rc = mysql_real_query(&(sdata->mysql), s, strlen(s));
if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return reindexed;
if(rc == 0){
res = mysql_store_result(&(sdata->mysql));
if(res){
while((row = mysql_fetch_row(res))){
p_bind_init(data);
if(p_exec_query(sdata, data->stmt_generic, data) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = (char *)&stored_id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = RND_STR_LEN+2; data->pos++;
data->sql[data->pos] = (char *)&(sdata->now); data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++;
data->sql[data->pos] = (char *)&(sdata->sent); data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++;
p_store_results(sdata, data->stmt_generic, data);
while(p_fetch_results(data->stmt_generic) == OK){
stored_id = strtoull(row[0], NULL, 10);
if(stored_id > 0){
snprintf(sdata->ttmpfile, SMALLBUFSIZE-1, "%s", (char*)row[1]);
snprintf(filename, sizeof(filename)-1, "%llu.eml", stored_id);
@ -109,9 +122,6 @@ uint64 retrieve_email_by_metadata_id(struct session_data *sdata, struct __data *
state = parse_message(sdata, 0, data, cfg);
post_parse(sdata, &state, cfg);
sdata->now = strtoul(row[2], NULL, 10);
sdata->sent = strtoul(row[3], NULL, 10);
rc = store_index_data(sdata, &state, data, stored_id, cfg);
if(rc == OK) reindexed++;
@ -124,11 +134,16 @@ uint64 retrieve_email_by_metadata_id(struct session_data *sdata, struct __data *
else printf("cannot open: %s\n", filename);
}
}
mysql_free_result(res);
}
else rc = 1;
}
p_free_results(data->stmt_generic);
ENDE:
close_prepared_statement(data->stmt_generic);
if(progressbar) printf("\n");
@ -224,7 +239,7 @@ int main(int argc, char **argv){
if(all == 1){
from_id = 1;
to_id = get_max_meta_id(&sdata);
to_id = get_max_meta_id(&sdata, &data);
}
n = retrieve_email_by_metadata_id(&sdata, &data, from_id, to_id, &cfg);

View File

@ -10,24 +10,64 @@
#include "rules.h"
void load_rules(struct session_data *sdata, struct rule **rules, char *table){
void load_rules(struct session_data *sdata, struct __data *data, struct rule **rules, char *table){
char s[SMALLBUFSIZE];
MYSQL_RES *res;
MYSQL_ROW row;
char from[SMALLBUFSIZE], to[SMALLBUFSIZE], subject[SMALLBUFSIZE], _size[SMALLBUFSIZE], attachment_type[SMALLBUFSIZE], _attachment_size[SMALLBUFSIZE];
int size=0, attachment_size=0, spam=0, days=0;
memset(from, 0, sizeof(from));
memset(to, 0, sizeof(to));
memset(subject, 0, sizeof(subject));
memset(_size, 0, sizeof(_size));
memset(attachment_type, 0, sizeof(attachment_type));
memset(_attachment_size, 0, sizeof(_attachment_size));
snprintf(s, sizeof(s)-1, "SELECT `from`, `to`, `subject`, `_size`, `size`, `attachment_type`, `_attachment_size`, `attachment_size`, `spam`, `days` FROM `%s`", table);
if(mysql_real_query(&(sdata->mysql), s, strlen(s)) == 0){
res = mysql_store_result(&(sdata->mysql));
if(res != NULL){
while((row = mysql_fetch_row(res))){
append_rule(rules, (char*)row[0], (char*)row[1], (char*)row[2], (char*)row[3], atoi(row[4]), (char*)row[5], (char*)row[6], atoi(row[7]), atoi(row[8]), atoi(row[9]));
if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return;
p_bind_init(data);
if(p_exec_query(sdata, data->stmt_generic, data) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = &from[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(from)-2; data->pos++;
data->sql[data->pos] = &to[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(to)-2; data->pos++;
data->sql[data->pos] = &subject[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(subject)-2; data->pos++;
data->sql[data->pos] = &_size[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(_size)-2; data->pos++;
data->sql[data->pos] = (char *)&size; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(size); data->pos++;
data->sql[data->pos] = &attachment_type[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(attachment_type)-2; data->pos++;
data->sql[data->pos] = &_attachment_size[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(_attachment_size)-2; data->pos++;
data->sql[data->pos] = (char *)&attachment_size; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(attachment_size); data->pos++;
data->sql[data->pos] = (char *)&spam; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(spam); data->pos++;
data->sql[data->pos] = (char *)&days; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(days); data->pos++;
p_store_results(sdata, data->stmt_generic, data);
while(p_fetch_results(data->stmt_generic) == OK){
append_rule(rules, from, to, subject, _size, size, attachment_type, _attachment_size, attachment_size, spam, days);
memset(from, 0, sizeof(from));
memset(to, 0, sizeof(to));
memset(subject, 0, sizeof(subject));
memset(_size, 0, sizeof(_size));
memset(attachment_type, 0, sizeof(attachment_type));
memset(_attachment_size, 0, sizeof(_attachment_size));
size=0, attachment_size=0, spam=0, days=0;
}
mysql_free_result(res);
}
p_free_results(data->stmt_generic);
}
ENDE:
close_prepared_statement(data->stmt_generic);
}

View File

@ -7,7 +7,7 @@
#include "defs.h"
void load_rules(struct session_data *sdata, struct rule **rules, char *table);
void load_rules(struct session_data *sdata, struct __data *data, struct rule **rules, char *table);
int append_rule(struct rule **rule, char *from, char *to, char *subject, char *_size, int size, char *attachment_type, char *_attachment_size, int attachment_size, int spam, int days);
struct rule *create_rule_item(char *from, char *to, char *subject, char *_size, int size, char *attachment_type, char *_attachment_size, int attachment_size, int spam, int days);
char *check_againt_ruleset(struct rule *rule, struct _state *state, int size, int spam);

26
src/sql.h Normal file
View File

@ -0,0 +1,26 @@
/*
* sql.h, SJ
*/
#ifndef _SQL_H
#define _SQL_H
int open_database(struct session_data *sdata, struct __config *cfg);
void close_database(struct session_data *sdata);
int prepare_sql_statement(struct session_data *sdata, MYSQL_STMT **stmt, char *s);
void p_query(struct session_data *sdata, char *s);
int p_exec_query(struct session_data *sdata, MYSQL_STMT *stmt, struct __data *data);
int p_store_results(struct session_data *sdata, MYSQL_STMT *stmt, struct __data *data);
int p_fetch_results(MYSQL_STMT *stmt);
void p_free_results(MYSQL_STMT *stmt);
void p_bind_init(struct __data *data);
uint64 p_get_insert_id(MYSQL_STMT *stmt);
int p_get_affected_rows(MYSQL_STMT *stmt);
void close_prepared_statement(MYSQL_STMT *stmt);
int create_prepared_statements(struct session_data *sdata, struct __data *data);
void close_prepared_statements(struct __data *data);
#endif /* _PILER_H */

View File

@ -49,8 +49,8 @@ int main(int argc, char **argv){
data.archiving_rules = NULL;
data.retention_rules = NULL;
load_rules(&sdata, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &(data.retention_rules), SQL_RETENTION_RULE_TABLE);
load_rules(&sdata, &data, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &data, &(data.retention_rules), SQL_RETENTION_RULE_TABLE);
load_mydomains(&sdata, &data, &cfg);