dbfix merge

This commit is contained in:
SJ 2013-05-16 12:21:36 +02:00
commit 43b72c3e57
23 changed files with 729 additions and 860 deletions

View File

@ -19,12 +19,10 @@
int store_attachments(struct session_data *sdata, struct _state *state, struct __data *data, struct __config *cfg){ int store_attachments(struct session_data *sdata, struct _state *state, struct __data *data, struct __config *cfg){
uint64 id=0; uint64 id=0;
int i, rc=1, found, affected_rows; 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_sql_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_get_attachment_id_by_signature), SQL_PREPARED_STMT_GET_ATTACHMENT_ID_BY_SIGNATURE) == ERR) return rc;
for(i=1; i<=state->n_attachments; i++){ 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){ if(state->attachments[i].size > 0){
memset(bind, 0, sizeof(bind)); p_bind_init(data);
bind[0].buffer_type = MYSQL_TYPE_STRING; data->sql[data->pos] = state->attachments[i].digest; data->type[data->pos] = TYPE_STRING; data->pos++;
bind[0].buffer = state->attachments[i].digest;
bind[0].is_null = 0;
len[0] = strlen(state->attachments[i].digest); bind[0].length = &len[0];
if(mysql_stmt_bind_param(data->stmt_get_attachment_id_by_signature, bind)){ if(p_exec_query(sdata, data->stmt_get_attachment_id_by_signature, data) == ERR) goto NOT_FOUND;
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)); p_bind_init(data);
bind[0].buffer_type = MYSQL_TYPE_LONGLONG; data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->len[data->pos] = sizeof(uint64); data->pos++;
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)){ p_store_results(sdata, data->stmt_get_attachment_id_by_signature, data);
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)); if(p_fetch_results(data->stmt_get_attachment_id_by_signature) == OK) found = 1;
goto NOT_FOUND; p_free_results(data->stmt_get_attachment_id_by_signature);
}
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: NOT_FOUND:
@ -81,56 +56,20 @@ NOT_FOUND:
} }
memset(bind, 0, sizeof(bind)); p_bind_init(data);
bind[0].buffer_type = MYSQL_TYPE_STRING; data->sql[data->pos] = sdata->ttmpfile; data->type[data->pos] = TYPE_STRING; data->pos++;
bind[0].buffer = sdata->ttmpfile; data->sql[data->pos] = (char *)&i; data->type[data->pos] = TYPE_LONG; data->pos++;
bind[0].is_null = 0; data->sql[data->pos] = state->attachments[i].digest; data->type[data->pos] = TYPE_STRING; data->pos++;
len[0] = strlen(sdata->ttmpfile); bind[0].length = &len[0]; 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; if(p_exec_query(sdata, data->stmt_insert_into_attachment_table, data) == ERR) goto CLOSE;
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(mysql_stmt_bind_param(data->stmt_insert_into_attachment_table, bind)){ affected_rows = p_get_affected_rows(data->stmt_insert_into_attachment_table);
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);
if(affected_rows != 1){ if(affected_rows != 1){
syslog(LOG_PRIORITY, "%s attachment sql error: affected rows: %d", sdata->ttmpfile, affected_rows); syslog(LOG_PRIORITY, "%s attachment sql error: affected rows: %d", sdata->ttmpfile, affected_rows);
goto CLOSE; goto CLOSE;
@ -146,8 +85,8 @@ NOT_FOUND:
rc = 0; rc = 0;
CLOSE: CLOSE:
mysql_stmt_close(data->stmt_insert_into_attachment_table); close_prepared_statement(data->stmt_insert_into_attachment_table);
mysql_stmt_close(data->stmt_get_attachment_id_by_signature); close_prepared_statement(data->stmt_get_attachment_id_by_signature);
return rc; 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 query_attachment_pointers(struct session_data *sdata, struct __data *data, uint64 ptr, char *piler_id, int *id, struct __config *cfg){
int rc=0; int rc=0;
MYSQL_BIND bind[2];
my_bool is_null[2]; if(prepare_sql_statement(sdata, &(data->stmt_get_attachment_pointer), SQL_PREPARED_STMT_GET_ATTACHMENT_POINTER) == ERR) return rc;
unsigned long len=0;
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; if(p_exec_query(sdata, data->stmt_get_attachment_pointer, data) == ERR) goto ENDE;
bind[0].buffer = (char *)&ptr;
bind[0].is_null = 0;
len = sizeof(uint64); bind[0].length = &len;
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)){ data->sql[data->pos] = piler_id; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = RND_STR_LEN; data->pos++;
goto CLOSE; 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)); if(p_fetch_results(data->stmt_get_attachment_pointer) == OK) rc = 1;
p_free_results(data->stmt_get_attachment_pointer);
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);
ENDE: ENDE:
close_prepared_statement(data->stmt_get_attachment_pointer);
return rc; 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 query_attachments(struct session_data *sdata, struct __data *data, struct ptr_array *ptr_arr, struct __config *cfg){
int i, rc, id, attachments=0; int i, rc, id, attachments=0;
uint64 ptr; 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)); 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; if(p_exec_query(sdata, data->stmt_query_attachment, data) == ERR) goto CLOSE;
bind[0].buffer = sdata->ttmpfile;
bind[0].is_null = 0;
len = strlen(sdata->ttmpfile); bind[0].length = &len;
if(mysql_stmt_bind_param(data->stmt_query_attachment, bind)){ p_bind_init(data);
goto CLOSE;
}
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)){ p_store_results(sdata, data->stmt_query_attachment, data);
goto CLOSE;
}
while(p_fetch_results(data->stmt_query_attachment) == OK){
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)){
if(id > 0 && id < MAX_ATTACHMENTS){ if(id > 0 && id < MAX_ATTACHMENTS){
if(ptr > 0){ 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: CLOSE:
mysql_stmt_close(data->stmt_query_attachment); close_prepared_statement(data->stmt_query_attachment);
ENDE: ENDE:

View File

@ -14,7 +14,7 @@
#define VERSION "0.1.24-master-branch" #define VERSION "0.1.24-master-branch"
#define BUILD 795 #define BUILD 817
#define HOSTID "mailarchiver" #define HOSTID "mailarchiver"
@ -90,6 +90,7 @@
#define SQL_MESSAGES_VIEW "v_messages" #define SQL_MESSAGES_VIEW "v_messages"
#define SQL_ATTACHMENTS_VIEW "v_attachment" #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_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_RCPT_TABLE "INSERT INTO " SQL_RECIPIENT_TABLE " (`id`,`to`,`todomain`) VALUES(?,?,?)"

View File

@ -11,7 +11,7 @@
#include <piler.h> #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]; char buf[SMALLBUFSIZE];
struct __counters counters; 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); 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){ if(prepare_sql_statement(sdata, &(data->stmt_generic), buf) == ERR) return counters;
res = mysql_store_result(&(sdata->mysql));
if(res != NULL){
row = mysql_fetch_row(res); p_bind_init(data);
if(row){
counters.c_rcvd = strtoull(row[0], NULL, 10); if(p_exec_query(sdata, data->stmt_generic, data) == OK){
counters.c_virus = strtoull(row[1], NULL, 10);
counters.c_duplicate = strtoull(row[2], NULL, 10); p_bind_init(data);
counters.c_ignore = strtoull(row[3], NULL, 10);
counters.c_size = strtoull(row[4], NULL, 10); 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++;
mysql_free_result(res); 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; 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); //if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: update counters: %s", sdata->ttmpfile, buf);
goto EXEC_SQL; p_query(sdata, buf);
} }
} }
} }
else { 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); 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 { else {
#endif #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); 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);
p_query(sdata, buf);
//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
#ifdef HAVE_MEMCACHED #ifdef HAVE_MEMCACHED
} }

View File

@ -55,6 +55,13 @@
#define READY 1 #define READY 1
#define BUSY 2 #define BUSY 2
#define MAX_SQL_VARS 20
#define TYPE_UNDEF 0
#define TYPE_SHORT 1
#define TYPE_LONG 2
#define TYPE_LONGLONG 3
#define TYPE_STRING 4
#define MAXCHILDREN 64 #define MAXCHILDREN 64
@ -180,8 +187,8 @@ struct session_data {
char acceptbuf[SMALLBUFSIZE]; char acceptbuf[SMALLBUFSIZE];
char attachments[SMALLBUFSIZE]; char attachments[SMALLBUFSIZE];
char internal_sender, internal_recipient, external_recipient; char internal_sender, internal_recipient, external_recipient;
int direction;
short int customer_id; short int customer_id;
int direction;
int tls; int tls;
int spam_message; int spam_message;
int fd, hdr_len, tot_len, num_of_rcpt_to, rav; int fd, hdr_len, tot_len, num_of_rcpt_to, rav;
@ -248,6 +255,7 @@ struct __data {
char mydomains[MAXBUFSIZE]; char mydomains[MAXBUFSIZE];
#ifdef NEED_MYSQL #ifdef NEED_MYSQL
MYSQL_STMT *stmt_generic;
MYSQL_STMT *stmt_get_meta_id_by_message_id; MYSQL_STMT *stmt_get_meta_id_by_message_id;
MYSQL_STMT *stmt_insert_into_rcpt_table; MYSQL_STMT *stmt_insert_into_rcpt_table;
MYSQL_STMT *stmt_insert_into_sphinx_table; MYSQL_STMT *stmt_insert_into_sphinx_table;
@ -259,8 +267,15 @@ struct __data {
MYSQL_STMT *stmt_get_folder_id; MYSQL_STMT *stmt_get_folder_id;
MYSQL_STMT *stmt_insert_into_folder_table; MYSQL_STMT *stmt_insert_into_folder_table;
MYSQL_STMT *stmt_update_metadata_reference; MYSQL_STMT *stmt_update_metadata_reference;
MYSQL_STMT *stmt_select_from_meta_table;
MYSQL_STMT *stmt_select_non_referenced_attachments;
#endif #endif
char *sql[MAX_SQL_VARS];
int type[MAX_SQL_VARS];
int len[MAX_SQL_VARS];
int pos;
#ifdef HAVE_TRE #ifdef HAVE_TRE
struct rule *archiving_rules; struct rule *archiving_rules;
struct rule *retention_rules; struct rule *retention_rules;

View File

@ -111,7 +111,7 @@ int unzip_file(struct session_data *sdata, struct _state *state, char *filename,
snprintf(extracted_filename, sizeof(extracted_filename)-1, "%s-%d-%d%s", sdata->ttmpfile, *rec, i, p); snprintf(extracted_filename, sizeof(extracted_filename)-1, "%s-%d-%d%s", sdata->ttmpfile, *rec, i, p);
fd = open(extracted_filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR); fd = open(extracted_filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(fd != -1){ if(fd != -1){
zf = zip_fopen_index(z, i, 0); zf = zip_fopen_index(z, i, 0);
if(zf){ if(zf){

View File

@ -86,14 +86,12 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da
if(rule){ if(rule){
printf("discarding %s by archiving policy: %s\n", filename, rule); printf("discarding %s by archiving policy: %s\n", filename, rule);
rc = OK; 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); unlink(sdata->tmpframe);
if(strcmp(filename, "-") == 0) unlink(sdata->ttmpfile); if(strcmp(filename, "-") == 0) unlink(sdata->ttmpfile);
@ -102,7 +100,6 @@ ENDE:
switch(rc) { switch(rc) {
case OK: case OK:
bzero(&counters, sizeof(counters)); bzero(&counters, sizeof(counters));
counters.c_rcvd = 1;
counters.c_size += sdata->tot_len; counters.c_size += sdata->tot_len;
update_counters(sdata, data, &counters, cfg); update_counters(sdata, data, &counters, cfg);
@ -110,11 +107,6 @@ ENDE:
case ERR_EXISTS: case ERR_EXISTS:
rc = OK; rc = OK;
bzero(&counters, sizeof(counters));
counters.c_duplicate = 1;
update_counters(sdata, data, &counters, cfg);
printf("duplicate: %s (id: %s)\n", filename, sdata->ttmpfile); printf("duplicate: %s (id: %s)\n", filename, sdata->ttmpfile);
break; break;
@ -129,54 +121,23 @@ ENDE:
unsigned long get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id){ unsigned long get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id){
unsigned long id=0; 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; if(p_exec_query(sdata, data->stmt_get_folder_id, data) == OK){
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; data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(unsigned long); data->pos++;
bind[1].buffer = (char *)&parent_id;
bind[1].is_null = 0;
bind[1].length = 0;
if(mysql_stmt_bind_param(data->stmt_get_folder_id, bind)){ p_store_results(sdata, data->stmt_get_folder_id, data);
goto CLOSE; p_fetch_results(data->stmt_get_folder_id);
p_free_results(data->stmt_get_folder_id);
} }
close_prepared_statement(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:
return id; return id;
} }
@ -184,41 +145,18 @@ ENDE:
unsigned long add_new_folder(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){
unsigned long id=0; 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)); if(p_exec_query(sdata, data->stmt_insert_into_folder_table, data) == OK){
id = p_get_insert_id(data->stmt_insert_into_folder_table);
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(mysql_stmt_execute(data->stmt_insert_into_folder_table)){ close_prepared_statement(data->stmt_get_folder_id);
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:
return id; return id;
} }

View File

@ -21,14 +21,11 @@ int store_index_data(struct session_data *sdata, struct _state *state, struct __
int rc=ERR; int rc=ERR;
char *subj; char *subj;
MYSQL_BIND bind[15];
unsigned long len[15];
subj = state->b_subject; subj = state->b_subject;
if(*subj == ' ') subj++; 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); fix_email_address_for_sphinx(state->b_from);
@ -37,102 +34,29 @@ int store_index_data(struct session_data *sdata, struct _state *state, struct __
fix_email_address_for_sphinx(state->b_to_domain); 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++;
#ifdef HAVE_MULTITENANCY #ifdef HAVE_MULTITENANCY
bind[14].buffer_type = MYSQL_TYPE_SHORT; data->sql[data->pos] = (char *)&sdata->customer_id; data->type[data->pos] = TYPE_SHORT; data->pos++;
bind[14].buffer = (char *)&sdata->customer_id;
bind[14].is_null = 0;
bind[14].length = 0;
#endif #endif
if(p_exec_query(sdata, data->stmt_insert_into_sphinx_table, data) == OK) rc = OK;
if(mysql_stmt_bind_param(data->stmt_insert_into_sphinx_table, bind)){ close_prepared_statement(data->stmt_insert_into_sphinx_table);
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(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);
return rc; return rc;
@ -140,46 +64,25 @@ CLOSE:
uint64 get_metaid_by_messageid(struct session_data *sdata, struct __data *data, char *message_id, struct __config *cfg){ 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; 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)){ p_bind_init(data);
goto CLOSE; 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)){ p_bind_init(data);
goto CLOSE; 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: CLOSE:
return id; return id;
} }
@ -188,10 +91,6 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to,
int ret=OK, n=0; int ret=OK, n=0;
char *p, *q, puf[SMALLBUFSIZE]; char *p, *q, puf[SMALLBUFSIZE];
MYSQL_BIND bind[3];
unsigned long len[3];
p = to; p = to;
do { do {
p = split_str(p, " ", puf, sizeof(puf)-1); p = split_str(p, " ", puf, sizeof(puf)-1);
@ -201,32 +100,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){ if(q && strlen(q) > 3 && does_it_seem_like_an_email_address(puf) == 1){
q++; q++;
memset(bind, 0, sizeof(bind)); p_bind_init(data);
bind[0].buffer_type = MYSQL_TYPE_LONGLONG; data->sql[data->pos] = (char *)&id; data->type[data->pos] = TYPE_LONGLONG; data->pos++;
bind[0].buffer = (char *)&id; data->sql[data->pos] = &puf[0]; data->type[data->pos] = TYPE_STRING; data->pos++;
bind[0].is_null = 0; data->sql[data->pos] = q; data->type[data->pos] = TYPE_STRING; data->pos++;
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;
}
if(mysql_stmt_execute(data->stmt_insert_into_rcpt_table) && log_errors == 1){ if(p_exec_query(sdata, data->stmt_insert_into_rcpt_table, data) == ERR){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_execute error: *%s*", sdata->ttmpfile, SQL_RECIPIENT_TABLE, mysql_error(&(sdata->mysql)));
ret = ERR; ret = ERR;
} }
else n++; else n++;
@ -235,7 +116,6 @@ int store_recipients(struct session_data *sdata, struct __data *data, char *to,
} while(p); } while(p);
CLOSE:
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: added %d recipients", sdata->ttmpfile, n); if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: added %d recipients", sdata->ttmpfile, n);
return ret; return ret;
@ -243,33 +123,18 @@ CLOSE:
int update_metadata_reference(struct session_data *sdata, struct _state *state, struct __data *data, char *ref, struct __config *cfg){ int update_metadata_reference(struct session_data *sdata, struct _state *state, struct __data *data, char *ref, struct __config *cfg){
int rc, ret = ERR; int ret = ERR;
MYSQL_BIND bind[2];
unsigned long len[2];
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING; p_bind_init(data);
bind[0].buffer = ref;
bind[0].is_null = 0;
len[0] = strlen(ref); bind[0].length = &len[0];
bind[1].buffer_type = MYSQL_TYPE_STRING; data->sql[data->pos] = ref; data->type[data->pos] = TYPE_STRING; data->pos++;
bind[1].buffer = state->reference; data->sql[data->pos] = state->reference; data->type[data->pos] = TYPE_STRING; data->pos++;
bind[1].is_null = 0;
len[1] = strlen(state->reference); bind[1].length = &len[1];
if(mysql_stmt_bind_param(data->stmt_update_metadata_reference, bind)){ if(p_exec_query(sdata, data->stmt_update_metadata_reference, data) == OK) ret = OK;
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;
}
rc = mysql_stmt_execute(data->stmt_update_metadata_reference); if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: updated meta reference for '%s', rc=%d", sdata->ttmpfile, state->reference, ret);
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, rc);
CLOSE:
return ret; return ret;
} }
@ -277,11 +142,7 @@ CLOSE:
int store_meta_data(struct session_data *sdata, struct _state *state, struct __data *data, struct __config *cfg){ int store_meta_data(struct session_data *sdata, struct _state *state, struct __data *data, struct __config *cfg){
int rc, ret=ERR; int rc, ret=ERR;
char *subj, *p, s[MAXBUFSIZE], s2[SMALLBUFSIZE], vcode[2*DIGEST_LENGTH+1], ref[2*DIGEST_LENGTH+1]; char *subj, *p, s[MAXBUFSIZE], s2[SMALLBUFSIZE], vcode[2*DIGEST_LENGTH+1], ref[2*DIGEST_LENGTH+1];
uint64 id=0;
MYSQL_BIND bind[18];
unsigned long len[18];
my_ulonglong id=0;
subj = state->b_subject; subj = state->b_subject;
@ -298,7 +159,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)); memset(s2, 0, sizeof(s2));
@ -317,115 +178,35 @@ int store_meta_data(struct session_data *sdata, struct _state *state, struct __d
snprintf(state->b_to, SMALLBUFSIZE-1, "undisclosed-recipients@no.domain"); snprintf(state->b_to, SMALLBUFSIZE-1, "undisclosed-recipients@no.domain");
} }
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING; p_bind_init(data);
bind[0].buffer = &s2[0];
bind[0].is_null = 0;
len[0] = strlen(s2); bind[0].length = &len[0];
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];
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];
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++;
#ifdef HAVE_MULTITENANCY #ifdef HAVE_MULTITENANCY
bind[17].buffer_type = MYSQL_TYPE_SHORT; data->sql[data->pos] = (char *)&sdata->customer_id; data->type[data->pos] = TYPE_SHORT; data->pos++;
bind[17].buffer = (char *)&sdata->customer_id;
bind[17].is_null = 0;
bind[17].length = 0;
#endif #endif
if(p_exec_query(sdata, data->stmt_insert_into_meta_table, data) == ERR){
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)));
ret = ERR_EXISTS; ret = ERR_EXISTS;
} }
else { 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); rc = store_recipients(sdata, data, state->b_to, id, 1, cfg);
@ -442,8 +223,7 @@ int store_meta_data(struct session_data *sdata, struct _state *state, struct __d
} }
} }
CLOSE: close_prepared_statement(data->stmt_insert_into_meta_table);
mysql_stmt_close(data->stmt_insert_into_meta_table);
return ret; return ret;
} }

View File

@ -13,18 +13,27 @@
void load_mydomains(struct session_data *sdata, struct __data *data, struct __config *cfg){ void load_mydomains(struct session_data *sdata, struct __data *data, struct __config *cfg){
int clen=0, len=0, size=sizeof(data->mydomains); int clen=0, len=0, size=sizeof(data->mydomains);
char s[SMALLBUFSIZE]; char s[SMALLBUFSIZE];
MYSQL_RES *res;
MYSQL_ROW row;
memset(data->mydomains, 0, size); 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){ if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_PREPARED_STMT_GET_DOMAINS) == ERR) return;
res = mysql_store_result(&(sdata->mysql));
if(res != NULL){
while((row = mysql_fetch_row(res))){ p_bind_init(data);
snprintf(s, sizeof(s)-1, "%s,", (char*)row[0]);
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); len = strlen(s);
if(clen + len + 1 < size){ if(clen + len + 1 < size){
@ -32,12 +41,14 @@ void load_mydomains(struct session_data *sdata, struct __data *data, struct __co
clen += len; clen += len;
} }
else break; 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); 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_SHORT:
bind[i].buffer_type = MYSQL_TYPE_SHORT;
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_SHORT: bind[i].buffer_type = MYSQL_TYPE_SHORT;
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)); *stmt = mysql_stmt_init(&(sdata->mysql));
if(!*stmt){ 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_insert_into_rcpt_table = NULL;
data->stmt_update_metadata_reference = 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_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_a_mysql_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_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_update_metadata_reference), SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE) == ERR) rc = ERR;
return rc; return rc;
} }
void close_prepared_statement(MYSQL_STMT *stmt){
if(stmt) mysql_stmt_close(stmt);
}
void close_prepared_statements(struct __data *data){ 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_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); if(data->stmt_insert_into_rcpt_table) mysql_stmt_close(data->stmt_insert_into_rcpt_table);

View File

@ -333,8 +333,8 @@ void initialise_configuration(){
return; return;
} }
load_rules(&sdata, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE); load_rules(&sdata, &data, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &(data.retention_rules), SQL_RETENTION_RULE_TABLE); load_rules(&sdata, &data, &(data.retention_rules), SQL_RETENTION_RULE_TABLE);
load_mydomains(&sdata, &data, &cfg); load_mydomains(&sdata, &data, &cfg);
@ -392,7 +392,6 @@ int main(int argc, char **argv){
#ifdef HAVE_MULTITENANCY #ifdef HAVE_MULTITENANCY
data.customers = NULL; data.customers = NULL;
#endif #endif
data.ctx = NULL; data.ctx = NULL;
data.ssl = NULL; data.ssl = NULL;

View File

@ -18,6 +18,7 @@
#include <sig.h> #include <sig.h>
#include <av.h> #include <av.h>
#include <rules.h> #include <rules.h>
#include <sql.h>
#include <config.h> #include <config.h>
#include <unistd.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 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 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); 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 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); unsigned long add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id);
@ -71,8 +68,5 @@ void load_mydomains(struct session_data *sdata, struct __data *data, struct __co
void load_customers(struct session_data *sdata, struct __data *data, struct __config *cfg); void load_customers(struct session_data *sdata, struct __data *data, struct __config *cfg);
unsigned char get_customer_id_by_rcpt_to_email(char *rcpttoemail, struct __data *data); unsigned char get_customer_id_by_rcpt_to_email(char *rcpttoemail, struct __data *data);
int create_prepared_statements(struct session_data *sdata, struct __data *data);
void close_prepared_statements(struct __data *data);
#endif /* _PILER_H */ #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){ 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; FILE *f;
uint64 id, n=0; uint64 id, n=0;
char *digest=NULL, *bodydigest=NULL; char digest[SMALLBUFSIZE], bodydigest[SMALLBUFSIZE];
char filename[SMALLBUFSIZE]; char filename[SMALLBUFSIZE];
int rc=0; 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){ if(id > 0){
snprintf(sdata->ttmpfile, SMALLBUFSIZE-1, "%s", (char*)row[1]);
digest = (char*)row[2];
bodydigest = (char*)row[3];
if(dryrun == 0){ if(dryrun == 0){
@ -187,11 +194,14 @@ int export_emails_matching_to_query(struct session_data *sdata, struct __data *d
} }
} }
} }
mysql_free_result(res);
} p_free_results(data->stmt_generic);
else rc = 1;
} ENDE:
close_prepared_statement(data->stmt_generic);
printf("\n"); 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, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &(data.retention_rules), SQL_RETENTION_RULE_TABLE); load_rules(&sdata, &data, &(data.retention_rules), SQL_RETENTION_RULE_TABLE);
load_mydomains(&sdata, &data, &cfg); 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 (" #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; 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){ if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_STMT_SELECT_PURGE_FROM_OPTION_TABLE) == ERR) return rc;
res = mysql_store_result(&(sdata->mysql));
if(res){
row = mysql_fetch_row(res);
if(row[0]){
rc = atoi(row[0]);
}
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; return rc;
} }
@ -83,7 +89,7 @@ int remove_message_frame_files(char *s, char *update_meta_sql, struct session_da
if(dryrun == 1){ if(dryrun == 1){
printf("running sql query: *%s*\n\n", update_meta_sql); printf("running sql query: *%s*\n\n", update_meta_sql);
} else { } 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 filename[SMALLBUFSIZE];
char *a, buf[BIGBUFSIZE-300], update_meta_sql[BIGBUFSIZE], delete_attachment_stmt[BIGBUFSIZE]; char *a, buf[BIGBUFSIZE-300], update_meta_sql[BIGBUFSIZE], delete_attachment_stmt[BIGBUFSIZE];
int n=0, len; char piler_id[SMALLBUFSIZE], i[BUFLEN];
MYSQL_RES *res; int n=0, len, attachment_id;
MYSQL_ROW row;
struct stat st; struct stat st;
@ -113,6 +118,8 @@ int remove_attachments(char *in, struct session_data *sdata, struct __config *cf
in[strlen(in)-1] = '\0'; in[strlen(in)-1] = '\0';
snprintf(a, len-1, "%s%s", SQL_STMT_SELECT_NON_REFERENCED_ATTACHMENTS, in); 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); 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(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); 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)){ 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){ if(dryrun == 1){
@ -141,14 +157,14 @@ int remove_attachments(char *in, struct session_data *sdata, struct __config *cf
} }
if(row[2]){ if(strlen(i) > 0){
memcpy(&delete_attachment_stmt[strlen(delete_attachment_stmt)], row[2], strlen(row[2])); memcpy(&delete_attachment_stmt[strlen(delete_attachment_stmt)], i, strlen(i));
memcpy(&delete_attachment_stmt[strlen(delete_attachment_stmt)], ",", 1); 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); memcpy(&update_meta_sql[strlen(update_meta_sql)], "','", 3);
if(strlen(buf) >= sizeof(buf)-RND_STR_LEN-2-1){ 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){ if(dryrun == 1){
printf("delete sql: *%s*\n", delete_attachment_stmt); printf("delete sql: *%s*\n", delete_attachment_stmt);
} else { } 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); 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); 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){ 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){ if(dryrun == 1){
printf("delete sql: *%s*\n", delete_attachment_stmt); printf("delete sql: *%s*\n", delete_attachment_stmt);
} else { } 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; 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; int purged=0;
char s[SMALLBUFSIZE], buf[BIGBUFSIZE-300], update_meta_sql[BIGBUFSIZE]; char id[BUFLEN], s[SMALLBUFSIZE], buf[BIGBUFSIZE-300], update_meta_sql[BIGBUFSIZE];
MYSQL_RES *res;
MYSQL_ROW row;
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
memset(update_meta_sql, 0, sizeof(update_meta_sql)); 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(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "purge sql: *%s*", s);
if(mysql_real_query(&(sdata->mysql), s, strlen(s)) == 0){ if(prepare_sql_statement(sdata, &(data->stmt_select_from_meta_table), s) == ERR) return purged;
res = mysql_store_result(&(sdata->mysql));
if(res){
while((row = mysql_fetch_row(res))){
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); memcpy(&update_meta_sql[strlen(update_meta_sql)], ",", 1);
if(strlen(buf) >= sizeof(buf)-RND_STR_LEN-2-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); 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); memcpy(&buf[strlen(buf)], " ", 1);
} }
} p_free_results(data->stmt_select_from_meta_table);
mysql_free_result(res);
}
}
if(strlen(buf) > 5 && strlen(update_meta_sql) > strlen(SQL_STMT_DELETE_FROM_META_TABLE)+5){ 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); purged += remove_message_frame_files(buf, update_meta_sql, sdata, cfg);
} }
ENDE:
close_prepared_statement(data->stmt_select_from_meta_table);
return purged; 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; int purged=0;
char s[SMALLBUFSIZE], idlist[BIGBUFSIZE]; char s[SMALLBUFSIZE], idlist[BIGBUFSIZE];
MYSQL_RES *res;
MYSQL_ROW row;
memset(idlist, 0, sizeof(idlist)); 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(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "purge sql: *%s*", s);
if(mysql_real_query(&(sdata->mysql), s, strlen(s)) == 0){ if(prepare_sql_statement(sdata, &(data->stmt_select_from_meta_table), s) == ERR) return purged;
res = mysql_store_result(&(sdata->mysql));
if(res){ p_bind_init(data);
while((row = mysql_fetch_row(res))){ if(p_exec_query(sdata, data->stmt_select_from_meta_table, data) == ERR) goto ENDE;
if((char *)row[0]){
memcpy(&idlist[strlen(idlist)], row[0], strlen(row[0]));
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); memcpy(&idlist[strlen(idlist)], "','", 3);
}
if(strlen(idlist) >= sizeof(idlist)-2*RND_STR_LEN){ 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)); memset(idlist, 0, sizeof(idlist));
} }
} }
mysql_free_result(res); p_free_results(data->stmt_select_from_meta_table);
}
}
if(strlen(idlist) > 5){ 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; return purged;
} }
@ -308,6 +337,7 @@ int main(int argc, char **argv){
int i, purged=0; int i, purged=0;
char *configfile=CONFIG_FILE; char *configfile=CONFIG_FILE;
struct session_data sdata; struct session_data sdata;
struct __data data;
struct __config cfg; struct __config cfg;
@ -343,10 +373,10 @@ int main(int argc, char **argv){
init_session_data(&sdata, &cfg); init_session_data(&sdata, &cfg);
i = is_purge_allowed(&sdata, &cfg); i = is_purge_allowed(&sdata, &data, &cfg);
if(i == 1){ if(i == 1){
purged += purge_messages_without_attachment(&sdata, &cfg); purged += purge_messages_without_attachment(&sdata, &data, &cfg);
purged += purge_messages_with_attachments(&sdata, &cfg); purged += purge_messages_with_attachments(&sdata, &data, &cfg);
printf("purged: %d\n", purged); 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){ uint64 get_max_meta_id(struct session_data *sdata, struct __data *data){
MYSQL_RES *res;
MYSQL_ROW row;
char s[SMALLBUFSIZE]; char s[SMALLBUFSIZE];
int rc;
uint64 id=0; uint64 id=0;
snprintf(s, sizeof(s)-1, "SELECT MAX(`id`) FROM %s", SQL_METADATA_TABLE); 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){ if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return id;
res = mysql_store_result(&(sdata->mysql));
if(res){
row = mysql_fetch_row(res); p_bind_init(data);
if(row){
id = strtoull(row[0], NULL, 10); if(p_exec_query(sdata, data->stmt_generic, data) == ERR) goto ENDE;
}
mysql_free_result(res);
}
} 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; return id;
} }
uint64 retrieve_email_by_metadata_id(struct session_data *sdata, struct __data *data, uint64 from_id, uint64 to_id, struct __config *cfg){ 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; FILE *f;
char filename[SMALLBUFSIZE]; char filename[SMALLBUFSIZE];
char s[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); 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){ p_bind_init(data);
res = mysql_store_result(&(sdata->mysql));
if(res){ if(p_exec_query(sdata, data->stmt_generic, data) == ERR) goto ENDE;
while((row = mysql_fetch_row(res))){
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){ if(stored_id > 0){
snprintf(sdata->ttmpfile, SMALLBUFSIZE-1, "%s", (char*)row[1]);
snprintf(filename, sizeof(filename)-1, "%llu.eml", stored_id); 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); state = parse_message(sdata, 0, data, cfg);
post_parse(sdata, &state, 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); rc = store_index_data(sdata, &state, data, stored_id, cfg);
if(rc == OK) reindexed++; 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); 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"); if(progressbar) printf("\n");
@ -224,7 +239,7 @@ int main(int argc, char **argv){
if(all == 1){ if(all == 1){
from_id = 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); n = retrieve_email_by_metadata_id(&sdata, &data, from_id, to_id, &cfg);

View File

@ -10,24 +10,64 @@
#include "rules.h" #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]; char s[SMALLBUFSIZE];
MYSQL_RES *res; char from[SMALLBUFSIZE], to[SMALLBUFSIZE], subject[SMALLBUFSIZE], _size[SMALLBUFSIZE], attachment_type[SMALLBUFSIZE], _attachment_size[SMALLBUFSIZE];
MYSQL_ROW row; 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); 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){ if(prepare_sql_statement(sdata, &(data->stmt_generic), s) == ERR) return;
res = mysql_store_result(&(sdata->mysql));
if(res != NULL){
while((row = mysql_fetch_row(res))){ p_bind_init(data);
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(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" #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); 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); 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); char *check_againt_ruleset(struct rule *rule, struct _state *state, int size, int spam);

View File

@ -414,6 +414,7 @@ AFTER_PERIOD:
if(sdata.customer_id == 0) sdata.customer_id = get_customer_id_by_rcpt_to_email(rcpttoemail, data); if(sdata.customer_id == 0) sdata.customer_id = get_customer_id_by_rcpt_to_email(rcpttoemail, data);
#endif #endif
if(sdata.num_of_rcpt_to < MAX_RCPT_TO-1) sdata.num_of_rcpt_to++; if(sdata.num_of_rcpt_to < MAX_RCPT_TO-1) sdata.num_of_rcpt_to++;

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.archiving_rules = NULL;
data.retention_rules = NULL; data.retention_rules = NULL;
load_rules(&sdata, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE); load_rules(&sdata, &data, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &(data.retention_rules), SQL_RETENTION_RULE_TABLE); load_rules(&sdata, &data, &(data.retention_rules), SQL_RETENTION_RULE_TABLE);
load_mydomains(&sdata, &data, &cfg); load_mydomains(&sdata, &data, &cfg);

View File

@ -422,8 +422,8 @@ int main(int argc, char **argv){
(void) openlog("pilerimport", LOG_PID, LOG_MAIL); (void) openlog("pilerimport", LOG_PID, LOG_MAIL);
load_rules(&sdata, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE); load_rules(&sdata, &data, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &(data.retention_rules), SQL_RETENTION_RULE_TABLE); load_rules(&sdata, &data, &(data.retention_rules), SQL_RETENTION_RULE_TABLE);
load_mydomains(&sdata, &data, &cfg); load_mydomains(&sdata, &data, &cfg);

View File

@ -1,5 +1,4 @@
drop table if exists `sph_counter`;
create table if not exists `sph_counter` ( create table if not exists `sph_counter` (
`counter_id` int not null, `counter_id` int not null,
`max_doc_id` int not null, `max_doc_id` int not null,
@ -7,7 +6,6 @@ create table if not exists `sph_counter` (
); );
drop table if exists `sph_index`;
create table if not exists `sph_index` ( create table if not exists `sph_index` (
`id` bigint not null, `id` bigint not null,
`from` char(255) default null, `from` char(255) default null,
@ -28,7 +26,6 @@ create table if not exists `sph_index` (
) Engine=InnoDB; ) Engine=InnoDB;
drop table if exists `metadata`;
create table if not exists `metadata` ( create table if not exists `metadata` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`from` varchar(128) not null, `from` varchar(128) not null,
@ -63,7 +60,6 @@ create index metadata_idx7 on metadata(`retained`);
create index metadata_idx8 on metadata(`customer`); create index metadata_idx8 on metadata(`customer`);
drop table if exists `rcpt`;
create table if not exists `rcpt` ( create table if not exists `rcpt` (
`id` bigint unsigned not null, `id` bigint unsigned not null,
`to` varchar(128) not null, `to` varchar(128) not null,
@ -79,7 +75,6 @@ drop view if exists `v_messages`;
create view `v_messages` AS select `metadata`.`id` AS `id`,`metadata`.`piler_id` AS `piler_id`,`metadata`.`from` AS `from`,`metadata`.`fromdomain` AS `fromdomain`,`rcpt`.`to` AS `to`,`rcpt`.`todomain` AS `todomain`,`metadata`.`subject` AS `subject`, `metadata`.`size` AS `size`, `metadata`.`direction` AS `direction`, `metadata`.`sent` AS `sent`, `metadata`.`arrived` AS `arrived`, `metadata`.`digest` AS `digest`, `metadata`.`bodydigest` AS `bodydigest` from (`metadata` join `rcpt`) where (`metadata`.`id` = `rcpt`.`id`); create view `v_messages` AS select `metadata`.`id` AS `id`,`metadata`.`piler_id` AS `piler_id`,`metadata`.`from` AS `from`,`metadata`.`fromdomain` AS `fromdomain`,`rcpt`.`to` AS `to`,`rcpt`.`todomain` AS `todomain`,`metadata`.`subject` AS `subject`, `metadata`.`size` AS `size`, `metadata`.`direction` AS `direction`, `metadata`.`sent` AS `sent`, `metadata`.`arrived` AS `arrived`, `metadata`.`digest` AS `digest`, `metadata`.`bodydigest` AS `bodydigest` from (`metadata` join `rcpt`) where (`metadata`.`id` = `rcpt`.`id`);
drop table if exists `attachment`;
create table if not exists `attachment` ( create table if not exists `attachment` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`piler_id` char(36) not null, `piler_id` char(36) not null,
@ -101,7 +96,6 @@ drop view if exists `v_attachment`;
create view `v_attachment` AS select `id` as `i`, `piler_id`, `attachment_id`, `ptr`, (select count(*) from `attachment` where `ptr`=`i`) as `refcount` from `attachment`; create view `v_attachment` AS select `id` as `i`, `piler_id`, `attachment_id`, `ptr`, (select count(*) from `attachment` where `ptr`=`i`) as `refcount` from `attachment`;
drop table if exists `tag`;
create table if not exists `tag` ( create table if not exists `tag` (
`_id` bigint unsigned auto_increment not null, `_id` bigint unsigned auto_increment not null,
`id` bigint not null, `id` bigint not null,
@ -112,7 +106,6 @@ create table if not exists `tag` (
) ENGINE=InnoDB; ) ENGINE=InnoDB;
drop table if exists `archiving_rule`;
create table if not exists `archiving_rule` ( create table if not exists `archiving_rule` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`from` char(128) default null, `from` char(128) default null,
@ -130,7 +123,6 @@ create table if not exists `archiving_rule` (
) ENGINE=InnoDB; ) ENGINE=InnoDB;
drop table if exists `retention_rule`;
create table if not exists `retention_rule` ( create table if not exists `retention_rule` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`from` char(128) default null, `from` char(128) default null,
@ -148,7 +140,6 @@ create table if not exists `retention_rule` (
) ENGINE=InnoDB; ) ENGINE=InnoDB;
drop table if exists `counter`;
create table if not exists `counter` ( create table if not exists `counter` (
`rcvd` bigint unsigned default 0, `rcvd` bigint unsigned default 0,
`virus` bigint unsigned default 0, `virus` bigint unsigned default 0,
@ -160,7 +151,6 @@ create table if not exists `counter` (
insert into `counter` values(0, 0, 0, 0, 0); insert into `counter` values(0, 0, 0, 0, 0);
drop table if exists `option`;
create table if not exists `option` ( create table if not exists `option` (
`key` char(64) not null, `key` char(64) not null,
`value` char(32) not null `value` char(32) not null
@ -169,7 +159,6 @@ create table if not exists `option` (
insert into `option` (`key`, `value`) values('enable_purge', '1'); insert into `option` (`key`, `value`) values('enable_purge', '1');
drop table if exists `search`;
create table if not exists `search` ( create table if not exists `search` (
`email` char(128) not null, `email` char(128) not null,
`ts` int default 0, `ts` int default 0,
@ -179,7 +168,6 @@ create table if not exists `search` (
create index `search_idx` on `search`(`email`); create index `search_idx` on `search`(`email`);
drop table if exists `user_settings`;
create table if not exists `user_settings` ( create table if not exists `user_settings` (
`username` char(64) not null unique, `username` char(64) not null unique,
`pagelen` int default 20, `pagelen` int default 20,
@ -190,7 +178,6 @@ create table if not exists `user_settings` (
create index `user_settings_idx` on `user_settings`(`username`); create index `user_settings_idx` on `user_settings`(`username`);
drop table if exists `user`;
create table if not exists `user` ( create table if not exists `user` (
`uid` int unsigned not null primary key, `uid` int unsigned not null primary key,
`username` char(64) not null unique, `username` char(64) not null unique,
@ -204,7 +191,6 @@ create table if not exists `user` (
insert into `user` (`uid`, `username`, `realname`, `samaccountname`, `password`, `isadmin`, `domain`) values (0, 'admin', 'built-in piler admin', '', '$1$PItc7d$zsUgON3JRrbdGS11t9JQW1', 1, 'local'); insert into `user` (`uid`, `username`, `realname`, `samaccountname`, `password`, `isadmin`, `domain`) values (0, 'admin', 'built-in piler admin', '', '$1$PItc7d$zsUgON3JRrbdGS11t9JQW1', 1, 'local');
drop table if exists `email`;
create table if not exists `email` ( create table if not exists `email` (
`uid` int unsigned not null, `uid` int unsigned not null,
`email` char(128) not null primary key `email` char(128) not null primary key
@ -302,7 +288,6 @@ create table if not exists `remote` (
) ENGINE=InnoDB; ) ENGINE=InnoDB;
drop table if exists `domain`;
create table if not exists `domain` ( create table if not exists `domain` (
`domain` char(64) not null primary key, `domain` char(64) not null primary key,
`mapped` char(64) not null `mapped` char(64) not null
@ -311,7 +296,6 @@ create table if not exists `domain` (
insert into `domain` (`domain`, `mapped`) values('local', 'local'); insert into `domain` (`domain`, `mapped`) values('local', 'local');
drop table if exists `audit`;
create table if not exists `audit` ( create table if not exists `audit` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`ts` int not null, `ts` int not null,
@ -331,7 +315,6 @@ create index `audit_idx4` on `audit`(`ts`);
drop table if exists `google`;
create table if not exists `google` ( create table if not exists `google` (
`id` bigint unsigned not null primary key, `id` bigint unsigned not null primary key,
`email` char(128) not null unique, `email` char(128) not null unique,
@ -341,7 +324,6 @@ create table if not exists `google` (
) ENGINE=InnoDB; ) ENGINE=InnoDB;
drop table if exists `google_imap`;
create table if not exists `google_imap` ( create table if not exists `google_imap` (
`id` bigint unsigned not null, `id` bigint unsigned not null,
`email` char(128) not null, `email` char(128) not null,

View File

@ -1,5 +1,4 @@
drop table if exists `sph_counter`;
create table if not exists `sph_counter` ( create table if not exists `sph_counter` (
`counter_id` int not null, `counter_id` int not null,
`max_doc_id` int not null, `max_doc_id` int not null,
@ -7,7 +6,6 @@ create table if not exists `sph_counter` (
); );
drop table if exists `sph_index`;
create table if not exists `sph_index` ( create table if not exists `sph_index` (
`id` bigint not null, `id` bigint not null,
`from` char(255) default null, `from` char(255) default null,
@ -27,7 +25,6 @@ create table if not exists `sph_index` (
) Engine=InnoDB; ) Engine=InnoDB;
drop table if exists `metadata`;
create table if not exists `metadata` ( create table if not exists `metadata` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`from` varchar(128) not null, `from` varchar(128) not null,
@ -60,7 +57,6 @@ create index metadata_idx6 on metadata(`arrived`);
create index metadata_idx7 on metadata(`retained`); create index metadata_idx7 on metadata(`retained`);
drop table if exists `rcpt`;
create table if not exists `rcpt` ( create table if not exists `rcpt` (
`id` bigint unsigned not null, `id` bigint unsigned not null,
`to` varchar(128) not null, `to` varchar(128) not null,
@ -76,7 +72,6 @@ drop view if exists `v_messages`;
create view `v_messages` AS select `metadata`.`id` AS `id`,`metadata`.`piler_id` AS `piler_id`,`metadata`.`from` AS `from`,`metadata`.`fromdomain` AS `fromdomain`,`rcpt`.`to` AS `to`,`rcpt`.`todomain` AS `todomain`,`metadata`.`subject` AS `subject`, `metadata`.`size` AS `size`, `metadata`.`direction` AS `direction`, `metadata`.`sent` AS `sent`, `metadata`.`arrived` AS `arrived`, `metadata`.`digest` AS `digest`, `metadata`.`bodydigest` AS `bodydigest` from (`metadata` join `rcpt`) where (`metadata`.`id` = `rcpt`.`id`); create view `v_messages` AS select `metadata`.`id` AS `id`,`metadata`.`piler_id` AS `piler_id`,`metadata`.`from` AS `from`,`metadata`.`fromdomain` AS `fromdomain`,`rcpt`.`to` AS `to`,`rcpt`.`todomain` AS `todomain`,`metadata`.`subject` AS `subject`, `metadata`.`size` AS `size`, `metadata`.`direction` AS `direction`, `metadata`.`sent` AS `sent`, `metadata`.`arrived` AS `arrived`, `metadata`.`digest` AS `digest`, `metadata`.`bodydigest` AS `bodydigest` from (`metadata` join `rcpt`) where (`metadata`.`id` = `rcpt`.`id`);
drop table if exists `attachment`;
create table if not exists `attachment` ( create table if not exists `attachment` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`piler_id` char(36) not null, `piler_id` char(36) not null,
@ -98,7 +93,6 @@ drop view if exists `v_attachment`;
create view `v_attachment` AS select `id` as `i`, `piler_id`, `attachment_id`, `ptr`, (select count(*) from `attachment` where `ptr`=`i`) as `refcount` from `attachment`; create view `v_attachment` AS select `id` as `i`, `piler_id`, `attachment_id`, `ptr`, (select count(*) from `attachment` where `ptr`=`i`) as `refcount` from `attachment`;
drop table if exists `tag`;
create table if not exists `tag` ( create table if not exists `tag` (
`_id` bigint unsigned auto_increment not null, `_id` bigint unsigned auto_increment not null,
`id` bigint not null, `id` bigint not null,
@ -109,7 +103,6 @@ create table if not exists `tag` (
) ENGINE=InnoDB; ) ENGINE=InnoDB;
drop table if exists `archiving_rule`;
create table if not exists `archiving_rule` ( create table if not exists `archiving_rule` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`from` char(128) default null, `from` char(128) default null,
@ -127,7 +120,6 @@ create table if not exists `archiving_rule` (
) ENGINE=InnoDB; ) ENGINE=InnoDB;
drop table if exists `retention_rule`;
create table if not exists `retention_rule` ( create table if not exists `retention_rule` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`from` char(128) default null, `from` char(128) default null,
@ -145,7 +137,6 @@ create table if not exists `retention_rule` (
) ENGINE=InnoDB; ) ENGINE=InnoDB;
drop table if exists `counter`;
create table if not exists `counter` ( create table if not exists `counter` (
`rcvd` bigint unsigned default 0, `rcvd` bigint unsigned default 0,
`virus` bigint unsigned default 0, `virus` bigint unsigned default 0,
@ -157,7 +148,6 @@ create table if not exists `counter` (
insert into `counter` values(0, 0, 0, 0, 0); insert into `counter` values(0, 0, 0, 0, 0);
drop table if exists `option`;
create table if not exists `option` ( create table if not exists `option` (
`key` char(64) not null, `key` char(64) not null,
`value` char(32) not null `value` char(32) not null
@ -166,7 +156,6 @@ create table if not exists `option` (
insert into `option` (`key`, `value`) values('enable_purge', '1'); insert into `option` (`key`, `value`) values('enable_purge', '1');
drop table if exists `search`;
create table if not exists `search` ( create table if not exists `search` (
`email` char(128) not null, `email` char(128) not null,
`ts` int default 0, `ts` int default 0,
@ -176,7 +165,6 @@ create table if not exists `search` (
create index `search_idx` on `search`(`email`); create index `search_idx` on `search`(`email`);
drop table if exists `user_settings`;
create table if not exists `user_settings` ( create table if not exists `user_settings` (
`username` char(64) not null unique, `username` char(64) not null unique,
`pagelen` int default 20, `pagelen` int default 20,
@ -187,7 +175,6 @@ create table if not exists `user_settings` (
create index `user_settings_idx` on `user_settings`(`username`); create index `user_settings_idx` on `user_settings`(`username`);
drop table if exists `user`;
create table if not exists `user` ( create table if not exists `user` (
`uid` int unsigned not null primary key, `uid` int unsigned not null primary key,
`username` char(64) not null unique, `username` char(64) not null unique,
@ -201,7 +188,6 @@ create table if not exists `user` (
insert into `user` (`uid`, `username`, `realname`, `samaccountname`, `password`, `isadmin`, `domain`) values (0, 'admin', 'built-in piler admin', '', '$1$PItc7d$zsUgON3JRrbdGS11t9JQW1', 1, 'local'); insert into `user` (`uid`, `username`, `realname`, `samaccountname`, `password`, `isadmin`, `domain`) values (0, 'admin', 'built-in piler admin', '', '$1$PItc7d$zsUgON3JRrbdGS11t9JQW1', 1, 'local');
drop table if exists `email`;
create table if not exists `email` ( create table if not exists `email` (
`uid` int unsigned not null, `uid` int unsigned not null,
`email` char(128) not null primary key `email` char(128) not null primary key
@ -299,7 +285,6 @@ create table if not exists `remote` (
) ENGINE=InnoDB; ) ENGINE=InnoDB;
drop table if exists `domain`;
create table if not exists `domain` ( create table if not exists `domain` (
`domain` char(64) not null primary key, `domain` char(64) not null primary key,
`mapped` char(64) not null `mapped` char(64) not null
@ -308,7 +293,6 @@ create table if not exists `domain` (
insert into `domain` (`domain`, `mapped`) values('local', 'local'); insert into `domain` (`domain`, `mapped`) values('local', 'local');
drop table if exists `audit`;
create table if not exists `audit` ( create table if not exists `audit` (
`id` bigint unsigned not null auto_increment, `id` bigint unsigned not null auto_increment,
`ts` int not null, `ts` int not null,
@ -328,7 +312,6 @@ create index `audit_idx4` on `audit`(`ts`);
drop table if exists `google`;
create table if not exists `google` ( create table if not exists `google` (
`id` bigint unsigned not null primary key, `id` bigint unsigned not null primary key,
`email` char(128) not null unique, `email` char(128) not null unique,
@ -338,7 +321,6 @@ create table if not exists `google` (
) ENGINE=InnoDB; ) ENGINE=InnoDB;
drop table if exists `google_imap`;
create table if not exists `google_imap` ( create table if not exists `google_imap` (
`id` bigint unsigned not null, `id` bigint unsigned not null,
`email` char(128) not null, `email` char(128) not null,