2012-05-26 15:18:22 +02:00
|
|
|
/*
|
|
|
|
* import.c, SJ
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-10 13:04:54 +02:00
|
|
|
#include <strings.h>
|
2012-05-26 15:18:22 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <piler.h>
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
int import_message(struct session_data *sdata, struct data *data, struct config *cfg){
|
2016-02-28 21:19:19 +01:00
|
|
|
int rc=ERR;
|
2017-05-14 17:48:47 +02:00
|
|
|
char *p, *rule, newpath[SMALLBUFSIZE];
|
2012-05-26 15:18:22 +02:00
|
|
|
struct stat st;
|
2015-11-21 23:06:47 +01:00
|
|
|
struct parser_state state;
|
2016-08-17 23:22:02 +02:00
|
|
|
struct counters counters;
|
2012-05-26 15:18:22 +02:00
|
|
|
|
|
|
|
|
2013-02-22 15:01:21 +01:00
|
|
|
init_session_data(sdata, cfg);
|
2012-05-26 15:18:22 +02:00
|
|
|
|
2014-07-01 11:43:36 +02:00
|
|
|
if(data->import->extra_recipient){
|
|
|
|
snprintf(sdata->rcptto[0], SMALLBUFSIZE-1, "%s", data->import->extra_recipient);
|
|
|
|
sdata->num_of_rcpt_to = 1;
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(cfg->verbosity > 1) printf("processing: %s\n", data->import->filename);
|
2012-05-26 15:18:22 +02:00
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(strcmp(data->import->filename, "-") == 0){
|
2012-05-26 15:18:22 +02:00
|
|
|
|
|
|
|
if(read_from_stdin(sdata) == ERR){
|
2017-08-08 15:34:45 +02:00
|
|
|
printf("ERROR: error reading from stdin\n");
|
2012-05-26 15:18:22 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", sdata->ttmpfile);
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(stat(data->import->filename, &st) != 0){
|
|
|
|
printf("ERROR: cannot stat() %s\n", data->import->filename);
|
2012-05-26 15:18:22 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(S_ISREG(st.st_mode) == 0){
|
2017-08-08 15:34:45 +02:00
|
|
|
printf("ERROR: %s is not a file\n", data->import->filename);
|
2012-05-26 15:18:22 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", data->import->filename);
|
2012-05-26 15:18:22 +02:00
|
|
|
|
|
|
|
sdata->tot_len = st.st_size;
|
2016-02-28 21:19:19 +01:00
|
|
|
}
|
|
|
|
|
2014-07-16 12:39:54 +02:00
|
|
|
|
2016-03-02 09:42:56 +01:00
|
|
|
if(sdata->tot_len < cfg->min_message_size){
|
2016-02-28 21:19:19 +01:00
|
|
|
printf("%s is too short: %d bytes\n", sdata->filename, sdata->tot_len);
|
|
|
|
return rc;
|
2012-05-26 15:18:22 +02:00
|
|
|
}
|
|
|
|
|
2016-02-28 21:19:19 +01:00
|
|
|
data->import->total_size += sdata->tot_len;
|
2012-05-26 15:18:22 +02:00
|
|
|
|
|
|
|
|
2012-12-03 14:21:55 +01:00
|
|
|
sdata->delivered = 0;
|
2012-05-26 15:18:22 +02:00
|
|
|
|
2014-01-13 13:06:10 +01:00
|
|
|
sdata->import = 1;
|
|
|
|
|
2013-01-06 22:16:21 +01:00
|
|
|
state = parse_message(sdata, 1, data, cfg);
|
2012-05-26 15:18:22 +02:00
|
|
|
post_parse(sdata, &state, cfg);
|
|
|
|
|
|
|
|
rule = check_againt_ruleset(data->archiving_rules, &state, sdata->tot_len, sdata->spam_message);
|
|
|
|
|
|
|
|
if(rule){
|
2017-08-08 15:34:45 +02:00
|
|
|
if(data->quiet == 0) printf("discarding %s by archiving policy: %s\n", data->import->filename, rule);
|
2012-05-26 15:18:22 +02:00
|
|
|
rc = OK;
|
2013-05-05 11:57:14 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
make_digests(sdata, cfg);
|
2013-06-29 15:43:16 +02:00
|
|
|
|
|
|
|
if(sdata->hdr_len < 10){
|
2017-08-08 15:34:45 +02:00
|
|
|
printf("%s: invalid message, hdr_len: %d\n", data->import->filename, sdata->hdr_len);
|
2013-06-29 15:43:16 +02:00
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
|
2016-10-06 22:36:53 +02:00
|
|
|
rc = process_message(sdata, &state, data, cfg);
|
2014-04-25 21:17:01 +02:00
|
|
|
unlink(state.message_id_hash);
|
2012-05-26 15:18:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unlink(sdata->tmpframe);
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(strcmp(data->import->filename, "-") == 0) unlink(sdata->ttmpfile);
|
2012-05-26 15:18:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
switch(rc) {
|
|
|
|
case OK:
|
2016-10-06 22:36:53 +02:00
|
|
|
bzero(&counters, sizeof(counters));
|
|
|
|
counters.c_rcvd = 1;
|
|
|
|
counters.c_size += sdata->tot_len;
|
|
|
|
counters.c_stored_size = sdata->stored_len;
|
|
|
|
update_counters(sdata, data, &counters, cfg);
|
2012-05-26 15:18:22 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ERR_EXISTS:
|
2012-07-09 15:18:21 +02:00
|
|
|
rc = OK;
|
2013-05-16 12:24:46 +02:00
|
|
|
|
|
|
|
bzero(&counters, sizeof(counters));
|
|
|
|
counters.c_duplicate = 1;
|
|
|
|
update_counters(sdata, data, &counters, cfg);
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(data->quiet == 0) printf("duplicate: %s (duplicate id: %llu)\n", data->import->filename, sdata->duplicate_id);
|
2012-05-26 15:18:22 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-08-08 15:34:45 +02:00
|
|
|
printf("failed to import: %s (id: %s)\n", data->import->filename, sdata->ttmpfile);
|
2012-05-26 15:18:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-14 17:18:09 +02:00
|
|
|
if(rc != OK && data->import->failed_folder){
|
2017-08-08 15:34:45 +02:00
|
|
|
p = strrchr(data->import->filename, '/');
|
2017-05-14 17:48:47 +02:00
|
|
|
if(p)
|
|
|
|
p++;
|
|
|
|
else
|
2017-08-08 15:34:45 +02:00
|
|
|
p = data->import->filename;
|
2017-05-14 17:48:47 +02:00
|
|
|
|
|
|
|
snprintf(newpath, sizeof(newpath)-2, "%s/%s", data->import->failed_folder, p);
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
if(rename(data->import->filename, newpath))
|
|
|
|
printf("cannot move %s to %s\n", data->import->filename, newpath);
|
2017-05-14 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
2012-05-26 15:18:22 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
int get_folder_id(struct session_data *sdata, struct data *data, char *foldername, int parent_id){
|
2013-06-29 15:22:07 +02:00
|
|
|
int id=ERR_FOLDER;
|
2017-08-11 18:18:45 +02:00
|
|
|
struct sql sql;
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_GET_FOLDER_ID) == ERR) return id;
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
p_bind_init(&sql);
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
sql.sql[sql.pos] = foldername; sql.type[sql.pos] = TYPE_STRING; sql.pos++;
|
|
|
|
sql.sql[sql.pos] = (char *)&parent_id; sql.type[sql.pos] = TYPE_LONG; sql.pos++;
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
if(p_exec_stmt(sdata, &sql) == OK){
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
p_bind_init(&sql);
|
|
|
|
|
|
|
|
sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(unsigned long); sql.pos++;
|
|
|
|
|
|
|
|
p_store_results(&sql);
|
|
|
|
p_fetch_results(&sql);
|
|
|
|
p_free_results(&sql);
|
2012-08-23 10:23:58 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
close_prepared_statement(&sql);
|
2012-08-23 10:23:58 +02:00
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
int add_new_folder(struct session_data *sdata, struct data *data, char *foldername, int parent_id){
|
2013-06-29 15:22:07 +02:00
|
|
|
int id=ERR_FOLDER;
|
2017-08-11 18:18:45 +02:00
|
|
|
struct sql sql;
|
2013-06-29 15:22:07 +02:00
|
|
|
|
|
|
|
if(foldername == NULL) return id;
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE) == ERR) return id;
|
|
|
|
|
|
|
|
p_bind_init(&sql);
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
sql.sql[sql.pos] = foldername; sql.type[sql.pos] = TYPE_STRING; sql.pos++;
|
|
|
|
sql.sql[sql.pos] = (char *)&parent_id; sql.type[sql.pos] = TYPE_LONG; sql.pos++;
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
if(p_exec_stmt(sdata, &sql) == OK){
|
|
|
|
id = p_get_insert_id(&sql);
|
2012-08-23 10:23:58 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
close_prepared_statement(&sql);
|
2012-08-23 10:23:58 +02:00
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|