diff --git a/src/cfg.c b/src/cfg.c index e177033d..c7e74f78 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -60,6 +60,7 @@ struct _parse_rule config_parse_rules[] = { { "archive_emails_not_having_message_id", "integer", (void*) int_parser, offsetof(struct __config, archive_emails_not_having_message_id), "0", sizeof(int)}, + { "archive_only_mydomains", "integer", (void*) int_parser, offsetof(struct __config, archive_only_mydomains), "0", sizeof(int)}, { "backlog", "integer", (void*) int_parser, offsetof(struct __config, backlog), "20", sizeof(int)}, { "cipher_list", "string", (void*) string_parser, offsetof(struct __config, cipher_list), "HIGH:MEDIUM", MAXVAL-1}, { "clamd_addr", "string", (void*) string_parser, offsetof(struct __config, clamd_addr), "", MAXVAL-1}, diff --git a/src/cfg.h b/src/cfg.h index 28c7abb7..0c1c9d0a 100644 --- a/src/cfg.h +++ b/src/cfg.h @@ -76,6 +76,8 @@ struct __config { int archive_emails_not_having_message_id; + int archive_only_mydomains; + int min_word_len; int tweak_sent_time_offset; diff --git a/src/config.h b/src/config.h index df78de86..5ad0d970 100644 --- a/src/config.h +++ b/src/config.h @@ -14,7 +14,7 @@ #define VERSION "0.1.24-master-branch" -#define BUILD 819 +#define BUILD 822 #define HOSTID "mailarchiver" @@ -116,6 +116,7 @@ #define OK 0 #define ERR 1 #define ERR_EXISTS 2 +#define ERR_MYDOMAINS 3 #define ERR_FOLDER -1 #define AVIR_OK 0 diff --git a/src/defs.h b/src/defs.h index 870c29e2..09e61a62 100644 --- a/src/defs.h +++ b/src/defs.h @@ -246,7 +246,7 @@ struct __data { int folder; char recursive_folder_names; char starttls[TINYBUFSIZE]; - char mydomains[MAXBUFSIZE]; + struct list *mydomains; #ifdef NEED_MYSQL MYSQL_STMT *stmt_generic; diff --git a/src/misc.c b/src/misc.c index 5fcb552d..098840ac 100644 --- a/src/misc.c +++ b/src/misc.c @@ -412,30 +412,6 @@ int drop_privileges(struct passwd *pwd){ } -int is_email_address_on_my_domains(char *email, struct __data *data){ - int rc=0; - char *p, *q=NULL; - - if(email == NULL || data->mydomains[0] == '\0') return rc; - - p = strchr(email, '@'); - - if(!p) return rc; - - if(strlen(p) < 3) return rc; - - q = strrchr(p+1, ' '); - - if(q) *q = '\0'; - - if(strcasestr(data->mydomains, p+1)) rc = 1; - - if(q) *q = ' '; - - return rc; -} - - void init_session_data(struct session_data *sdata, struct __config *cfg){ int i; diff --git a/src/misc.h b/src/misc.h index 5bffa9f5..3d4b6f4d 100644 --- a/src/misc.h +++ b/src/misc.h @@ -34,7 +34,6 @@ int recvtimeoutssl(int s, char *buf, int len, int timeout, int use_ssl, SSL *ssl void write_pid_file(char *pidfile); int drop_privileges(struct passwd *pwd); -int is_email_address_on_my_domains(char *email, struct __data *data); void init_session_data(struct session_data *sdata, struct __config *cfg); int read_from_stdin(struct session_data *sdata); void strtolower(char *s); diff --git a/src/mydomains.c b/src/mydomains.c index 396ca83b..ee984546 100644 --- a/src/mydomains.c +++ b/src/mydomains.c @@ -11,10 +11,9 @@ void load_mydomains(struct session_data *sdata, struct __data *data, struct __config *cfg){ - int clen=0, len=0, size=sizeof(data->mydomains); + int rc; char s[SMALLBUFSIZE]; - memset(data->mydomains, 0, size); memset(s, 0, sizeof(s)); @@ -34,13 +33,9 @@ void load_mydomains(struct session_data *sdata, struct __data *data, struct __co p_store_results(sdata, data->stmt_generic, data); while(p_fetch_results(data->stmt_generic) == OK){ - len = strlen(s); - - if(clen + len + 1 < size){ - memcpy(data->mydomains+clen, s, len); - clen += len; - } - else break; + rc = append_list(&(data->mydomains), s); + if(rc == -1) syslog(LOG_PRIORITY, "failed to append mydomain: '%s'", s); + else if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "added mydomain: '%s'", s); memset(s, 0, sizeof(s)); } @@ -49,7 +44,38 @@ void load_mydomains(struct session_data *sdata, struct __data *data, struct __co ENDE: close_prepared_statement(data->stmt_generic); - - if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "mydomains: '%s'", data->mydomains); } + +int is_email_address_on_my_domains(char *email, struct __data *data){ + int rc=0; + char *q, *s; + struct list *p; + + if(email == NULL) return rc; + + q = strchr(email, '@'); + if(!q || strlen(q) < 3) return rc; + + s = strrchr(q+1, ' '); + + if(s) *s = '\0'; + + + p = data->mydomains; + + while(p != NULL){ + if(strcasecmp(p->s, q+1) == 0){ + rc = 1; + break; + } + + p = p->r; + } + + if(s) *s = ' '; + + return rc; +} + + diff --git a/src/piler.c b/src/piler.c index 911bdd31..5765e59c 100644 --- a/src/piler.c +++ b/src/piler.c @@ -231,6 +231,8 @@ void p_clean_exit(){ free_rule(data.archiving_rules); free_rule(data.retention_rules); + free_list(data.mydomains); + #ifdef HAVE_MULTITENANCY free_list(data.customers); #endif @@ -308,6 +310,8 @@ void initialise_configuration(){ free_rule(data.archiving_rules); free_rule(data.retention_rules); + free_list(data.mydomains); + #ifdef HAVE_MULTITENANCY free_list(data.customers); #endif @@ -316,6 +320,8 @@ void initialise_configuration(){ data.recursive_folder_names = 0; data.archiving_rules = NULL; data.retention_rules = NULL; + data.mydomains = NULL; + #ifdef HAVE_MULTITENANCY data.customers = NULL; #endif @@ -389,6 +395,7 @@ int main(int argc, char **argv){ data.recursive_folder_names = 0; data.archiving_rules = NULL; data.retention_rules = NULL; + data.mydomains = NULL; #ifdef HAVE_MULTITENANCY data.customers = NULL; #endif diff --git a/src/piler.h b/src/piler.h index b5aa7ecb..eb61dcf4 100644 --- a/src/piler.h +++ b/src/piler.h @@ -65,6 +65,8 @@ void extract_attachment_content(struct session_data *sdata, struct _state *state int retrieve_file_from_archive(char *filename, int mode, char **buffer, FILE *dest, struct __config *cfg); void load_mydomains(struct session_data *sdata, struct __data *data, struct __config *cfg); +int is_email_address_on_my_domains(char *email, struct __data *data); + 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); diff --git a/src/pilerimport.c b/src/pilerimport.c index c6c60b46..ea76c2d6 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -399,6 +399,7 @@ int main(int argc, char **argv){ data.recursive_folder_names = 0; data.archiving_rules = NULL; data.retention_rules = NULL; + data.mydomains = NULL; while(1){ @@ -571,6 +572,8 @@ int main(int argc, char **argv){ free_rule(data.archiving_rules); free_rule(data.retention_rules); + free_list(data.mydomains); + close_database(&sdata); if(quiet == 0) printf("\n"); diff --git a/src/reindex.c b/src/reindex.c index 996efdd8..1d7e77bc 100644 --- a/src/reindex.c +++ b/src/reindex.c @@ -218,6 +218,7 @@ int main(int argc, char **argv){ data.recursive_folder_names = 0; data.archiving_rules = NULL; data.retention_rules = NULL; + data.mydomains = NULL; if(folder){ data.folder = get_folder_id(&sdata, &data, folder, 0); @@ -246,6 +247,8 @@ int main(int argc, char **argv){ printf("put %llu messages to %s table for reindexing\n", n, SQL_SPHINX_TABLE); + free_list(data.mydomains); + close_database(&sdata); return 0; diff --git a/src/session.c b/src/session.c index 600144ed..d94172c2 100644 --- a/src/session.c +++ b/src/session.c @@ -161,6 +161,18 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){ if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: parsed message", sdata.ttmpfile); + if(cfg->archive_only_mydomains == 1 && sdata.internal_sender == 0 && sdata.internal_recipient == 0){ + remove_stripped_attachments(&sstate); + inj = ERR_MYDOMAINS; + + snprintf(sdata.acceptbuf, SMALLBUFSIZE-1, "250 Ok %s <%s>\r\n", sdata.ttmpfile, rcpttoemail); + write1(new_sd, sdata.acceptbuf, strlen(sdata.acceptbuf), sdata.tls, data->ssl); + + syslog(LOG_PRIORITY, "%s: discarding: not on mydomains, from=%s, message-id=%s", sdata.ttmpfile, sdata.fromemail, sstate.message_id); + + goto END_OF_PROCESSING; + } + sdata.need_scan = 1; make_digests(&sdata, cfg); @@ -194,7 +206,7 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){ if(db_conn == 1){ if(sdata.restored_copy == 1){ - syslog(LOG_PRIORITY, "%s: discarding restored copy", sdata.ttmpfile); + syslog(LOG_PRIORITY, "%s: discarding: restored copy", sdata.ttmpfile); inj = OK; } else if(AVIR_VIRUS == sdata.rav){ @@ -212,7 +224,7 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){ arule = check_againt_ruleset(data->archiving_rules, &sstate, sdata.tot_len, sdata.spam_message); if(arule){ - syslog(LOG_PRIORITY, "%s: discarding message by archiving policy: *%s*", sdata.ttmpfile, arule); + syslog(LOG_PRIORITY, "%s: discarding: archiving policy: *%s*", sdata.ttmpfile, arule); inj = OK; counters.c_ignore++; @@ -242,7 +254,7 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){ counters.c_rcvd++; if(inj == ERR_EXISTS){ - syslog(LOG_PRIORITY, "%s: discarding duplicate message", sdata.ttmpfile); + syslog(LOG_PRIORITY, "%s: discarding: duplicate message", sdata.ttmpfile); counters.c_duplicate++; } @@ -258,6 +270,8 @@ int handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){ } /* for */ #endif + END_OF_PROCESSING: + unlink(sdata.ttmpfile); unlink(sdata.tmpframe); diff --git a/src/test.c b/src/test.c index 760145de..4e614c12 100644 --- a/src/test.c +++ b/src/test.c @@ -48,6 +48,7 @@ int main(int argc, char **argv){ data.recursive_folder_names = 0; data.archiving_rules = NULL; data.retention_rules = NULL; + data.mydomains = NULL; load_rules(&sdata, &data, &(data.archiving_rules), SQL_ARCHIVING_RULE_TABLE); load_rules(&sdata, &data, &(data.retention_rules), SQL_RETENTION_RULE_TABLE); @@ -96,6 +97,8 @@ int main(int argc, char **argv){ free_rule(data.archiving_rules); free_rule(data.retention_rules); + free_list(data.mydomains); + for(i=1; i<=state.n_attachments; i++){ printf("i:%d, name=*%s*, type: *%s*, size: %d, int.name: %s, digest: %s\n", i, state.attachments[i].filename, state.attachments[i].type, state.attachments[i].size, state.attachments[i].internalname, state.attachments[i].digest); unlink(state.attachments[i].internalname);