2012-09-07 15:08:50 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2012-09-11 14:11:17 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2015-03-17 12:03:45 +01:00
|
|
|
#include <sys/wait.h>
|
2012-09-11 14:11:17 +02:00
|
|
|
#include <sys/stat.h>
|
2013-09-11 09:19:29 +02:00
|
|
|
#include <dirent.h>
|
2012-09-11 14:11:17 +02:00
|
|
|
#include <fcntl.h>
|
2015-03-16 11:10:52 +01:00
|
|
|
#include <ctype.h>
|
2016-10-21 07:41:30 +02:00
|
|
|
#include <signal.h>
|
2012-09-07 15:08:50 +02:00
|
|
|
#include <piler.h>
|
|
|
|
|
2012-09-09 23:16:09 +02:00
|
|
|
#ifdef HAVE_ZIP
|
|
|
|
#include <zip.h>
|
|
|
|
#endif
|
2012-09-07 15:08:50 +02:00
|
|
|
|
2015-03-17 12:03:45 +01:00
|
|
|
#define die(e) do { syslog(LOG_INFO, "error: helper: %s", e); exit(EXIT_FAILURE); } while (0);
|
|
|
|
|
2012-09-09 23:16:09 +02:00
|
|
|
|
2018-03-04 13:21:19 +01:00
|
|
|
int remove_xml(char *src, char *dest, int destlen, int *html){
|
2012-09-09 23:16:09 +02:00
|
|
|
int i=0;
|
|
|
|
|
2018-03-04 13:21:19 +01:00
|
|
|
memset(dest, 0, destlen);
|
2012-09-09 23:16:09 +02:00
|
|
|
|
2018-03-04 13:21:19 +01:00
|
|
|
for(; *src; src++){
|
|
|
|
if(*src == '<'){ *html = 1; continue; }
|
|
|
|
if(*src == '>'){ *html = 0; continue; }
|
2012-09-09 23:16:09 +02:00
|
|
|
|
|
|
|
if(*html == 0){
|
2018-03-04 13:21:19 +01:00
|
|
|
if(i < destlen) *(dest+i) = *src;
|
2012-09-09 23:16:09 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 13:21:19 +01:00
|
|
|
return i;
|
2012-09-09 23:16:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-11 14:11:17 +02:00
|
|
|
#ifdef HAVE_ZIP
|
2015-11-21 23:06:47 +01:00
|
|
|
int extract_opendocument(struct session_data *sdata, struct parser_state *state, char *filename, char *prefix){
|
2012-09-09 23:16:09 +02:00
|
|
|
int errorp, i=0, len=0, html=0;
|
2013-03-24 01:20:12 +01:00
|
|
|
int len2;
|
2018-03-04 13:21:19 +01:00
|
|
|
char buf[4*MAXBUFSIZE], puf[4*MAXBUFSIZE];
|
2012-09-09 23:16:09 +02:00
|
|
|
struct zip *z;
|
|
|
|
struct zip_stat sb;
|
|
|
|
struct zip_file *zf;
|
|
|
|
|
2015-01-26 21:34:03 +01:00
|
|
|
z = zip_open(filename, ZIP_CHECKCONS, &errorp);
|
|
|
|
if(!z){
|
2015-02-01 10:40:18 +01:00
|
|
|
syslog(LOG_INFO, "%s: error: corrupt zip file=%s, error code=%d", sdata->ttmpfile, filename, errorp);
|
2015-01-26 21:34:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2012-09-09 23:16:09 +02:00
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
|
|
|
while(zip_stat_index(z, i, 0, &sb) == 0){
|
2014-04-27 10:59:59 +02:00
|
|
|
if(ZIP_EM_NONE == sb.encryption_method && strncmp(sb.name, prefix, strlen(prefix)) == 0 && (int)sb.size > 0){
|
2012-09-09 23:16:09 +02:00
|
|
|
|
|
|
|
zf = zip_fopen_index(z, i, 0);
|
|
|
|
if(zf){
|
2013-03-24 01:20:12 +01:00
|
|
|
while((len = zip_fread(zf, buf, sizeof(buf)-2)) > 0){
|
2012-09-09 23:16:09 +02:00
|
|
|
|
2018-03-04 13:21:19 +01:00
|
|
|
len2 = remove_xml(buf, puf, sizeof(puf), &html);
|
2012-09-09 23:16:09 +02:00
|
|
|
|
2013-03-24 01:20:12 +01:00
|
|
|
if(len2 > 0 && state->bodylen < BIGBUFSIZE-len2-1){
|
2018-03-04 13:21:19 +01:00
|
|
|
memcpy(&(state->b_body[state->bodylen]), puf, len2);
|
2013-03-24 01:20:12 +01:00
|
|
|
state->bodylen += len2;
|
2012-09-09 23:16:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
}
|
|
|
|
zip_fclose(zf);
|
|
|
|
}
|
2012-09-11 14:11:17 +02:00
|
|
|
else syslog(LOG_PRIORITY, "%s: cannot extract '%s' from '%s'", sdata->ttmpfile, sb.name, filename);
|
2012-09-09 23:16:09 +02:00
|
|
|
|
|
|
|
if(state->bodylen > BIGBUFSIZE-1024) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
zip_close(z);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
int unzip_file(struct session_data *sdata, struct parser_state *state, char *filename, int *rec, struct config *cfg){
|
2015-03-17 12:03:45 +01:00
|
|
|
int errorp, i=0, len=0, fd;
|
2012-09-11 14:11:17 +02:00
|
|
|
char *p, extracted_filename[SMALLBUFSIZE], buf[MAXBUFSIZE];
|
|
|
|
struct zip *z;
|
|
|
|
struct zip_stat sb;
|
|
|
|
struct zip_file *zf;
|
|
|
|
|
|
|
|
(*rec)++;
|
|
|
|
|
2015-01-26 21:34:03 +01:00
|
|
|
z = zip_open(filename, ZIP_CHECKCONS, &errorp);
|
|
|
|
if(!z){
|
2015-02-01 10:40:18 +01:00
|
|
|
syslog(LOG_INFO, "%s: error: corrupt zip file=%s, error code=%d", sdata->ttmpfile, filename, errorp);
|
2015-01-26 21:34:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2012-09-11 14:11:17 +02:00
|
|
|
|
|
|
|
while(zip_stat_index(z, i, 0, &sb) == 0){
|
2012-12-04 12:01:33 +01:00
|
|
|
//printf("processing file inside the zip: %s, index: %d, size: %d\n", sb.name, sb.index, (int)sb.size);
|
|
|
|
|
2014-04-27 10:59:59 +02:00
|
|
|
if(ZIP_EM_NONE == sb.encryption_method) {
|
2012-09-11 14:11:17 +02:00
|
|
|
|
2014-04-27 10:59:59 +02:00
|
|
|
p = strrchr(sb.name, '.');
|
2012-09-11 14:11:17 +02:00
|
|
|
|
2015-03-17 12:03:45 +01:00
|
|
|
if((int)sb.size > 0 && p && strcmp(get_attachment_extractor_by_filename((char*)sb.name), "other")){
|
2012-09-11 14:11:17 +02:00
|
|
|
|
2014-04-27 10:59:59 +02:00
|
|
|
snprintf(extracted_filename, sizeof(extracted_filename)-1, "%s-%d-%d%s", sdata->ttmpfile, *rec, i, p);
|
|
|
|
|
2015-03-16 11:10:52 +01:00
|
|
|
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_INFO, "%s: writing zip content to '%s'", sdata->ttmpfile, extracted_filename);
|
|
|
|
|
2014-04-27 10:59:59 +02:00
|
|
|
fd = open(extracted_filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
|
|
|
if(fd != -1){
|
|
|
|
zf = zip_fopen_index(z, i, 0);
|
|
|
|
if(zf){
|
|
|
|
while((len = zip_fread(zf, buf, sizeof(buf))) > 0){
|
2017-08-08 15:34:45 +02:00
|
|
|
if(write(fd, buf, len) == -1) syslog(LOG_PRIORITY, "ERROR: error writing to fd in %s", __func__);
|
2014-04-27 10:59:59 +02:00
|
|
|
}
|
|
|
|
zip_fclose(zf);
|
2012-09-11 14:11:17 +02:00
|
|
|
}
|
2014-04-27 10:59:59 +02:00
|
|
|
else syslog(LOG_PRIORITY, "%s: cannot extract '%s' from '%s'", sdata->ttmpfile, sb.name, extracted_filename);
|
2012-09-11 14:11:17 +02:00
|
|
|
|
2014-04-27 10:59:59 +02:00
|
|
|
close(fd);
|
2012-09-11 14:11:17 +02:00
|
|
|
|
2014-11-04 12:01:39 +01:00
|
|
|
extract_attachment_content(sdata, state, extracted_filename, get_attachment_extractor_by_filename(extracted_filename), rec, cfg);
|
2012-09-11 14:11:17 +02:00
|
|
|
|
2014-04-27 10:59:59 +02:00
|
|
|
unlink(extracted_filename);
|
2012-09-11 14:11:17 +02:00
|
|
|
|
2014-04-27 10:59:59 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
syslog(LOG_PRIORITY, "%s: cannot open '%s'", sdata->ttmpfile, extracted_filename);
|
|
|
|
}
|
2012-09-11 14:11:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-04-27 10:59:59 +02:00
|
|
|
else {
|
2015-03-17 12:03:45 +01:00
|
|
|
syslog(LOG_PRIORITY, "error: attachment ('%s') is in encrypted zip file", sb.name);
|
2014-04-27 10:59:59 +02:00
|
|
|
}
|
2012-09-11 14:11:17 +02:00
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
zip_close(z);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-09-24 21:04:15 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_TNEF
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
int extract_tnef(struct session_data *sdata, struct parser_state *state, char *filename, struct config *cfg){
|
2013-09-24 21:04:15 +02:00
|
|
|
int rc=0, n, rec=1;
|
|
|
|
char tmpdir[BUFLEN], buf[SMALLBUFSIZE];
|
|
|
|
struct dirent **namelist;
|
|
|
|
|
|
|
|
memset(tmpdir, 0, sizeof(tmpdir));
|
|
|
|
make_random_string(&tmpdir[0], sizeof(tmpdir)-3);
|
|
|
|
|
|
|
|
memcpy(&tmpdir[sizeof(tmpdir)-3], ".d", 2);
|
|
|
|
|
|
|
|
if(mkdir(tmpdir, 0700)) return rc;
|
|
|
|
|
2013-12-16 22:20:23 +01:00
|
|
|
snprintf(buf, sizeof(buf)-1, "%s --unix-paths -C %s %s", HAVE_TNEF, tmpdir, filename);
|
2013-09-24 21:04:15 +02:00
|
|
|
|
|
|
|
system(buf);
|
|
|
|
|
|
|
|
n = scandir(tmpdir, &namelist, NULL, alphasort);
|
2015-03-17 12:03:45 +01:00
|
|
|
if(n < 0) syslog(LOG_INFO, "error: reading %s", tmpdir);
|
2013-09-24 21:04:15 +02:00
|
|
|
else {
|
|
|
|
while(n--){
|
|
|
|
if(strcmp(namelist[n]->d_name, ".") && strcmp(namelist[n]->d_name, "..")){
|
2015-03-16 13:06:57 +01:00
|
|
|
|
2013-09-24 21:04:15 +02:00
|
|
|
snprintf(buf, sizeof(buf)-1, "%s/%s", tmpdir, namelist[n]->d_name);
|
2015-03-17 12:03:45 +01:00
|
|
|
extract_attachment_content(sdata, state, buf, get_attachment_extractor_by_filename(buf), &rec, cfg);
|
2013-09-24 21:04:15 +02:00
|
|
|
|
|
|
|
unlink(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(namelist[n]);
|
|
|
|
}
|
|
|
|
free(namelist);
|
|
|
|
}
|
|
|
|
|
|
|
|
rmdir(tmpdir);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-09-07 15:08:50 +02:00
|
|
|
|
2015-03-17 12:03:45 +01:00
|
|
|
void kill_helper(){
|
|
|
|
syslog(LOG_PRIORITY, "error: helper is killed by alarm");
|
|
|
|
die("timeout for helper!");
|
|
|
|
}
|
2012-09-14 15:03:00 +02:00
|
|
|
|
2013-09-11 09:19:29 +02:00
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
void extract_attachment_content(struct session_data *sdata, struct parser_state *state, char *filename, char *type, int *rec, struct config *cfg){
|
2015-03-17 12:03:45 +01:00
|
|
|
int link[2], n;
|
|
|
|
pid_t pid;
|
|
|
|
char outbuf[MAXBUFSIZE];
|
2012-09-09 23:16:09 +02:00
|
|
|
|
2015-03-20 15:46:10 +01:00
|
|
|
if(strcmp(type, "other") == 0 || strcmp(type, "text") == 0) return;
|
2012-09-09 23:16:09 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_ZIP
|
|
|
|
if(strcmp(type, "odf") == 0){
|
|
|
|
extract_opendocument(sdata, state, filename, "content.xml");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strcmp(type, "docx") == 0){
|
|
|
|
extract_opendocument(sdata, state, filename, "word/document.xml");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strcmp(type, "xlsx") == 0){
|
2012-09-26 15:26:59 +02:00
|
|
|
extract_opendocument(sdata, state, filename, "xl/sharedStrings.xml");
|
2012-09-09 23:16:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strcmp(type, "pptx") == 0){
|
|
|
|
extract_opendocument(sdata, state, filename, "ppt/slides/slide");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-11 14:11:17 +02:00
|
|
|
if(strcmp(type, "zip") == 0){
|
|
|
|
if(*rec < MAX_ZIP_RECURSION_LEVEL){
|
2014-11-04 12:01:39 +01:00
|
|
|
unzip_file(sdata, state, filename, rec, cfg);
|
2012-09-11 14:11:17 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
syslog(LOG_PRIORITY, "%s: multiple recursion level zip attachment, skipping %s", sdata->ttmpfile, filename);
|
|
|
|
}
|
2012-09-09 23:16:09 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-17 12:03:45 +01:00
|
|
|
#ifdef HAVE_TNEF
|
|
|
|
if(strcmp(type, "tnef") == 0){
|
|
|
|
extract_tnef(sdata, state, filename, cfg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-03-17 12:07:48 +01:00
|
|
|
/*
|
|
|
|
* http://stackoverflow.com/questions/7292642/grabbing-output-from-exec
|
|
|
|
*/
|
|
|
|
|
2015-03-17 12:03:45 +01:00
|
|
|
if(pipe(link) == -1){
|
|
|
|
syslog(LOG_PRIORITY, "%s: cannot open link", sdata->ttmpfile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((pid = fork()) == -1){
|
|
|
|
syslog(LOG_PRIORITY, "%s: cannot fork", sdata->ttmpfile);
|
2015-03-17 12:07:48 +01:00
|
|
|
close(link[0]);
|
|
|
|
close(link[1]);
|
2015-03-17 12:03:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pid == 0){
|
|
|
|
dup2(link[1], STDOUT_FILENO);
|
|
|
|
close(link[0]);
|
|
|
|
close(link[1]);
|
|
|
|
|
|
|
|
alarm(cfg->helper_timeout);
|
|
|
|
sig_catch(SIGALRM, kill_helper);
|
|
|
|
|
|
|
|
#ifdef HAVE_PDFTOTEXT
|
|
|
|
if(strcmp(type, "pdf") == 0) execl(HAVE_PDFTOTEXT, HAVE_PDFTOTEXT, "-enc", "UTF-8", filename, "-", (char *) 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_CATDOC
|
|
|
|
if(strcmp(type, "doc") == 0) execl(HAVE_CATDOC, HAVE_CATDOC, "-d", "utf-8", filename, (char *) 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_CATPPT
|
|
|
|
if(strcmp(type, "ppt") == 0) execl(HAVE_CATPPT, HAVE_CATPPT, "-d", "utf-8", filename, (char *) 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XLS2CSV
|
|
|
|
if(strcmp(type, "xls") == 0) execl(HAVE_XLS2CSV, HAVE_XLS2CSV, "-d", "utf-8", filename, (char *) 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PPTHTML
|
|
|
|
if(strcmp(type, "ppt") == 0) execl(HAVE_PPTHTML, HAVE_PPTHTML, filename, (char *) 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNRTF
|
|
|
|
if(strcmp(type, "rtf") == 0) execl(HAVE_UNRTF, HAVE_UNRTF, "--text", filename, (char *) 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
die("execl");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
close(link[1]);
|
|
|
|
while((n = read(link[0], outbuf, sizeof(outbuf))) > 0){
|
|
|
|
if(state->bodylen < BIGBUFSIZE-n-1){
|
|
|
|
memcpy(&(state->b_body[state->bodylen]), outbuf, n);
|
|
|
|
state->bodylen += n;
|
|
|
|
}
|
|
|
|
//printf("Output: %.*s\n", n, outbuf);
|
|
|
|
}
|
|
|
|
wait(NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-09 23:16:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|