diff --git a/src/misc.c b/src/misc.c index 5dc0d9cd..5e43fb2c 100644 --- a/src/misc.c +++ b/src/misc.c @@ -409,6 +409,31 @@ void init_session_data(struct session_data *sdata){ } +int read_from_stdin(struct session_data *sdata){ + int fd, rc=ERR, n; + char buf[MAXBUFSIZE]; + + fd = open(sdata->ttmpfile, O_CREAT|O_EXCL|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR); + if(fd == -1){ + syslog(LOG_PRIORITY, "%s: cannot open ttmpfile", sdata->ttmpfile); + return rc; + } + + while((n = read(0, buf, sizeof(buf))) > 0){ + sdata->tot_len += write(fd, buf, n); + } + + if(fsync(fd)){ + syslog(LOG_PRIORITY, "%s: fsync() error", sdata->ttmpfile); + } + else rc = OK; + + close(fd); + + return rc; +} + + #ifndef _GNU_SOURCE char *strcasestr(const char *s, const char *find){ char c, sc; diff --git a/src/misc.h b/src/misc.h index df6439c7..941b7c6e 100644 --- a/src/misc.h +++ b/src/misc.h @@ -29,6 +29,7 @@ int drop_privileges(struct passwd *pwd); int is_email_address_on_my_domains(char *email, struct __config *cfg); void init_session_data(struct session_data *sdata); +int read_from_stdin(struct session_data *sdata); #ifndef _GNU_SOURCE char *strcasestr(const char *s, const char *find); diff --git a/src/pilerimport.c b/src/pilerimport.c index a1b983b1..49c17c09 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -27,22 +27,40 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da struct _state state; struct __counters counters; - if(stat(filename, &st) != 0){ - printf("cannot read: %s\n", filename); - return rc; - } - - if(S_ISREG(st.st_mode) == 0){ - printf("%s is not a file\n", filename); - return rc; - } - init_session_data(sdata); - snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename); + + + if(strcmp(filename, "-") == 0){ + + if(read_from_stdin(sdata) == ERR){ + printf("error reading from stdin\n"); + return rc; + } + + snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", sdata->ttmpfile); + + } + else { + + if(stat(filename, &st) != 0){ + printf("cannot read: %s\n", filename); + return rc; + } + + if(S_ISREG(st.st_mode) == 0){ + printf("%s is not a file\n", filename); + return rc; + } + + snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename); + + sdata->tot_len = st.st_size; + } + + sdata->sent = 0; - sdata->tot_len = st.st_size; state = parse_message(sdata, cfg); post_parse(sdata, &state, cfg); @@ -54,7 +72,7 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da if(sdata->sent > 631148400) sdata->retained = sdata->sent; - rule = check_againt_ruleset(data->archiving_rules, &state, st.st_size, sdata->spam_message); + rule = check_againt_ruleset(data->archiving_rules, &state, sdata->tot_len, sdata->spam_message); if(rule){ printf("discarding %s by archiving policy: %s\n", filename, rule); @@ -69,6 +87,9 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da ENDE: unlink(sdata->tmpframe); + if(strcmp(filename, "-") == 0) unlink(sdata->ttmpfile); + + switch(rc) { case OK: printf("imported: %s\n", filename);