libclamav/mbox.c
b151ef55
 /*
e82a5185
  *  Copyright (C) 2002-2006 Nigel Horne <njh@bandsman.co.uk>
b151ef55
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
e82a5185
 static	char	const	rcsid[] = "$Id: mbox.c,v 1.279 2006/02/06 02:36:39 nigelhorne Exp $";
8b242bb9
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
b151ef55
 
 #ifndef	CL_DEBUG
897fd9c7
 #define	NDEBUG	/* map CLAMAV debug onto standard */
b151ef55
 #endif
 
 #ifdef CL_THREAD_SAFE
f5e9abc8
 #ifndef	_REENTRANT
b151ef55
 #define	_REENTRANT	/* for Solaris 2.8 */
 #endif
f5e9abc8
 #endif
b151ef55
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <assert.h>
 #include <string.h>
 #include <strings.h>
 #include <ctype.h>
 #include <time.h>
 #include <fcntl.h>
0bcad2b1
 #include <sys/param.h>
b151ef55
 #include <clamav.h>
9a7398ee
 #include <dirent.h>
67a25177
 #include <limits.h>
b151ef55
 
a77dc192
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
 #include <stddef.h>
 #endif
 
8a88fb93
 #ifdef	CL_THREAD_SAFE
 #include <pthread.h>
 #endif
 
b151ef55
 #include "table.h"
 #include "mbox.h"
 #include "blob.h"
de617e3e
 #include "line.h"
b151ef55
 #include "text.h"
 #include "message.h"
 #include "others.h"
 #include "defaults.h"
7fca6080
 #include "str.h"
22f3b19b
 #include "filetypes.h"
e82a5185
 #include "uuencode.h"
b151ef55
 
98685ac1
 #ifdef	CL_DEBUG
 #if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 1
 #define HAVE_BACKTRACE
 #endif
89e9a596
 #endif
98685ac1
 
 #ifdef HAVE_BACKTRACE
 #include <execinfo.h>
 #include <signal.h>
 #include <syslog.h>
 
 static	void	sigsegv(int sig);
 static	void	print_trace(int use_syslog);
 #endif
 
07cbf822
 #if	defined(NO_STRTOK_R) || !defined(CL_THREAD_SAFE)
b151ef55
 #undef strtok_r
 #undef __strtok_r
 #define strtok_r(a,b,c)	strtok(a,b)
 #endif
 
 /* required for AIX and Tru64 */
 #ifdef TRUE
 #undef TRUE
 #endif
 #ifdef FALSE
 #undef FALSE
 #endif
 
6b93ea0c
 typedef enum	{ FALSE = 0, TRUE = 1 } bool;
 
e82a5185
 #ifndef isblank
 #define isblank(c)	(((c) == ' ') || ((c) == '\t'))
 #endif
 
3fa72383
 #define	SAVE_TO_DISC	/* multipart/message are saved in a temporary file */
49674596
 
e9bdeb72
 /*
  * Code does exist to run FOLLORURLS on systems without libcurl, however that
  * is not recommended so it is not compiled by default
cbe29191
  *
dd35cb90
  * On Solaris, when using the GNU C compiler, the clamAV build system uses the
  * Sun supplied ld instead of the GNU ld causing an error. Therefore you cannot
  * use WITH_CURL on Solaris with gcc, you must configure with
  * "--without-libcurl". I don't know if it works with Sun's own compiler
  *
cbe29191
  * Fails to link on Solaris 10 with this error:
  *      Undefined                       first referenced
  *  symbol                             in file
  *  __floatdidf                         /opt/sfw/lib/libcurl.s
e9bdeb72
  */
dd35cb90
 #if	C_SOLARIS && __GNUC__
cbe29191
 #undef	WITH_CURL
 #endif
 
e9bdeb72
 #ifdef	WITH_CURL
f52d7358
 #define	FOLLOWURLS	5	/*
 				 * Maximum number of URLs scanned in a message
e82a5185
 				 * part. Helps to find Dialer.gen-45. If
f52d7358
 				 * not defined, don't check any URLs
 				 */
e9bdeb72
 #endif
3fa72383
 
3eb12bae
 #ifdef	FOLLOWURLS
da812a6a
 
6da40aa1
 #include "htmlnorm.h"
 
da812a6a
 #ifdef	WITH_CURL	/* Set in configure */
 /*
  * To build with WITH_CURL:
  * LDFLAGS=`curl-config --libs` ./configure ...
  */
88771ffa
 #include <curl/curl.h>
6736d46f
 
 /*
e82a5185
  * Needs curl >= 7.11 (I've heard that 7.9 can cause crashes and I have seen
  *	7.10 segfault, later versions can be flakey as well)
6736d46f
  * untested)
  */
e82a5185
 #if     (LIBCURL_VERSION_NUM < 0x070B00)
6736d46f
 #undef	WITH_CURL	/* also undef FOLLOWURLS? */
 #endif
 
 #endif	/*WITH_CURL*/
 
3eb12bae
 #else	/*!FOLLOWURLS*/
 #undef	WITH_CURL
6736d46f
 #endif	/*FOLLOWURLS*/
88771ffa
 
9a7398ee
 /*
c29ebe66
  * Define this to handle messages covered by section 7.3.2 of RFC1341.
9a7398ee
  *	This is experimental code so it is up to YOU to (1) ensure it's secure
291ac47f
  * (2) periodically trim the directory of old files
  *
  * If you use the load balancing feature of clamav-milter to run clamd on
d85c1fad
  * more than one machine you must make sure that .../partial is on a shared
291ac47f
  * network filesystem
9a7398ee
  */
d85c1fad
 #define	PARTIAL_DIR
9a7398ee
 
273cd2bb
 /*#define	NEW_WORLD*/
 
00615ec9
 static	int	cli_parse_mbox(const char *dir, int desc, unsigned int options);
b3a5cdd8
 static	message	*parseEmailFile(FILE *fin, const table_t *rfc821Table, const char *firstLine, const char *dir);
de617e3e
 static	message	*parseEmailHeaders(const message *m, const table_t *rfc821Table);
8c0250d5
 static	int	parseEmailHeader(message *m, const char *line, const table_t *rfc821Table);
565c449d
 static	int	parseEmailBody(message *messageIn, text *textIn, const char *dir, const table_t *rfc821Table, const table_t *subtypeTable, unsigned int options);
b151ef55
 static	int	boundaryStart(const char *line, const char *boundary);
 static	int	endOfMessage(const char *line, const char *boundary);
 static	int	initialiseTables(table_t **rfc821Table, table_t **subtypeTable);
 static	int	getTextPart(message *const messages[], size_t size);
 static	size_t	strip(char *buf, int len);
 static	bool	continuationMarker(const char *line);
 static	int	parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const char *arg);
5a01973c
 static	void	saveTextPart(message *m, const char *dir);
0674e2af
 static	char	*rfc2047(const char *in);
273cd2bb
 static	char	*rfc822comments(const char *in, char *out);
9a7398ee
 #ifdef	PARTIAL_DIR
 static	int	rfc1341(message *m, const char *dir);
 #endif
82933497
 static	bool	usefulHeader(int commandNumber, const char *cmd);
c840fa41
 static	char	*getline_from_mbox(char *buffer, size_t len, FILE *fin);
3fa72383
 
c5ed8336
 static	void	checkURLs(message *m, const char *dir);
da812a6a
 #ifdef	WITH_CURL
314ff77b
 struct arg {
a95c894a
 	const char *url;
 	const char *dir;
314ff77b
 	char *filename;
 };
 #ifdef	CL_THREAD_SAFE
 static	void	*getURL(void *a);
 #else
 static	void	*getURL(struct arg *arg);
 #endif
3fa72383
 #endif
 
b151ef55
 /* Maximum line length according to RFC821 */
e82a5185
 #define	RFC2821LENGTH	1000
b151ef55
 
 /* Hashcodes for our hash tables */
 #define	CONTENT_TYPE			1
 #define	CONTENT_TRANSFER_ENCODING	2
 #define	CONTENT_DISPOSITION		3
 
 /* Mime sub types */
 #define	PLAIN		1
 #define	ENRICHED	2
 #define	HTML		3
 #define	RICHTEXT	4
 #define	MIXED		5
 #define	ALTERNATIVE	6
 #define	DIGEST		7
 #define	SIGNED		8
 #define	PARALLEL	9
 #define	RELATED		10	/* RFC2387 */
 #define	REPORT		11	/* RFC1892 */
fdc8a467
 #define	APPLEDOUBLE	12	/* Handling of this in only noddy for now */
49674596
 #define	FAX		MIXED	/*
 				 * RFC3458
 				 * Drafts stated to treat is as mixed if it is
 				 * not known.  This disappeared in the final
 				 * version (except when talking about
 				 * voice-message), but it is good enough for us
 				 * since we do no validation of coversheet
 				 * presence etc. (which also has disappeared
 				 * in the final version)
 				 */
b62a19da
 #define	ENCRYPTED	13	/*
 				 * e.g. RFC2015
 				 * Content-Type: multipart/encrypted;
 				 * boundary="nextPart1383049.XCRrrar2yq";
 				 * protocol="application/pgp-encrypted"
 				 */
db09f781
 #define	X_BFILE		RELATED	/*
 				 * BeOS, expert two parts: the file and it's
 				 * attributes. The attributes part comes as
 				 *	Content-Type: application/x-be_attribute
 				 *		name="foo"
 				 * I can't find where it is defined, any
 				 * pointers would be appreciated. For now
 				 * we treat it as multipart/related
 				 */
cbc2eaa9
 #define	KNOWBOT		14	/* Unknown and undocumented format? */
b151ef55
 
 static	const	struct tableinit {
 	const	char	*key;
 	int	value;
 } rfc821headers[] = {
68badbc1
 	/* TODO: make these regular expressions */
b759d5eb
 	{	"Content-Type",			CONTENT_TYPE		},
39ff42ee
 	{	"Content-Transfer-Encoding",	CONTENT_TRANSFER_ENCODING	},
 	{	"Content-Disposition",		CONTENT_DISPOSITION	},
b151ef55
 	{	NULL,				0			}
4fc38d69
 }, mimeSubtypes[] = {	/* see RFC2045 */
b151ef55
 		/* subtypes of Text */
 	{	"plain",	PLAIN		},
 	{	"enriched",	ENRICHED	},
 	{	"html",		HTML		},
 	{	"richtext",	RICHTEXT	},
 		/* subtypes of Multipart */
 	{	"mixed",	MIXED		},
 	{	"alternative",	ALTERNATIVE	},
 	{	"digest",	DIGEST		},
 	{	"signed",	SIGNED		},
 	{	"parallel",	PARALLEL	},
 	{	"related",	RELATED		},
 	{	"report",	REPORT		},
fdc8a467
 	{	"appledouble",	APPLEDOUBLE	},
49674596
 	{	"fax-message",	FAX		},
b62a19da
 	{	"encrypted",	ENCRYPTED	},
db09f781
 	{	"x-bfile",	X_BFILE		},	/* BeOS */
cbc2eaa9
 	{	"knowbot",		KNOWBOT		},	/* ??? */
 	{	"knowbot-metadata",	KNOWBOT		},	/* ??? */
 	{	"knowbot-code",		KNOWBOT		},	/* ??? */
 	{	"knowbot-state",	KNOWBOT		},	/* ??? */
b151ef55
 	{	NULL,		0		}
 };
8a88fb93
 
 #ifdef	CL_THREAD_SAFE
 static	pthread_mutex_t	tables_mutex = PTHREAD_MUTEX_INITIALIZER;
 #endif
b151ef55
 
0dbec6b9
 #ifndef	O_BINARY
 #define	O_BINARY	0
 #endif
 
273cd2bb
 #ifdef	NEW_WORLD
00615ec9
 
e82a5185
 #undef	PARTIAL_DIR
 
00615ec9
 #if HAVE_MMAP
 #if HAVE_SYS_MMAN_H
 #include <sys/mman.h>
 #else /* HAVE_SYS_MMAN_H */
 #undef HAVE_MMAP
 #endif
e82a5185
 #else	/*HAVE_MMAP*/
 #undef	NEW_WORLD
 #endif
00615ec9
 #endif
 
e82a5185
 #ifdef	NEW_WORLD
 /*
  * Files larger than this are scanned with the old method, should be
  *	StreamMaxLength, I guess
  * If NW_MAX_FILE_SIZE is not defined, all files go through the
  *	new method. This definition is for machines very tight on RAM, or
  *	with large StreamMaxLength values
  */
 #define	MAX_ALLOCATION	134217728	/* see libclamav/others.c */
 #define	NW_MAX_FILE_SIZE	MAX_ALLOCATION
 
273cd2bb
 struct scanlist {
e82a5185
 	const	char	*start;
 	size_t	size;
 	encoding_type	decoder;	/* only BASE64 and QUOTEDPRINTABLE for now */
 	struct	scanlist *next;
273cd2bb
 };
 
e82a5185
 static struct map {
 	const	char	*offset;	/* sorted */
 	const	char	*word;
 	struct	map	*next;
 } *map, *tail;
 
 static	void	create_map(const char *begin, const char *end);
 static	void	add_to_map(const char *offset, const char *word);
 static	const	char	*find_in_map(const char *offset, const char *word);
 static	void	free_map(void);
 
00615ec9
 /*
  * This could be the future. Instead of parsing and decoding it just decodes.
273cd2bb
  *
00615ec9
  * USE IT AT YOUR PERIL, a large number of viruses are not detected with this
  * method, possibly because the decoded files must be exact and not have
  * extra data at the start or end, which this code will produce.
53ee0b60
  *
273cd2bb
  * Currently only supports base64 and quoted-printable
  *
  * You may also see a lot of warnings. For the moment it falls back to old
  *	world mode if it doesn't know what to do - that'll be removed.
  * The code is untidy...
  *
  * FIXME: Some mailbox scans are slower with this method. I suspect that it's
  * because the scan can proceed to the end of the file rather than the end
  * of the attachment which can mean than later emails are scanned many times
9c8806fb
  *
e82a5185
  * FIXME: quoted printable doesn't know when to stop, so size related virus
  *	matching breaks
  *
  * TODO: Fall through to cli_parse_mbox() too often
  *
  * TODO: Add support for systems without mmap()
  *
  * TODO: partial_dir fall through
00615ec9
  */
 int
 cli_mbox(const char *dir, int desc, unsigned int options)
 {
e82a5185
 	char *start, *ptr, *line;
 	const char *last, *p, *q;
 	size_t size;
00615ec9
 	struct stat statb;
 	message *m;
 	fileblob *fb;
53ee0b60
 	int ret = 0;
8c2e0f32
 	int wasAlloced;
273cd2bb
 	struct scanlist *scanlist, *scanelem;
00615ec9
 
f60a8d41
 	if(dir == NULL) {
 		cli_warnmsg("cli_mbox called with NULL dir\n");
 		return CL_ENULLARG;
 	}
00615ec9
 	if(fstat(desc, &statb) < 0)
 		return CL_EOPEN;
 
 	size = statb.st_size;
 
 	if(size == 0)
 		return CL_CLEAN;
 
e82a5185
 #ifdef	NW_MAX_FILE_SIZE
 	if(size > NW_MAX_FILE_SIZE)
 		return cli_parse_mbox(dir, desc, options);
 #endif
00615ec9
 
e82a5185
 	/*cli_warnmsg("NEW_WORLD is new code - use at your own risk.\n");*/
 #ifdef	PARTIAL_DIR
 	cli_warnmsg("PARTIAL_DIR doesn't work in the NEW_WORLD yet\n");
 #endif
0d9e07a9
 
273cd2bb
 	start = mmap(NULL, size, PROT_READ, MAP_PRIVATE, desc, 0);
 	if(start == MAP_FAILED)
00615ec9
 		return CL_EMEM;
 
 	cli_dbgmsg("mmap'ed mbox\n");
 
273cd2bb
 	ptr = cli_malloc(size);
 	if(ptr) {
 		memcpy(ptr, start, size);
8c2e0f32
 		munmap(start, size);
273cd2bb
 		start = ptr;
e82a5185
 		wasAlloced = 1;
8c2e0f32
 	} else
 		wasAlloced = 0;
 
e82a5185
 	/* last points to the last *valid* address in the array */
 	last = &start[size - 1];
 
 	create_map(start, last);
 
273cd2bb
 	scanelem = scanlist = NULL;
 	q = start;
e82a5185
 	/*
 	 * FIXME: mismatch of const char * and char * here and in later calls
 	 *	to find_in_map()
 	 */
 	while((p = find_in_map(q, "base64")) != NULL) {
273cd2bb
 		cli_dbgmsg("Found base64\n");
 		if(scanelem) {
 			scanelem->next = cli_malloc(sizeof(struct scanlist));
 			scanelem = scanelem->next;
 		} else
 			scanlist = scanelem = cli_malloc(sizeof(struct scanlist));
 		scanelem->next = NULL;
 		scanelem->decoder = BASE64;
 		q = scanelem->start = &p[6];
e82a5185
 		if(((p = find_in_map(q, "\nFrom ")) != NULL) ||
 		   ((p = find_in_map(q, "base64")) != NULL) ||
 		   ((p = find_in_map(q, "quoted-printable")) != NULL)) {
64ff0d49
 			scanelem->size = (size_t)(p - q);
273cd2bb
 			q = p;
9c8806fb
 		} else {
 			scanelem->size = (size_t)(last - scanelem->start) + 1;
 			break;
 		}
e82a5185
 		cli_dbgmsg("base64: last %u q %u\n", (unsigned int)last, (unsigned int)q);
273cd2bb
 		assert(scanelem->size <= size);
00615ec9
 	}
e82a5185
 
273cd2bb
 	q = start;
e82a5185
 	while((p = find_in_map(q, "quoted-printable")) != NULL) {
9c8806fb
 		if(p != q)
 			switch(p[-1]) {
 				case ' ':
 				case ':':
 				case '=':	/* wrong but allow it */
 					break;
 				default:
 					q = &p[16];
 					cli_dbgmsg("Ignore quoted-printable false positive\n");
 					continue;	/* false positive */
 			}
 
273cd2bb
 		cli_dbgmsg("Found quoted-printable\n");
e82a5185
 #ifdef	notdef
 		/*
 		 * The problem with quoted printable is recognising when to stop
 		 * parsing
 		 */
273cd2bb
 		if(scanelem) {
 			scanelem->next = cli_malloc(sizeof(struct scanlist));
 			scanelem = scanelem->next;
 		} else
 			scanlist = scanelem = cli_malloc(sizeof(struct scanlist));
 		scanelem->next = NULL;
 		scanelem->decoder = QUOTEDPRINTABLE;
 		q = scanelem->start = &p[16];
e82a5185
 		cli_dbgmsg("qp: last %u q %u\n", (unsigned int)last, (unsigned int)q);
 		if(((p = find_in_map(q, "\nFrom ")) != NULL) ||
 		   ((p = find_in_map(q, "quoted-printable")) != NULL) ||
 		   ((p = find_in_map(q, "base64")) != NULL)) {
64ff0d49
 			scanelem->size = (size_t)(p - q);
273cd2bb
 			q = p;
9c8806fb
 			cli_dbgmsg("qp: scanelem->size = %u\n", scanelem->size);
 		} else {
 			scanelem->size = (size_t)(last - scanelem->start) + 1;
 			break;
 		}
273cd2bb
 		assert(scanelem->size <= size);
e82a5185
 #else
 		if(wasAlloced)
 			free(start);
 		else
 			munmap(start, size);
 
 		free_map();
 		return cli_parse_mbox(dir, desc, options);
 #endif
00615ec9
 	}
 
273cd2bb
 	if(scanlist == NULL) {
 		const struct tableinit *tableinit;
 		bool anyHeadersFound = FALSE;
9c8806fb
 		bool hasuuencode = FALSE;
e82a5185
 		cli_file_t type;
273cd2bb
 
 		/* FIXME: message: There could of course be no decoder needed... */
 		for(tableinit = rfc821headers; tableinit->key; tableinit++)
e82a5185
 			if(find_in_map(start, tableinit->key)) {
273cd2bb
 				anyHeadersFound = TRUE;
 				break;
 			}
 
e82a5185
 		if((!anyHeadersFound) && find_in_map(start, "\nbegin "))
9c8806fb
 			/* uuencoded part */
 			hasuuencode = TRUE;
 
e82a5185
 		free_map();
 
 		type = cli_filetype(start, size);
 
 		if((type == CL_TYPE_UNKNOWN_TEXT) &&
 		   (strncmp(start, "Microsoft Mail Internet Headers", 31) == 0))
 			type = CL_TYPE_MAIL;
 
8c2e0f32
 		if(wasAlloced)
 			free(start);
 		else
 			munmap(start, size);
00615ec9
 
9c8806fb
 		if(anyHeadersFound || hasuuencode) {
 			/* TODO: reduce the number of falls through here */
e82a5185
 			if(hasuuencode)
 				cli_dbgmsg("New world - fall back to old uudecoder, type %d\n", type);
 			else
 				cli_dbgmsg("cli_mbox: unknown encoder, type %d\n", type);
 			if(type == CL_TYPE_MAIL)
 				return cli_parse_mbox(dir, desc, options);
 			cli_dbgmsg("Unknown filetype %d, return CLEAN\n", type);
 			return CL_CLEAN;
53ee0b60
 		}
9c8806fb
 
e82a5185
 		/* The message could be a plain text phish */
 		if((type == CL_TYPE_MAIL) && (!(options&CL_DB_NOPHISHING)))
 			return cli_parse_mbox(dir, desc, options);
 		cli_dbgmsg("cli_mbox: I believe it's plain text which must be clean\n");
273cd2bb
 		return CL_CLEAN;
 	}
e82a5185
 	free_map();
 
 #if	0
 	if(wasAlloced) {
 		const char *max = NULL;
 
 		for(scanelem = scanlist; scanelem; scanelem = scanelem->next) {
 			const char *end = &scanelem->start[scanelem->size];
 
 			if(end > max)
 				max = end;
 		}
 
 		if(max < last)
 			printf("could free %d bytes\n", (int)(last - max));
 	}
 #endif
273cd2bb
 
 	for(scanelem = scanlist; scanelem; scanelem = scanelem->next) {
 		if(scanelem->decoder == BASE64) {
e82a5185
 			const char *b64start = scanelem->start;
 			size_t b64size = scanelem->size;
273cd2bb
 
 			cli_dbgmsg("b64size = %lu\n", b64size);
e82a5185
 			while((*b64start != '\n') && (*b64start != '\r')) {
53ee0b60
 				b64start++;
 				b64size--;
273cd2bb
 			}
 			/*
 			 * Look for the end of the headers
 			 */
 			while(b64start < last) {
 				if(*b64start == ';') {
53ee0b60
 					b64start++;
 					b64size--;
e82a5185
 				} else if((memcmp(b64start, "\n\n", 2) == 0) ||
 					  (memcmp(b64start, "\r\r", 2) == 0)) {
 					b64start += 2;
 					b64size -= 2;
 					break;
 				} else if(memcmp(b64start, "\r\n\r\n", 4) == 0) {
 					b64start += 4;
 					b64size -= 4;
 					break;
 				} else if(memcmp(b64start, "\n \n", 3) == 0) {
 					/*
 					 * Some viruses are broken and have
 					 * one space character at the end of
 					 * the headers
 					 */
 					b64start += 3;
 					b64size -= 3;
 					break;
 				} else if(memcmp(b64start, "\r\n \r\n", 5) == 0) {
 					/*
 					 * Some viruses are broken and have
 					 * one space character at the end of
 					 * the headers
 					 */
 					b64start += 5;
 					b64size -= 5;
 					break;
53ee0b60
 				}
6c4485f9
 				b64start++;
273cd2bb
 				b64size--;
6c4485f9
 			}
53ee0b60
 
273cd2bb
 			if(b64size > 0L)
9c8806fb
 				while((!isalnum(*b64start)) && (*b64start != '/')) {
273cd2bb
 					if(b64size-- == 0L)
 						break;
 					b64start++;
 				}
 
 			if(b64size > 0L) {
e82a5185
 				int lastline;
273cd2bb
 				cli_dbgmsg("cli_mbox: decoding %ld base64 bytes\n", b64size);
53ee0b60
 
273cd2bb
 				line = NULL;
53ee0b60
 
273cd2bb
 				m = messageCreate();
e82a5185
 				if(m == NULL) {
 					if(wasAlloced)
 						free(start);
 					else
 						munmap(start, size);
 
273cd2bb
 					return CL_EMEM;
e82a5185
 				}
273cd2bb
 				messageSetEncoding(m, "base64");
53ee0b60
 
e82a5185
 				lastline = 0;
 
9c8806fb
 				do {
273cd2bb
 					int length = 0;
e82a5185
 					char *newline, *equal;
53ee0b60
 
273cd2bb
 					/*printf("%ld: ", b64size); fflush(stdout);*/
53ee0b60
 
273cd2bb
 					for(ptr = b64start; b64size && (*ptr != '\n') && (*ptr != '\r'); ptr++) {
 						length++;
 						--b64size;
 					}
53ee0b60
 
273cd2bb
 					/*printf("%d: ", length); fflush(stdout);*/
53ee0b60
 
e82a5185
 					newline = cli_realloc(line, length + 1);
 					if(newline == NULL)
 						break;
 					line = newline;
53ee0b60
 
273cd2bb
 					memcpy(line, b64start, length);
 					line[length] = '\0';
53ee0b60
 
e82a5185
 					equal = strchr(line, '=');
 					if(equal) {
 						lastline++;
 						*equal = '\0';
 					}
273cd2bb
 					/*puts(line);*/
53ee0b60
 
273cd2bb
 					if(messageAddStr(m, line) < 0)
 						break;
53ee0b60
 
273cd2bb
 					if((b64size > 0) && (*ptr == '\r')) {
e82a5185
 						b64start = ++ptr;
 						--b64size;
 					}
 					if((b64size > 0) && (*ptr == '\n')) {
 						b64start = ++ptr;
273cd2bb
 						--b64size;
 					}
e82a5185
 					if(lastline)
273cd2bb
 						break;
9c8806fb
 				} while(b64size > 0L);
 
273cd2bb
 				free(line);
 				fb = messageToFileblob(m, dir);
 				messageDestroy(m);
 
 				if(fb)
 					fileblobDestroy(fb);
 				else
 					ret = -1;
00615ec9
 			}
273cd2bb
 		} else if(scanelem->decoder == QUOTEDPRINTABLE) {
e82a5185
 			const char *quotedstart = scanelem->start;
 			size_t quotedsize = scanelem->size;
53ee0b60
 
273cd2bb
 			cli_dbgmsg("quotedsize = %lu\n", quotedsize);
 			while(*quotedstart != '\n') {
53ee0b60
 				quotedstart++;
 				quotedsize--;
273cd2bb
 			}
 			/*
 			 * Look for the end of the headers
 			 */
 			while(quotedstart < last) {
 				if(*quotedstart == ';') {
53ee0b60
 					quotedstart++;
 					quotedsize--;
e82a5185
 				} else if((*quotedstart == '\n') || (*quotedstart == '\r')) {
273cd2bb
 					quotedstart++;
 					quotedsize--;
 					if((*quotedstart == '\n') || (*quotedstart == '\r')) {
 						quotedstart++;
 						quotedsize--;
 						break;
 					}
53ee0b60
 				}
273cd2bb
 				quotedstart++;
 				quotedsize--;
53ee0b60
 			}
00615ec9
 
273cd2bb
 			while(!isalnum(*quotedstart)) {
 				quotedstart++;
 				quotedsize--;
 			}
00615ec9
 
273cd2bb
 			if(quotedsize > 0L) {
 				cli_dbgmsg("cli_mbox: decoding %ld quoted-printable bytes\n", quotedsize);
00615ec9
 
273cd2bb
 				m = messageCreate();
e82a5185
 				if(m == NULL) {
 					if(wasAlloced)
 						free(start);
 					else
 						munmap(start, size);
 
273cd2bb
 					return CL_EMEM;
e82a5185
 				}
273cd2bb
 				messageSetEncoding(m, "quoted-printable");
00615ec9
 
273cd2bb
 				line = NULL;
00615ec9
 
9c8806fb
 				do {
273cd2bb
 					int length = 0;
e82a5185
 					char *newline;
00615ec9
 
273cd2bb
 					/*printf("%ld: ", quotedsize); fflush(stdout);*/
00615ec9
 
273cd2bb
 					for(ptr = quotedstart; quotedsize && (*ptr != '\n') && (*ptr != '\r'); ptr++) {
 						length++;
 						--quotedsize;
 					}
00615ec9
 
273cd2bb
 					/*printf("%d: ", length); fflush(stdout);*/
00615ec9
 
e82a5185
 					newline = cli_realloc(line, length + 1);
 					if(newline == NULL)
 						break;
 					line = newline;
00615ec9
 
273cd2bb
 					memcpy(line, quotedstart, length);
 					line[length] = '\0';
00615ec9
 
273cd2bb
 					/*puts(line);*/
00615ec9
 
273cd2bb
 					if(messageAddStr(m, line) < 0)
 						break;
53ee0b60
 
273cd2bb
 					if((quotedsize > 0) && (*ptr == '\r')) {
e82a5185
 						quotedstart = ++ptr;
 						--quotedsize;
 					}
 					if((quotedsize > 0) && (*ptr == '\n')) {
 						quotedstart = ++ptr;
273cd2bb
 						--quotedsize;
 					}
9c8806fb
 				} while(quotedsize > 0L);
 
273cd2bb
 				free(line);
 				fb = messageToFileblob(m, dir);
 				messageDestroy(m);
53ee0b60
 
273cd2bb
 				if(fb)
 					fileblobDestroy(fb);
 				else
 					ret = -1;
 			}
00615ec9
 		}
 	}
273cd2bb
 	scanelem = scanlist;
 
 	while(scanelem) {
 		struct scanlist *n = scanelem->next;
 
 		free(scanelem);
 		scanelem = n;
 	}
00615ec9
 
8c2e0f32
 	if(wasAlloced)
 		free(start);
 	else
 		munmap(start, size);
00615ec9
 
8c2e0f32
 	/*
 	 * FIXME: Need to run cl_scandir() here and return that value
 	 */
53ee0b60
 	if(ret == 0)
00615ec9
 		return CL_CLEAN;	/* a lie - but it gets things going */
53ee0b60
 
e82a5185
 	cli_dbgmsg("New world - don't know what to do - fall back to old world\n");
273cd2bb
 	/* Fall back for now */
9c8806fb
 	lseek(desc, 0L, SEEK_SET);
00615ec9
 	return cli_parse_mbox(dir, desc, options);
 }
e82a5185
 
 static void
 create_map(const char *begin, const char *end)
 {
 	const struct wordlist {
 		const char *word;
 		int len;
 	} wordlist[] = {
 		{	"base64",		6	},
 		{	"quoted-printable",	16	},
 		{	"\nbegin ",		7	},
 		{	"\nFrom ",		7	},
 		{	NULL,			0	}
 	};
 
 	if(map) {
 		cli_warnmsg("create_map called without free_map\n");
 		free_map();
 	}
 	while(begin < end) {
 		const struct wordlist *word;
 
 		for(word = wordlist; word->word; word++) {
 			if((end - begin) < word->len)
 				continue;
 			if(strncasecmp(begin, word->word, word->len) == 0) {
 				add_to_map(begin, word->word);
 				break;
 			}
 		}
 		begin++;
 	}
 }
 
 /* To sort map, assume 'offset' is presented in sorted order */
 static void
 add_to_map(const char *offset, const char *word)
 {
 	if(map) {
 		tail->next = cli_malloc(sizeof(struct map));	/* FIXME: verify */
 		tail = tail->next;
 	} else
 		map = tail = cli_malloc(sizeof(struct map));	/* FIXME: verify */
 
 	tail->offset = offset;
 	tail->word = word;
 	tail->next = NULL;
 }
 
 static const char *
 find_in_map(const char *offset, const char *word)
 {
 	const struct map *item;
 
 	for(item = map; item; item = item->next)
 		if(item->offset >= offset)
 			if(strcasecmp(word, item->word) == 0)
 				return item->offset;
 
 	return NULL;
 }
 
 static void
 free_map(void)
 {
 	while(map) {
 		struct map *next = map->next;
 
 		free(map);
 		map = next;
 	}
 	map = NULL;
 }
 
 #else	/*!NEW_WORLD*/
00615ec9
 int
 cli_mbox(const char *dir, int desc, unsigned int options)
 {
f60a8d41
 	if(dir == NULL) {
 		cli_warnmsg("cli_mbox called with NULL dir\n");
 		return CL_ENULLARG;
 	}
00615ec9
 	return cli_parse_mbox(dir, desc, options);
 }
 #endif
 
b151ef55
 /*
  * TODO: when signal handling is added, need to remove temp files when a
7d3d11d0
  *	signal is received
b151ef55
  * TODO: add option to scan in memory not via temp files, perhaps with a
74b5c349
  * named pipe or memory mapped file, though this won't work on big e-mails
  * containing many levels of encapsulated messages - it'd just take too much
  * RAM
c6259ac5
  * TODO: parse .msg format files
fdc8a467
  * TODO: fully handle AppleDouble format, see
7d3d11d0
  *	http://www.lazerware.com/formats/Specs/AppleSingle_AppleDouble.pdf
f54a8635
  * TODO: ensure parseEmailHeaders is always called before parseEmailBody
  * TODO: create parseEmail which calls parseEmailHeaders then parseEmailBody
f1c33aa0
  * TODO: Look into TNEF. Is there anything that needs to be done here?
e82a5185
  * TODO: Handle unepected NUL bytes in header lines which stop strcmp()s:
  *	e.g. \0Content-Type: application/binary;
b151ef55
  */
00615ec9
 static int
 cli_parse_mbox(const char *dir, int desc, unsigned int options)
b151ef55
 {
c6259ac5
 	int retcode, i;
82933497
 	message *body;
b151ef55
 	FILE *fd;
e82a5185
 	char buffer[RFC2821LENGTH + 1];
0e3b08fc
 #ifdef HAVE_BACKTRACE
98685ac1
 	void (*segv)(int);
 #endif
49674596
 	static table_t *rfc821, *subtype;
11466e82
 #ifdef	CL_DEBUG
 	char tmpfilename[16];
 	int tmpfd;
 #endif
b151ef55
 
e82a5185
 #ifdef	NEW_WORLD
 	cli_dbgmsg("fall back to old world\n");
 #else
b151ef55
 	cli_dbgmsg("in mbox()\n");
e82a5185
 #endif
b151ef55
 
c6259ac5
 	i = dup(desc);
 	if((fd = fdopen(i, "rb")) == NULL) {
 		cli_errmsg("Can't open descriptor %d\n", desc);
 		close(i);
7d3d11d0
 		return CL_EOPEN;
c6259ac5
 	}
11466e82
 #ifdef	CL_DEBUG
 	/*
 	 * Copy the incoming mail for debugging, so that if it falls over
 	 * we have a copy of the offending email. This is debugging code
 	 * that you shouldn't of course install in a live environment. I am
 	 * not interested in hearing about security issues with this section
 	 * of the parser.
 	 */
 	strcpy(tmpfilename, "/tmp/mboxXXXXXX");
 	tmpfd = mkstemp(tmpfilename);
 	if(tmpfd < 0) {
 		perror(tmpfilename);
 		cli_errmsg("Can't make debugging file\n");
 	} else {
 		FILE *tmpfp = fdopen(tmpfd, "w");
 
 		if(tmpfp) {
 			while(fgets(buffer, sizeof(buffer) - 1, fd) != NULL)
 				fputs(buffer, tmpfp);
 			fclose(tmpfp);
 			rewind(fd);
 		} else
 			cli_errmsg("Can't fdopen debugging file\n");
 	}
 #endif
802c37fc
 	if(fgets(buffer, sizeof(buffer) - 1, fd) == NULL) {
c6259ac5
 		/* empty message */
 		fclose(fd);
11466e82
 #ifdef	CL_DEBUG
 		unlink(tmpfilename);
 #endif
7d3d11d0
 		return CL_CLEAN;
c6259ac5
 	}
8a88fb93
 #ifdef	CL_THREAD_SAFE
 	pthread_mutex_lock(&tables_mutex);
 #endif
49674596
 	if(rfc821 == NULL) {
 		assert(subtype == NULL);
7b8fb055
 
49674596
 		if(initialiseTables(&rfc821, &subtype) < 0) {
 			rfc821 = NULL;
 			subtype = NULL;
8a88fb93
 #ifdef	CL_THREAD_SAFE
 			pthread_mutex_unlock(&tables_mutex);
 #endif
7b8fb055
 			fclose(fd);
11466e82
 #ifdef	CL_DEBUG
 			unlink(tmpfilename);
 #endif
7d3d11d0
 			return CL_EMEM;
7b8fb055
 		}
b151ef55
 	}
8a88fb93
 #ifdef	CL_THREAD_SAFE
 	pthread_mutex_unlock(&tables_mutex);
 #endif
b151ef55
 
89e9a596
 #ifdef HAVE_BACKTRACE
98685ac1
 	segv = signal(SIGSEGV, sigsegv);
 #endif
 
f54a8635
 	/*
9c8806fb
 	 * Is it a UNIX style mbox with more than one
f54a8635
 	 * mail message, or just a single mail message?
9c8806fb
 	 *
 	 * TODO: It would be better if we called cli_scandir here rather than
 	 * in cli_scanmail. Then we could improve the way mailboxes with more
 	 * than one message is handled, e.g. stopping parsing when an infected
 	 * message is stopped, and giving a better indication of which message
 	 * within the mailbox is infected
f54a8635
 	 */
e82a5185
 	if((strncmp(buffer, "From ", 5) == 0) && isalnum(buffer[5])) {
b151ef55
 		/*
c6259ac5
 		 * Have been asked to check a UNIX style mbox file, which
 		 * may contain more than one e-mail message to decode
53ee0b60
 		 *
 		 * It would be far better for scanners.c to do this splitting
 		 * and do this
 		 *	FOR EACH mail in the mailbox
 		 *	DO
 		 *		pass this mail to cli_mbox --
 		 *		scan this file
 		 *		IF this file has a virus quit
 		 *		THEN
 		 *			return CL_VIRUS
 		 *		FI
 		 *	END
 		 * This would remove a problem with this code that it can
 		 * fill up the tmp directory before it starts scanning
b151ef55
 		 */
82933497
 		bool lastLineWasEmpty;
 		int messagenumber;
 		message *m = messageCreate();
 
 		if(m == NULL) {
 			fclose(fd);
 #ifdef HAVE_BACKTRACE
 			signal(SIGSEGV, segv);
 #endif
11466e82
 #ifdef	CL_DEBUG
 			unlink(tmpfilename);
 #endif
82933497
 			return CL_EMEM;
 		}
 
 		lastLineWasEmpty = FALSE;
 		messagenumber = 1;
b151ef55
 
c6259ac5
 		do {
f54a8635
 			cli_chomp(buffer);
e82a5185
 			if(lastLineWasEmpty && (strncmp(buffer, "From ", 5) == 0) && isalnum(buffer[5])) {
bf497d0a
 				cli_dbgmsg("Deal with email number %d\n", messagenumber++);
b151ef55
 				/*
f54a8635
 				 * End of a message in the mail box
b151ef55
 				 */
de617e3e
 				body = parseEmailHeaders(m, rfc821);
f2b068fb
 				if(body == NULL) {
 					messageReset(m);
 					continue;
 				}
f54a8635
 				messageDestroy(m);
 				if(messageGetBody(body))
565c449d
 					if(!parseEmailBody(body, NULL, dir, rfc821, subtype, options)) {
09ccd6e0
 						messageReset(body);
 						m = body;
 						continue;
 					}
b151ef55
 				/*
f54a8635
 				 * Starting a new message, throw away all the
00615ec9
 				 * information about the old one. It would
 				 * be best to be able to scan this message
 				 * now, but cli_scanfile needs arguments
 				 * that haven't been passed here so it can't be
 				 * called
b151ef55
 				 */
f54a8635
 				m = body;
 				messageReset(body);
b151ef55
 
c6259ac5
 				cli_dbgmsg("Finished processing message\n");
f54a8635
 			} else
a66ca28a
 				lastLineWasEmpty = (bool)(buffer[0] == '\0');
d1a6ea81
 
1ae303c2
 			if(isuuencodebegin(buffer)) {
64ff0d49
 				/*
d1a6ea81
 				 * Fast track visa to uudecode.
 				 * TODO: binhex, yenc
 				 */
e82a5185
 				if(uudecodeFile(m, buffer, dir, fd) < 0)
1ae303c2
 					if(messageAddStr(m, buffer) < 0)
e82a5185
 						break;
1ae303c2
 			} else
d1a6ea81
 				if(messageAddStr(m, buffer) < 0)
 					break;
802c37fc
 		} while(fgets(buffer, sizeof(buffer) - 1, fd) != NULL);
bf497d0a
 
82933497
 		fclose(fd);
 
00615ec9
 		cli_dbgmsg("Extract attachments from email %d\n", messagenumber);
82933497
 		body = parseEmailHeaders(m, rfc821);
 		messageDestroy(m);
f2b068fb
 	} else {
7fca6080
 		/*
 		 * It's a single message, parse the headers then the body
f2b068fb
 		 */
f1c33aa0
 		if(strncmp(buffer, "P I ", 4) == 0)
 			/*
 			 * CommuniGate Pro format: ignore headers until
 			 * blank line
 			 */
802c37fc
 			while((fgets(buffer, sizeof(buffer) - 1, fd) != NULL) &&
f1c33aa0
 				(strchr("\r\n", buffer[0]) == NULL))
 					;
 		/*
 		 * Ignore any blank lines at the top of the message
 		 */
f2b068fb
 		while(strchr("\r\n", buffer[0]) &&
c840fa41
 		     (getline_from_mbox(buffer, sizeof(buffer) - 1, fd) != NULL))
6b93ea0c
 			;
 
a78256af
 		buffer[sizeof(buffer) - 1] = '\0';
802c37fc
 
b3a5cdd8
 		body = parseEmailFile(fd, rfc821, buffer, dir);
82933497
 		fclose(fd);
f2b068fb
 	}
7fca6080
 
7d3d11d0
 	/*
 	 * This is not necessarily true, but since the only options are
 	 * CL_CLEAN and CL_VIRUS this is the better choice. It would be
 	 * nice to have CL_CONTINUESCANNING or something like that
 	 */
 	retcode = CL_CLEAN;
c6259ac5
 
f2b068fb
 	if(body) {
 		/*
 		 * Write out the last entry in the mailbox
 		 */
 		if(messageGetBody(body))
565c449d
 			if(!parseEmailBody(body, NULL, dir, rfc821, subtype, options))
e745ac7e
 				retcode = CL_EFORMAT;
b151ef55
 
f2b068fb
 		/*
 		 * Tidy up and quit
 		 */
 		messageDestroy(body);
 	}
b151ef55
 
 	cli_dbgmsg("cli_mbox returning %d\n", retcode);
 
89e9a596
 #ifdef HAVE_BACKTRACE
98685ac1
 	signal(SIGSEGV, segv);
 #endif
 
11466e82
 #ifdef	CL_DEBUG
 	unlink(tmpfilename);
 #endif
b151ef55
 	return retcode;
 }
 
 /*
82933497
  * Read in an email message from fin, parse it, and return the message
7fca6080
  *
82933497
  * FIXME: files full of new lines and nothing else are
  * handled ungracefully...
  */
 static message *
b3a5cdd8
 parseEmailFile(FILE *fin, const table_t *rfc821, const char *firstLine, const char *dir)
82933497
 {
 	bool inHeader = TRUE;
 	bool contMarker = FALSE;
7e0afd07
 	bool lastWasBlank = FALSE;
82933497
 	message *ret;
 	bool anyHeadersFound = FALSE;
 	int commandNumber = -1;
7e0afd07
 	char *fullline = NULL, *boundary = NULL;
82933497
 	size_t fulllinelength = 0;
e82a5185
 	char buffer[RFC2821LENGTH + 1];
82933497
 
 	cli_dbgmsg("parseEmailFile\n");
 
 	ret = messageCreate();
 	if(ret == NULL)
 		return NULL;
 
 	strcpy(buffer, firstLine);
 	do {
9c8806fb
 		char *line;
82933497
 
 		(void)cli_chomp(buffer);
 
9c8806fb
 		line = buffer;
 
 		if(line[0] == '\0')
 			line = NULL;
82933497
 
 		/*
 		 * Don't blank lines which are only spaces from headers,
 		 * otherwise they'll be treated as the end of header marker
 		 */
7e0afd07
 		if(lastWasBlank) {
 			lastWasBlank = FALSE;
 			if(boundaryStart(buffer, boundary)) {
 				cli_dbgmsg("Found a header line with space that should be blank\n");
 				inHeader = FALSE;
 			}
 		}
 		if(boundary) {
 			free(boundary);
 			boundary = NULL;
 		}
82933497
 		if(inHeader) {
e82a5185
 			cli_dbgmsg("parseEmailFile: check '%s' contMarker %d fullline %p\n",
7e0afd07
 				buffer ? buffer : "", (int)contMarker, fullline);
9c8806fb
 			if(line && isspace(line[0])) {
7e0afd07
 				char copy[sizeof(buffer)];
 
 				strcpy(copy, buffer);
 				strstrip(copy);
 				if(copy[0] == '\0') {
 					/*
097d9527
 					 * The header line contains only white
 					 * space. This is not the end of the
 					 * headers according to RFC2822, but
 					 * some MUAs will handle it as though
 					 * it were, and virus writers exploit
 					 * this bug. We can't just break from
 					 * the loop here since that would allow
 					 * other exploits such as inserting a
 					 * white space line before the
 					 * content-type line. So we just have
 					 * to make a best guess. Sigh.
7e0afd07
 					 */
 					if(fullline) {
 						if(parseEmailHeader(ret, fullline, rfc821) < 0)
 							continue;
 
 						free(fullline);
 						fullline = NULL;
 					}
097d9527
 					if((boundary = (char *)messageFindArgument(ret, "boundary")) != NULL) {
7e0afd07
 						lastWasBlank = TRUE;
 						continue;
 					}
 				}
 			}
 			lastWasBlank = FALSE;
9c8806fb
 			if((line == NULL) && (fullline == NULL)) {	/* empty line */
82933497
 				if(!contMarker) {
 					/*
 					 * A blank line signifies the end of
 					 * the header and the start of the text
 					 */
0c9750fa
 					if(!anyHeadersFound)
 						/* Ignore the junk at the top */
 						continue;
 
82933497
 					cli_dbgmsg("End of header information\n");
 					inHeader = FALSE;
 				} else
 					contMarker = FALSE;
 			} else {
 				char *ptr;
 				const char *qptr;
cdfc0f05
 				int lookahead;
82933497
 
 				if(fullline == NULL) {
e82a5185
 					char cmd[RFC2821LENGTH + 1], out[RFC2821LENGTH + 1];
82933497
 
 					/*
 					 * Continuation of line we're ignoring?
 					 */
e82a5185
 					if(isblank(line[0])) {
9c8806fb
 						contMarker = continuationMarker(line);
82933497
 						continue;
 					}
 
 					/*
 					 * Is this a header we're interested in?
 					 */
9c8806fb
 					if((strchr(line, ':') == NULL) ||
 					   (cli_strtokbuf(line, 0, ":", cmd) == NULL)) {
 						if(strncmp(line, "From ", 5) == 0)
82933497
 							anyHeadersFound = TRUE;
 						continue;
 					}
 
273cd2bb
 					ptr = rfc822comments(cmd, out);
82933497
 					commandNumber = tableFind(rfc821, ptr ? ptr : cmd);
 
 					switch(commandNumber) {
 						case CONTENT_TRANSFER_ENCODING:
 						case CONTENT_DISPOSITION:
 						case CONTENT_TYPE:
 							anyHeadersFound = TRUE;
 							break;
 						default:
 							if(!anyHeadersFound)
 								anyHeadersFound = usefulHeader(commandNumber, cmd);
 							continue;
 					}
9c8806fb
 					fullline = strdup(line);
 					fulllinelength = strlen(line) + 1;
 				} else if(line != NULL) {
 					fulllinelength += strlen(line);
e82a5185
 					ptr = cli_realloc(fullline, fulllinelength);
 					if(ptr == NULL)
 						continue;
 					fullline = ptr;
9c8806fb
 					strcat(fullline, line);
82933497
 				}
 
9c8806fb
 				if(line) {
 					contMarker = continuationMarker(line);
82933497
 
cdfc0f05
 					if(contMarker)
 						continue;
 				} else
 					contMarker = FALSE;
82933497
 
 				assert(fullline != NULL);
 
 				lookahead = getc(fin);
 				if(lookahead != EOF) {
 					ungetc(lookahead, fin);
 
 					/*
 					 * Section B.2 of RFC822 says TAB or
 					 * SPACE means a continuation of the
 					 * previous entry.
 					 *
 					 * Add all the arguments on the line
 					 */
e82a5185
 					if(isblank(lookahead))
82933497
 						continue;
 				}
 
9c8806fb
 				if(line) {
cdfc0f05
 					int quotes = 0;
 					for(qptr = fullline; *qptr; qptr++)
 						if(*qptr == '\"')
 							quotes++;
82933497
 
cdfc0f05
 					if(quotes & 1)
 						continue;
 				}
82933497
 
273cd2bb
 				ptr = rfc822comments(fullline, NULL);
82933497
 				if(ptr) {
 					free(fullline);
 					fullline = ptr;
 				}
 
 				if(parseEmailHeader(ret, fullline, rfc821) < 0)
 					continue;
 
 				free(fullline);
 				fullline = NULL;
 			}
1ae303c2
 		} else if(line && isuuencodebegin(line)) {
b3a5cdd8
 			/*
 			 * Fast track visa to uudecode.
 			 * TODO: binhex, yenc
 			 */
e82a5185
 			if(uudecodeFile(ret, line, dir, fin) < 0)
1ae303c2
 				if(messageAddStr(ret, line) < 0)
 					break;
 		} else
9c8806fb
 			if(messageAddStr(ret, line) < 0)
82933497
 				break;
c840fa41
 	} while(getline_from_mbox(buffer, sizeof(buffer) - 1, fin) != NULL);
82933497
 
 	if(fullline) {
 		if(*fullline) switch(commandNumber) {
 			case CONTENT_TRANSFER_ENCODING:
 			case CONTENT_DISPOSITION:
 			case CONTENT_TYPE:
17bd4b65
 				cli_dbgmsg("parseEmailHeaders: Fullline unparsed '%s'\n", fullline);
82933497
 		}
 		free(fullline);
 	}
 
 	if(!anyHeadersFound) {
 		/*
 		 * False positive in believing we have an e-mail when we don't
 		 */
 		messageDestroy(ret);
 		cli_dbgmsg("parseEmailFile: no headers found, assuming it isn't an email\n");
 		return NULL;
 	}
 
 	messageClean(ret);
 
 	cli_dbgmsg("parseEmailFile: return\n");
 
 	return ret;
 }
 
 /*
  * The given message contains a raw e-mail.
68be129f
  *
  * Returns the message's body with the correct arguments set
735377bc
  *
  * The downside of this approach is that for a short time we have two copies
  * of the message in memory, the upside is that it makes for easier parsing
  * of encapsulated messages, and in the long run uses less memory in those
  * scenarios
82933497
  *
  * TODO: remove the duplication with parseEmailFile
7fca6080
  */
68be129f
 static message *
de617e3e
 parseEmailHeaders(const message *m, const table_t *rfc821)
7fca6080
 {
68be129f
 	bool inHeader = TRUE;
de617e3e
 	const text *t;
f54a8635
 	message *ret;
f2b068fb
 	bool anyHeadersFound = FALSE;
4fc38d69
 	int commandNumber = -1;
d768ac5a
 	char *fullline = NULL;
ad642304
 	size_t fulllinelength = 0;
f54a8635
 
98685ac1
 	cli_dbgmsg("parseEmailHeaders\n");
 
f54a8635
 	if(m == NULL)
 		return NULL;
 
 	ret = messageCreate();
7fca6080
 
de617e3e
 	for(t = messageGetBody(m); t; t = t->t_next) {
 		const char *buffer;
7fca6080
 
de617e3e
 		if(t->t_line)
 			buffer = lineGetData(t->t_line);
 		else
98685ac1
 			buffer = NULL;
7fca6080
 
b4cb4486
 		if(inHeader) {
663e5963
 			cli_dbgmsg("parseEmailHeaders: check '%s'\n",
 				buffer ? buffer : "");
82933497
 			if(buffer == NULL) {
663e5963
 				/*
 				 * A blank line signifies the end of
 				 * the header and the start of the text
 				 */
 				cli_dbgmsg("End of header information\n");
 				inHeader = FALSE;
f52d7358
 				if(!anyHeadersFound) {
 					cli_dbgmsg("Nothing interesting in the header\n");
 					break;
 				}
ad642304
 			} else {
0856891e
 				char *ptr;
ad642304
 				const char *qptr;
 				int quotes;
0856891e
 
ad642304
 				if(fullline == NULL) {
e82a5185
 					char cmd[RFC2821LENGTH + 1];
9180b8bb
 
 					/*
 					 * Continuation of line we're ignoring?
 					 */
e82a5185
 					if(isblank(buffer[0]))
9180b8bb
 						continue;
 
 					/*
 					 * Is this a header we're interested in?
 					 */
0856891e
 					if((strchr(buffer, ':') == NULL) ||
 					   (cli_strtokbuf(buffer, 0, ":", cmd) == NULL)) {
 						if(strncmp(buffer, "From ", 5) == 0)
 							anyHeadersFound = TRUE;
9180b8bb
 						continue;
0856891e
 					}
9180b8bb
 
273cd2bb
 					ptr = rfc822comments(cmd, NULL);
a1c924f9
 					commandNumber = tableFind(rfc821, ptr ? ptr : cmd);
 					if(ptr)
 						free(ptr);
9180b8bb
 
 					switch(commandNumber) {
 						case CONTENT_TRANSFER_ENCODING:
 						case CONTENT_DISPOSITION:
 						case CONTENT_TYPE:
0856891e
 							anyHeadersFound = TRUE;
9180b8bb
 							break;
 						default:
82933497
 							if(!anyHeadersFound)
 								anyHeadersFound = usefulHeader(commandNumber, cmd);
9180b8bb
 							continue;
 					}
 					fullline = strdup(buffer);
 					fulllinelength = strlen(buffer) + 1;
 				} else if(buffer) {
 					fulllinelength += strlen(buffer);
e82a5185
 					ptr = cli_realloc(fullline, fulllinelength);
 					if(ptr == NULL)
 						continue;
 					fullline = ptr;
9180b8bb
 					strcat(fullline, buffer);
ad642304
 				}
3a0946f5
 
82933497
 				assert(fullline != NULL);
ad642304
 
82933497
 				if(t->t_next && (t->t_next->t_line != NULL))
ad642304
 					/*
0856891e
 					 * Section B.2 of RFC822 says TAB or
 					 * SPACE means a continuation of the
 					 * previous entry.
ad642304
 					 *
 					 * Add all the arguments on the line
 					 */
e82a5185
 					if(isblank(lineGetData(t->t_next->t_line)[0]))
 						continue;
ad642304
 
 				quotes = 0;
cdfc0f05
 				for(qptr = fullline; *qptr; qptr++)
ad642304
 					if(*qptr == '\"')
 						quotes++;
 
9180b8bb
 				if(quotes & 1)
ad642304
 					continue;
 
273cd2bb
 				ptr = rfc822comments(fullline, NULL);
ad642304
 				if(ptr) {
 					free(fullline);
 					fullline = ptr;
 				}
37819555
 
82933497
 				if(parseEmailHeader(ret, fullline, rfc821) < 0)
 					continue;
b4cb4486
 
82933497
 				free(fullline);
 				fullline = NULL;
d32343c3
 			}
9180b8bb
 		} else
09ccd6e0
 			/*cli_dbgmsg("Add line to body '%s'\n", buffer);*/
de617e3e
 			if(messageAddLine(ret, t->t_line) < 0)
80a8c7d8
 				break;
ffd59a3e
 	}
68be129f
 
d768ac5a
 	if(fullline) {
ad642304
 		if(*fullline) switch(commandNumber) {
 			case CONTENT_TRANSFER_ENCODING:
 			case CONTENT_DISPOSITION:
 			case CONTENT_TYPE:
17bd4b65
 				cli_dbgmsg("parseEmailHeaders: Fullline unparsed '%s'\n", fullline);
ad642304
 		}
d768ac5a
 		free(fullline);
 	}
 
f2b068fb
 	if(!anyHeadersFound) {
 		/*
 		 * False positive in believing we have an e-mail when we don't
 		 */
 		messageDestroy(ret);
 		cli_dbgmsg("parseEmailHeaders: no headers found, assuming it isn't an email\n");
 		return NULL;
 	}
 
4465fb04
 	messageClean(ret);
 
09ccd6e0
 	cli_dbgmsg("parseEmailHeaders: return\n");
 
68be129f
 	return ret;
7fca6080
 }
 
 /*
8c0250d5
  * Handle a header line of an email message
  */
 static int
49674596
 parseEmailHeader(message *m, const char *line, const table_t *rfc821)
8c0250d5
 {
de509b8e
 	char *cmd;
8c0250d5
 	int ret = -1;
 #ifdef CL_THREAD_SAFE
 	char *strptr;
 #endif
31b05bcb
 	const char *separater;
0674e2af
 	char *copy, tokenseparater[2];
8c0250d5
 
0704dad8
 	cli_dbgmsg("parseEmailHeader '%s'\n", line);
 
31b05bcb
 	/*
 	 * In RFC822 the separater between the key a value is a colon,
 	 * e.g.	Content-Transfer-Encoding: base64
 	 * However some MUA's are lapse about this and virus writers exploit
 	 * this hole, so we need to check all known possiblities
 	 */
 	for(separater = ":= "; *separater; separater++)
 		if(strchr(line, *separater) != NULL)
 			break;
 
 	if(*separater == '\0')
74b5c349
 		return -1;
 
0674e2af
 	copy = rfc2047(line);
 	if(copy == NULL)
bd6146af
 		/* an RFC checker would return -1 here */
 		copy = strdup(line);
d1382234
 
31b05bcb
 	tokenseparater[0] = *separater;
 	tokenseparater[1] = '\0';
 
897fd9c7
 #ifdef	CL_THREAD_SAFE
31b05bcb
 	cmd = strtok_r(copy, tokenseparater, &strptr);
897fd9c7
 #else
 	cmd = strtok(copy, tokenseparater);
 #endif
8c0250d5
 
3a0ef2ee
 	if(cmd && (strstrip(cmd) > 0)) {
897fd9c7
 #ifdef	CL_THREAD_SAFE
8c0250d5
 		char *arg = strtok_r(NULL, "", &strptr);
897fd9c7
 #else
 		char *arg = strtok(NULL, "");
 #endif
8c0250d5
 
 		if(arg)
 			/*
 			 * Found a header such as
 			 * Content-Type: multipart/mixed;
 			 * set arg to be
 			 * "multipart/mixed" and cmd to
39ff42ee
 			 * be "Content-Type"
8c0250d5
 			 */
49674596
 			ret = parseMimeHeader(m, cmd, rfc821, arg);
8c0250d5
 	}
0674e2af
 	free(copy);
8c0250d5
 	return ret;
 }
 
 /*
b151ef55
  * This is a recursive routine.
9c8806fb
  * FIXME: We are not passed &mrec so we can't check against MAX_MAIL_RECURSION
b151ef55
  *
7fca6080
  * This function parses the body of mainMessage and saves its attachments in dir
  *
68be129f
  * mainMessage is the buffer to be parsed, it contains an e-mail's body, without
d32343c3
  * any headers. First time of calling it'll be
  * the whole message. Later it'll be parts of a multipart message
b151ef55
  * textIn is the plain text message being built up so far
  *
0bcad2b1
  * Returns:
b151ef55
  *	0 for fail
852e3ce4
  *	1 for success, attachments saved
  *	2 for success, attachments not saved
b151ef55
  */
 static int	/* success or fail */
565c449d
 parseEmailBody(message *messageIn, text *textIn, const char *dir, const table_t *rfc821Table, const table_t *subtypeTable, unsigned int options)
b151ef55
 {
6613d595
 	message **messages;	/* parts of a multipart message */
30fb8a0b
 	int inMimeHead, i, rc = 1, htmltextPart, multiparts = 0;
b151ef55
 	text *aText;
 	const char *cptr;
2250ea69
 	message *mainMessage;
565c449d
 	fileblob *fb;
b151ef55
 
565c449d
 	cli_dbgmsg("in parseEmailBody\n");
b151ef55
 
 	aText = textIn;
6613d595
 	messages = NULL;
2250ea69
 	mainMessage = messageIn;
b151ef55
 
 	/* Anything left to be parsed? */
0bcad2b1
 	if(mainMessage && (messageGetBody(mainMessage) != NULL)) {
b151ef55
 		mime_type mimeType;
30fb8a0b
 		int subtype, inhead;
b62a19da
 		const char *mimeSubtype, *boundary;
 		char *protocol;
b151ef55
 		const text *t_line;
f5e9abc8
 		/*bool isAlternative;*/
b151ef55
 		message *aMessage;
 
c6259ac5
 		cli_dbgmsg("Parsing mail file\n");
 
b151ef55
 		mimeType = messageGetMimeType(mainMessage);
 		mimeSubtype = messageGetMimeSubtype(mainMessage);
 
20917083
 		/* pre-process */
3497daca
 		subtype = tableFind(subtypeTable, mimeSubtype);
5eeffbb9
 		if((mimeType == TEXT) && (subtype == PLAIN)) {
b151ef55
 			/*
 			 * This is effectively no encoding, notice that we
 			 * don't check that charset is us-ascii
 			 */
 			cli_dbgmsg("assume no encoding\n");
 			mimeType = NOMIME;
b3a5cdd8
 			messageSetMimeSubtype(mainMessage, "");
20917083
 		} else if((mimeType == MESSAGE) &&
 			  (strcasecmp(mimeSubtype, "rfc822-headers") == 0)) {
 			/*
 			 * RFC1892/RFC3462: section 2 text/rfc822-headers
 			 * incorrectly sent as message/rfc822-headers
ef5eba29
 			 *
 			 * Parse as text/plain, i.e. no mime
20917083
 			 */
 			cli_dbgmsg("Changing message/rfc822-headers to text/rfc822-headers\n");
ef5eba29
 			mimeType = NOMIME;
b3a5cdd8
 			messageSetMimeSubtype(mainMessage, "");
b151ef55
 		}
 
c6259ac5
 		cli_dbgmsg("mimeType = %d\n", mimeType);
 
b151ef55
 		switch(mimeType) {
 		case NOMIME:
22f3b19b
 			cli_dbgmsg("Not a mime encoded message\n");
b151ef55
 			aText = textAddMessage(aText, mainMessage);
 			break;
 		case TEXT:
3497daca
 			/* text/plain has been preprocessed as no encoding */
 			if((options&CL_SCAN_MAILURL) && (subtype == HTML))
5eeffbb9
 				checkURLs(mainMessage, dir);
b151ef55
 			break;
 		case MULTIPART:
10c3ed55
 			cli_dbgmsg("Content-type 'multipart' handler\n");
b151ef55
 			boundary = messageFindArgument(mainMessage, "boundary");
 
 			if(boundary == NULL) {
 				cli_warnmsg("Multipart MIME message contains no boundaries\n");
2227f20e
 				/* Broken e-mail message */
 				mimeType = NOMIME;
 				/*
 				 * The break means that we will still
 				 * check if the file contains a uuencoded file
 				 */
 				break;
b151ef55
 			}
 
cbc2eaa9
 			/* Perhaps it should assume mixed? */
93002b48
 			if(mimeSubtype[0] == '\0') {
 				cli_warnmsg("Multipart has no subtype assuming alternative\n");
 				mimeSubtype = "alternative";
 				messageSetMimeSubtype(mainMessage, "alternative");
 			}
 
b151ef55
 			/*
 			 * Get to the start of the first message
 			 */
0704dad8
 			t_line = messageGetBody(mainMessage);
 
 			if(t_line == NULL) {
 				cli_warnmsg("Multipart MIME message has no body\n");
 				free((char *)boundary);
 				mimeType = NOMIME;
 				break;
 			}
 
 			do
74ca33e9
 				if(t_line->t_line) {
 					if(boundaryStart(lineGetData(t_line->t_line), boundary))
 						break;
 					/*
e82a5185
 					 * Found a binhex file before
4b54f2e0
 					 *	the first multipart
0856891e
 					 * TODO: check yEnc
74ca33e9
 					 */
e82a5185
 					if(binhexBegin(mainMessage) == t_line) {
0856891e
 						if(messageGetEncoding(mainMessage) == NOENCODING) {
 							messageSetEncoding(mainMessage, "x-binhex");
 							fb = messageToFileblob(mainMessage, dir);
 
 							if(fb)
 								fileblobDestroy(fb);
 						}
9f43cc75
 					} else if(encodingLine(mainMessage) == t_line->t_next) {
 						/*
 						 * We look for the next line
 						 * since later on we'll skip
 						 * over the important line when
 						 * we think it's a blank line
 						 * at the top of the message -
 						 * which it would have been in
 						 * an RFC compliant world
 						 */
 						cli_dbgmsg("Found MIME attachment before the first MIME section\n");
 						if(messageGetEncoding(mainMessage) == NOENCODING)
 							break;
0856891e
 					}
74ca33e9
 				}
0704dad8
 			while((t_line = t_line->t_next) != NULL);
b151ef55
 
 			if(t_line == NULL) {
b4cb4486
 				cli_dbgmsg("Multipart MIME message contains no boundary lines\n");
bf8ea488
 				/*
 				 * Free added by Thomas Lamy
 				 * <Thomas.Lamy@in-online.net>
 				 */
 				free((char *)boundary);
2227f20e
 				mimeType = NOMIME;
 				/*
 				 * The break means that we will still
e82a5185
 				 * check if the file contains a yEnc/binhex file
2227f20e
 				 */
 				break;
b151ef55
 			}
 			/*
 			 * Build up a table of all of the parts of this
 			 * multipart message. Remember, each part may itself
 			 * be a multipart message.
 			 */
 			inhead = 1;
 			inMimeHead = 0;
 
68be129f
 			/*
b62a19da
 			 * Parse the mainMessage object and create an array
 			 * of objects called messages, one for each of the
 			 * multiparts that mainMessage contains
 			 *
68be129f
 			 * This looks like parseEmailHeaders() - maybe there's
 			 * some duplication of code to be cleaned up
 			 */
6613d595
 			for(multiparts = 0; t_line; multiparts++) {
26564cf5
 				int lines = 0;
79e432d2
 				message **m;
26564cf5
 
79e432d2
 				m = cli_realloc(messages, ((multiparts + 1) * sizeof(message *)));
d32343c3
 				if(m == NULL)
79e432d2
 					break;
 				messages = m;
6613d595
 
b151ef55
 				aMessage = messages[multiparts] = messageCreate();
89e9a596
 				if(aMessage == NULL) {
 					multiparts--;
 					continue;
 				}
b151ef55
 
 				cli_dbgmsg("Now read in part %d\n", multiparts);
 
0bf1353d
 				/*
 				 * Ignore blank lines. There shouldn't be ANY
 				 * but some viruses insert them
 				 */
98685ac1
 				while((t_line = t_line->t_next) != NULL)
de617e3e
 					if(t_line->t_line &&
 					   /*(cli_chomp(t_line->t_text) > 0))*/
 					   (strlen(lineGetData(t_line->t_line)) > 0))
784e2335
 						break;
0bf1353d
 
 				if(t_line == NULL) {
 					cli_dbgmsg("Empty part\n");
61db35a1
 					/*
 					 * Remove this part unless there's
e82a5185
 					 * a binhex portion somewhere in
61db35a1
 					 * the complete message that we may
 					 * throw away by mistake if the MIME
 					 * encoding information is incorrect
 					 */
e82a5185
 					if(binhexBegin(mainMessage) == NULL) {
61db35a1
 						messageDestroy(aMessage);
 						--multiparts;
 					}
0bf1353d
 					continue;
 				}
 
 				do {
de617e3e
 					const char *line = lineGetData(t_line->t_line);
b151ef55
 
e82a5185
 					/*cli_dbgmsg("multipart %d: inMimeHead %d inhead %d boundary '%s' line '%s' next '%s'\n",
 						multiparts, inMimeHead, inhead, boundary, line,
30fb8a0b
 						t_line->t_next && t_line->t_next->t_line ? lineGetData(t_line->t_next->t_line) : "(null)");*/
b151ef55
 
e0377124
 					if(inMimeHead) {	/* continuation line */
98685ac1
 						if(line == NULL) {
699fafc3
 							/*inhead =*/ inMimeHead = 0;
98685ac1
 							continue;
 						}
7baeb4a6
 						/*
 						 * Handle continuation lines
 						 * because the previous line
21cd233d
 						 * ended with a ; or this line
 						 * starts with a white space
7baeb4a6
 						 */
21cd233d
 						cli_dbgmsg("Multipart %d: About to add mime Argument '%s'\n",
 							multiparts, line);
7baeb4a6
 						/*
 						 * Handle the case when it
 						 * isn't really a continuation
 						 * line:
 						 * Content-Type: application/octet-stream;
 						 * Content-Transfer-Encoding: base64
 						 */
 						parseEmailHeader(aMessage, line, rfc821Table);
 
b151ef55
 						while(isspace((int)*line))
 							line++;
 
 						if(*line == '\0') {
 							inhead = inMimeHead = 0;
 							continue;
 						}
 						/*
 						 * This may cause a trailing ';'
 						 * to be added if this test
 						 * fails - TODO: verify this
 						 */
 						inMimeHead = continuationMarker(line);
 						messageAddArgument(aMessage, line);
e0377124
 					} else if(inhead) {	/* handling normal headers */
10c3ed55
 						int quotes;
 						char *fullline, *ptr;
 						const char *qptr;
 						const text *next;
ad642304
 
98685ac1
 						if(line == NULL) {
9cb47b80
 							/*
 							 * empty line, should the end of the headers,
 							 * but some base64 decoders, e.g. uudeview, are broken
 							 * and will handle this type of entry, decoding the
 							 * base64 content...
 							 * Content-Type: application/octet-stream; name=text.zip
 							 * Content-Transfer-Encoding: base64
 							 * Content-Disposition: attachment; filename="text.zip"
64ff0d49
 							 *
9cb47b80
 							 * Content-Disposition: attachment;
 							 *	filename=text.zip
 							 * Content-Type: application/octet-stream;
 							 *	name=text.zip
 							 * Content-Transfer-Encoding: base64
64ff0d49
 							 *
9cb47b80
 							 * UEsDBAoAAAAAAACgPjJ2RHw676gAAO+oAABEAAAAbWFpbF90ZXh0LWluZm8udHh0ICAgICAgICAg
 							 */
 							next = t_line->t_next;
 							if(next && next->t_line) {
 								const char *data = lineGetData(next->t_line);
6c4485f9
 
 								if((messageGetEncoding(aMessage) == NOENCODING) &&
 								   (messageGetMimeType(aMessage) == APPLICATION))
273cd2bb
 									/*
 									 * Handle this nightmare (note the blank
 									 * line in the header and the incorrect
 									 * content-transfer-encoding header)
 									 *
 									 * Content-Type: application/octet-stream; name="zipped_files.EXEX-Spanska: Yes
 									 *
 									 * r-Encoding: base64
 									 * Content-Disposition: attachment; filename="zipped_files.EXE"
 									 */
 									if(strstr(data, "base64")) {
6c4485f9
 										messageSetEncoding(aMessage, "base64");
 										cli_dbgmsg("Ignoring fake end of headers\n");
 										continue;
 									}
11466e82
 								if((strncmp(data, "Content", 7) == 0) ||
 								   (strncmp(data, "filename=", 9) == 0)) {
9cb47b80
 									cli_dbgmsg("Ignoring fake end of headers\n");
 									continue;
 								}
 							}
699fafc3
 							cli_dbgmsg("Multipart %d: End of header information\n",
 								multiparts);
b151ef55
 							inhead = 0;
 							continue;
 						}
a64bf87e
 						if(isspace((int)*line)) {
 							/*
 							 * The first line is
 							 * continuation line.
 							 * This is tricky
 							 * to handle, but
 							 * all we can do is our
 							 * best
 							 */
 							cli_dbgmsg("Part %d starts with a continuation line\n",
 								multiparts);
 							messageAddArgument(aMessage, line);
 							/*
 							 * Give it a default
 							 * MIME type since
 							 * that may be the
 							 * missing line
 							 *
 							 * Choose application to
 							 * force a save
 							 */
 							if(messageGetMimeType(aMessage) == NOMIME)
 								messageSetMimeType(aMessage, "application");
 							continue;
 						}
 
10c3ed55
 						inMimeHead = FALSE;
de617e3e
 
e82a5185
 						assert(strlen(line) <= RFC2821LENGTH);
e0377124
 
273cd2bb
 						fullline = rfc822comments(line, NULL);
10c3ed55
 						if(fullline == NULL)
 							fullline = strdup(line);
ad642304
 
10c3ed55
 						quotes = 0;
 						for(qptr = fullline; *qptr; qptr++)
 							if(*qptr == '\"')
 								quotes++;
e0377124
 
10c3ed55
 						/*
 						 * Fold next lines to the end of this
 						 * if they start with a white space
 						 * or if this line has an odd number of quotes:
 						 * Content-Type: application/octet-stream; name="foo
 						 * "
 						 */
 						next = t_line->t_next;
 						while(next && next->t_line) {
 							const char *data = lineGetData(next->t_line);
21cd233d
 
60aec445
 							/*if((!isspace(data[0])) &&
10c3ed55
 							   ((quotes & 1) == 0))
60aec445
 								break;*/
 							if(!isspace(data[0]))
10c3ed55
 								break;
e0377124
 
d99b1840
 							if(data[1] == '\0') {
 								/*
 								 * Broken message: the
 								 * blank line at the end
 								 * of the headers isn't blank -
 								 * it contains a space
 								 */
 								cli_dbgmsg("Multipart %d: headers not terminated by blank line\n",
 									multiparts);
 								inhead = FALSE;
 								break;
 							}
 
10c3ed55
 							ptr = cli_realloc(fullline,
 								strlen(fullline) + strlen(data) + 1);
68badbc1
 
10c3ed55
 							if(ptr == NULL)
 								break;
21cd233d
 
10c3ed55
 							fullline = ptr;
 							strcat(fullline, data);
ad642304
 
60aec445
 							/*for(qptr = data; *qptr; qptr++)
10c3ed55
 								if(*qptr == '\"')
60aec445
 									quotes++;*/
ad642304
 
10c3ed55
 							t_line = next;
 							next = next->t_next;
21cd233d
 						}
10c3ed55
 						cli_dbgmsg("Multipart %d: About to parse folded header '%s'\n",
 							multiparts, fullline);
 
 						parseEmailHeader(aMessage, fullline, rfc821Table);
 						free(fullline);
b151ef55
 					} else if(endOfMessage(line, boundary)) {
 						/*
 						 * Some viruses put information
 						 * *after* the end of message,
 						 * which presumably some broken
 						 * mail clients find, so we
 						 * can't assume that this
 						 * is the end of the message
 						 */
 						/* t_line = NULL;*/
 						break;
30fb8a0b
 					} else if(boundaryStart(line, boundary)) {
 						inhead = 1;
 						break;
26564cf5
 					} else {
de617e3e
 						if(messageAddLine(aMessage, t_line->t_line) < 0)
79e432d2
 							break;
26564cf5
 						lines++;
 					}
0bf1353d
 				} while((t_line = t_line->t_next) != NULL);
 
b151ef55
 				messageClean(aMessage);
26564cf5
 
 				cli_dbgmsg("Part %d has %d lines\n",
 					multiparts, lines);
b151ef55
 			}
 
 			free((char *)boundary);
 
6638be41
 			/*
cbc2eaa9
 			 * Preprocess. Anything special to be done before
 			 * we handle the multiparts?
b62a19da
 			 */
cbc2eaa9
 			switch(tableFind(subtypeTable, mimeSubtype)) {
 				case KNOWBOT:
 					/* TODO */
 					cli_dbgmsg("multipart/knowbot parsed as multipart/mixed for now\n");
 					mimeSubtype = "mixed";
 					break;
e279f3ea
 				case -1:
 					/*
 					 * According to section 7.2.6 of
 					 * RFC1521, unrecognised multiparts
 					 * should be treated as multipart/mixed.
 					 */
0cb54827
 					cli_dbgmsg("Unsupported multipart format `%s', parsed as mixed\n", mimeSubtype);
e279f3ea
 					mimeSubtype = "mixed";
 					break;
cbc2eaa9
 			}
b62a19da
 
 			/*
6638be41
 			 * We've finished message we're parsing
 			 */
 			if(mainMessage && (mainMessage != messageIn)) {
 				messageDestroy(mainMessage);
 				mainMessage = NULL;
2250ea69
 			}
b151ef55
 
6613d595
 			if(multiparts == 0) {
 				if(messages)
 					free(messages);
6638be41
 				return 2;	/* Nothing to do */
6613d595
 			}
6638be41
 
b151ef55
 			cli_dbgmsg("The message has %d parts\n", multiparts);
49674596
 			cli_dbgmsg("Find out the multipart type (%s)\n", mimeSubtype);
b151ef55
 
b62a19da
 			/*
 			 * We now have all the parts of the multipart message
 			 * in the messages array:
 			 *	message *messages[multiparts]
 			 * Let's decide what to do with them all
 			 */
b151ef55
 			switch(tableFind(subtypeTable, mimeSubtype)) {
 			case RELATED:
68be129f
 				cli_dbgmsg("Multipart related handler\n");
b151ef55
 				/*
295e425f
 				 * Have a look to see if there's HTML code
 				 * which will need scanning
b151ef55
 				 */
 				aMessage = NULL;
 				assert(multiparts > 0);
 
0bcad2b1
 				htmltextPart = getTextPart(messages, multiparts);
b151ef55
 
0bcad2b1
 				if(htmltextPart >= 0)
 					aText = textAddMessage(aText, messages[htmltextPart]);
b151ef55
 				else
 					/*
295e425f
 					 * There isn't an HTML bit. If there's a
 					 * multipart bit, it'll may be in there
 					 * somewhere
b151ef55
 					 */
 					for(i = 0; i < multiparts; i++)
 						if(messageGetMimeType(messages[i]) == MULTIPART) {
 							aMessage = messages[i];
0bcad2b1
 							htmltextPart = i;
b151ef55
 							break;
 						}
 
74c6f514
 				if(htmltextPart == -1)
295e425f
 					cli_dbgmsg("No HTML code found to be scanned");
74c6f514
 				else {
565c449d
 					rc = parseEmailBody(aMessage, aText, dir, rfc821Table, subtypeTable, options);
74c6f514
 					if(rc == 1) {
 						assert(aMessage == messages[htmltextPart]);
 						messageDestroy(aMessage);
 						messages[htmltextPart] = NULL;
 					}
 				}
b151ef55
 
 				/*
 				 * Fixed based on an idea from Stephen White <stephen@earth.li>
 				 * The message is confused about the difference
 				 * between alternative and related. Badtrans.B
 				 * suffers from this problem.
 				 *
 				 * Fall through in this case:
 				 * Content-Type: multipart/related;
 				 *	type="multipart/alternative"
 				 */
f5e9abc8
 				/*
 				 * Changed to always fall through based on
 				 * an idea from Michael Dankov <misha@btrc.ru>
 				 * that some viruses are completely confused
 				 * about the difference between related
 				 * and mixed
 				 */
 				/*cptr = messageFindArgument(mainMessage, "type");
b151ef55
 				if(cptr == NULL)
 					break;
 				isAlternative = (bool)(strcasecmp(cptr, "multipart/alternative") == 0);
 				free((char *)cptr);
 				if(!isAlternative)
f5e9abc8
 					break;*/
d28e1902
 			case DIGEST:
 				/*
 				 * According to section 5.1.5 RFC2046, the
 				 * default mime type of multipart/digest parts
 				 * is message/rfc822
 				 *
 				 * We consider them as alternative, wrong in
 				 * the strictest sense since they aren't
 				 * alternatives - all parts a valid - but it's
 				 * OK for our needs since it means each part
 				 * will be scanned
 				 */
b151ef55
 			case ALTERNATIVE:
 				cli_dbgmsg("Multipart alternative handler\n");
 
 				/*
 				 * Fall through - some clients are broken and
 				 * say alternative instead of mixed. The Klez
e279f3ea
 				 * virus is broken that way, and anyway we
 				 * wish to scan all of the alternatives
b151ef55
 				 */
 			case REPORT:
 				/*
 				 * According to section 1 of RFC1892, the
 				 * syntax of multipart/report is the same
 				 * as multipart/mixed. There are some required
 				 * parameters, but there's no need for us to
 				 * verify that they exist
 				 */
 			case MIXED:
fdc8a467
 			case APPLEDOUBLE:	/* not really supported */
b151ef55
 				/*
 				 * Look for attachments
 				 *
 				 * Not all formats are supported. If an
 				 * unsupported format turns out to be
 				 * common enough to implement, it is a simple
 				 * matter to add it
 				 */
2250ea69
 				if(aText) {
 					if(mainMessage && (mainMessage != messageIn))
 						messageDestroy(mainMessage);
b151ef55
 					mainMessage = NULL;
2250ea69
 				}
b151ef55
 
 				cli_dbgmsg("Mixed message with %d parts\n", multiparts);
 				for(i = 0; i < multiparts; i++) {
 					bool addToText = FALSE;
 					const char *dtype;
565c449d
 #ifndef	SAVE_TO_DISC
f54a8635
 					message *body;
d32343c3
 #endif
b151ef55
 
 					aMessage = messages[i];
 
74c6f514
 					if(aMessage == NULL)
 						continue;
b151ef55
 
 					cli_dbgmsg("Mixed message part %d is of type %d\n",
 						i, messageGetMimeType(aMessage));
 
 					switch(messageGetMimeType(aMessage)) {
 					case APPLICATION:
9c8806fb
 					case AUDIO:
 					case IMAGE:
 					case VIDEO:
b151ef55
 						break;
 					case NOMIME:
c29ebe66
 						cli_dbgmsg("No mime headers found in multipart part %d\n", i);
7b8fb055
 						if(mainMessage) {
e82a5185
 							if(binhexBegin(aMessage)) {
 								cli_dbgmsg("Found binhex message in multipart/mixed mainMessage\n");
 								messageSetEncoding(mainMessage, "x-bunhex");
1e06e1ab
 								fb = messageToFileblob(mainMessage, dir);
7b8fb055
 
1e06e1ab
 								if(fb)
 									fileblobDestroy(fb);
7b8fb055
 							}
 							if(mainMessage != messageIn)
 								messageDestroy(mainMessage);
 							mainMessage = NULL;
30fb8a0b
 						} else if(aMessage) {
e82a5185
 							if(binhexBegin(aMessage)) {
39b5a552
 								cli_dbgmsg("Found binhex message in multipart/mixed non mime part\n");
 								messageSetEncoding(aMessage, "x-binhex");
 								fb = messageToFileblob(aMessage, dir);
 
 								if(fb)
 									fileblobDestroy(fb);
 								assert(aMessage == messages[i]);
 								messageReset(messages[i]);
30fb8a0b
 							}
7b8fb055
 						}
b151ef55
 						addToText = TRUE;
 						if(messageGetBody(aMessage) == NULL)
 							/*
 							 * No plain text version
 							 */
e82a5185
 							cli_dbgmsg("No plain text alternative");
b151ef55
 						break;
 					case TEXT:
ef3cf57d
 						dtype = messageGetDispositionType(aMessage);
852e3ce4
 						cli_dbgmsg("Mixed message text part disposition \"%s\"\n",
 							dtype);
b151ef55
 						if(strcasecmp(dtype, "attachment") == 0)
23e1c37c
 							break;
 						if((*dtype == '\0') || (strcasecmp(dtype, "inline") == 0)) {
2250ea69
 							if(mainMessage && (mainMessage != messageIn))
 								messageDestroy(mainMessage);
b151ef55
 							mainMessage = NULL;
ef3cf57d
 							cptr = messageGetMimeSubtype(aMessage);
e6b25cd3
 							cli_dbgmsg("Mime subtype \"%s\"\n", cptr);
e82a5185
 							if((tableFind(subtypeTable, cptr) == PLAIN) &&
10c3ed55
 								  (messageGetEncoding(aMessage) == NOENCODING)) {
7584963d
 								char *filename;
852e3ce4
 								/*
 								 * Strictly speaking
c9ae17be
 								 * a text/plain part is
852e3ce4
 								 * not an attachment. We
 								 * pretend it is so that
 								 * we can decode and
 								 * scan it
 								 */
7584963d
 								filename = (char *)messageFindArgument(aMessage, "filename");
 								if(filename == NULL)
 									filename = (char *)messageFindArgument(aMessage, "name");
 
 								if(filename == NULL) {
 									cli_dbgmsg("Adding part to main message\n");
 									addToText = TRUE;
 								} else {
 									cli_dbgmsg("Treating %s as attachment\n",
 										filename);
 									free(filename);
 								}
852e3ce4
 							} else {
06d4e856
 								if(options&CL_SCAN_MAILURL)
c9ae17be
 									if(tableFind(subtypeTable, cptr) == HTML)
 										checkURLs(aMessage, dir);
f52d7358
 								messageAddArgument(aMessage, "filename=mixedtextportion");
b151ef55
 							}
 						} else {
b4cb4486
 							cli_dbgmsg("Text type %s is not supported\n", dtype);
b151ef55
 							continue;
 						}
 						break;
 					case MESSAGE:
2e0f78a6
 						/* Content-Type: message/rfc822 */
27116a59
 						cli_dbgmsg("Found message inside multipart (encoding type %d)\n",
 							messageGetEncoding(aMessage));
388072d8
 						switch(messageGetEncoding(aMessage)) {
 							case NOENCODING:
 							case EIGHTBIT:
 							case BINARY:
 								if(encodingLine(aMessage) == NULL) {
 									/*
 									 * This means that the message has no attachments
 									 * The test for messageGetEncoding is needed since
 									 * encodingLine won't have been set if the message
 									 * itself has been encoded
 									 */
 									cli_dbgmsg("No encoding line found in the multipart/message\n");
 									assert(aMessage == messages[i]);
 									messageDestroy(messages[i]);
 									messages[i] = NULL;
 									continue;
 								}
ffd59a3e
 						}
388072d8
 #if	0
de617e3e
 						messageAddStrAtTop(aMessage,
0856891e
 							"Received: by clamd (message/rfc822)");
27116a59
 #endif
4465fb04
 #ifdef	SAVE_TO_DISC
 						/*
 						 * Save this embedded message
 						 * to a temporary file
 						 */
 						saveTextPart(aMessage, dir);
 						assert(aMessage == messages[i]);
 						messageDestroy(messages[i]);
 						messages[i] = NULL;
 #else
 						/*
 						 * Scan in memory, faster but
 						 * is open to DoS attacks when
 						 * many nested levels are
 						 * involved.
 						 */
735377bc
 						body = parseEmailHeaders(aMessage, rfc821Table, TRUE);
bad123c6
 						/*
 						 * We've fininished with the
 						 * original copy of the message,
 						 * so throw that away and
 						 * deal with the encapsulated
 						 * message as a message.
 						 * This can save a lot of memory
 						 */
 						assert(aMessage == messages[i]);
 						messageDestroy(messages[i]);
 						messages[i] = NULL;
f54a8635
 						if(body) {
565c449d
 							rc = parseEmailBody(body, NULL, dir, rfc821Table, subtypeTable, options);
f54a8635
 							messageDestroy(body);
 						}
4465fb04
 #endif
b151ef55
 						continue;
 					case MULTIPART:
 						/*
 						 * It's a multi part within a multi part
 						 * Run the message parser on this bit, it won't
 						 * be an attachment
 						 */
 						cli_dbgmsg("Found multipart inside multipart\n");
d32343c3
 						if(aMessage) {
 							/*
 							 * The headers were parsed when reading in the
 							 * whole multipart section
 							 */
565c449d
 							rc = parseEmailBody(aMessage, aText, dir, rfc821Table, subtypeTable, options);
d32343c3
 							cli_dbgmsg("Finished recursion\n");
 							assert(aMessage == messages[i]);
 							messageDestroy(messages[i]);
 							messages[i] = NULL;
f54a8635
 						} else {
565c449d
 							rc = parseEmailBody(NULL, NULL, dir, rfc821Table, subtypeTable, options);
2250ea69
 							if(mainMessage && (mainMessage != messageIn))
 								messageDestroy(mainMessage);
f54a8635
 							mainMessage = NULL;
 						}
b151ef55
 						continue;
 					default:
c7256385
 						cli_warnmsg("Only text and application attachments are supported, type = %d\n",
b151ef55
 							messageGetMimeType(aMessage));
 						continue;
 					}
 
c29ebe66
 					if(addToText) {
 						cli_dbgmsg("Adding to non mime-part\n");
b151ef55
 						aText = textAdd(aText, messageGetBody(aMessage));
c29ebe66
 					} else {
565c449d
 						fb = messageToFileblob(aMessage, dir);
b151ef55
 
1e06e1ab
 						if(fb)
 							fileblobDestroy(fb);
b151ef55
 					}
6638be41
 					assert(aMessage == messages[i]);
 					messageDestroy(messages[i]);
 					messages[i] = NULL;
b151ef55
 				}
 
565c449d
 				/* rc = parseEmailBody(NULL, NULL, dir, rfc821Table, subtypeTable, options); */
b151ef55
 				break;
 			case SIGNED:
 			case PARALLEL:
 				/*
 				 * If we're here it could be because we have a
 				 * multipart/mixed message, consisting of a
 				 * message followed by an attachment. That
 				 * message itself is a multipart/alternative
 				 * message and we need to dig out the plain
 				 * text part of that alternative
 				 */
0bcad2b1
 				htmltextPart = getTextPart(messages, multiparts);
 				if(htmltextPart == -1)
 					htmltextPart = 0;
b151ef55
 
565c449d
 				rc = parseEmailBody(messages[htmltextPart], aText, dir, rfc821Table, subtypeTable, options);
b151ef55
 				break;
b62a19da
 			case ENCRYPTED:
 				rc = 0;
cbc2eaa9
 				protocol = (char *)messageFindArgument(mainMessage, "protocol");
b62a19da
 				if(protocol) {
 					if(strcasecmp(protocol, "application/pgp-encrypted") == 0) {
 						/* RFC2015 */
 						cli_warnmsg("PGP encoded attachment not scanned\n");
 						rc = 2;
 					} else
e82a5185
 						cli_warnmsg("Unknown encryption protocol '%s' - if you believe this file contains a virus, submit it to www.clamav.net\n", protocol);
b62a19da
 					free(protocol);
 				} else
23e1c37c
 					cli_dbgmsg("Encryption method missing protocol name\n");
b62a19da
 
 				break;
b151ef55
 			default:
e279f3ea
 				assert(0);
b151ef55
 			}
 
2250ea69
 			if(mainMessage && (mainMessage != messageIn))
 				messageDestroy(mainMessage);
 
c29ebe66
 			if(aText && (textIn == NULL)) {
 				if((fb = fileblobCreate()) != NULL) {
10c3ed55
 					cli_dbgmsg("Save non mime and/or text/plain part\n");
c29ebe66
 					fileblobSetFilename(fb, dir, "textpart");
b5053e3f
 					/*fileblobAddData(fb, "Received: by clamd (textpart)\n", 30);*/
60aec445
 					(void)textToFileblob(aText, fb);
c29ebe66
 
 					fileblobDestroy(fb);
 				}
c6259ac5
 				textDestroy(aText);
c29ebe66
 			}
c6259ac5
 
0856891e
 			for(i = 0; i < multiparts; i++)
 				if(messages[i])
 					messageDestroy(messages[i]);
 
6613d595
 			if(messages)
 				free(messages);
 
b151ef55
 			return rc;
 
 		case MESSAGE:
 			/*
 			 * Check for forbidden encodings
 			 */
 			switch(messageGetEncoding(mainMessage)) {
 				case NOENCODING:
 				case EIGHTBIT:
 				case BINARY:
 					break;
 				default:
c6259ac5
 					cli_warnmsg("MIME type 'message' cannot be decoded\n");
b151ef55
 					break;
 			}
9a7398ee
 			rc = 0;
c6259ac5
 			if((strcasecmp(mimeSubtype, "rfc822") == 0) ||
 			   (strcasecmp(mimeSubtype, "delivery-status") == 0)) {
de617e3e
 				message *m = parseEmailHeaders(mainMessage, rfc821Table);
bad123c6
 				if(m) {
 					cli_dbgmsg("Decode rfc822");
 
4465fb04
 					if(mainMessage && (mainMessage != messageIn)) {
 						messageDestroy(mainMessage);
 						mainMessage = NULL;
74c6f514
 					} else
 						messageReset(mainMessage);
bad123c6
 					if(messageGetBody(m))
565c449d
 						rc = parseEmailBody(m, NULL, dir, rfc821Table, subtypeTable, options);
bad123c6
 
 					messageDestroy(m);
 				}
b151ef55
 				break;
f8c25c7a
 			} else if(strcasecmp(mimeSubtype, "disposition-notification") == 0) {
d6e30cce
 				/* RFC 2298 - handle like a normal email */
f8c25c7a
 				rc = 1;
d6e30cce
 				break;
f8c25c7a
 			} else if(strcasecmp(mimeSubtype, "partial") == 0) {
9a7398ee
 #ifdef	PARTIAL_DIR
 				/* RFC1341 message split over many emails */
 				if(rfc1341(mainMessage, dir) >= 0)
 					rc = 1;
 #else
e94471f4
 				cli_warnmsg("Partial message received from MUA/MTA - message cannot be scanned\n");
9a7398ee
 				rc = 0;
 #endif
 			} else if(strcasecmp(mimeSubtype, "external-body") == 0)
e94471f4
 				/* TODO */
b151ef55
 				cli_warnmsg("Attempt to send Content-type message/external-body trapped");
bf8ea488
 			else
e82a5185
 				cli_warnmsg("Unsupported message format `%s' - if you believe this file contains a virus, submit it to www.clamav.net\n", mimeSubtype);
b151ef55
 
9a7398ee
 
2250ea69
 			if(mainMessage && (mainMessage != messageIn))
 				messageDestroy(mainMessage);
6613d595
 			if(messages)
 				free(messages);
9a7398ee
 			return rc;
b151ef55
 
 		case APPLICATION:
23e1c37c
 			/*cptr = messageGetMimeSubtype(mainMessage);
0bcad2b1
 
23e1c37c
 			if((strcasecmp(cptr, "octet-stream") == 0) ||
04421a14
 			   (strcasecmp(cptr, "x-msdownload") == 0)) {*/
 			{
565c449d
 				fb = messageToFileblob(mainMessage, dir);
b151ef55
 
1e06e1ab
 				if(fb) {
 					cli_dbgmsg("Saving main message as attachment\n");
 					fileblobDestroy(fb);
43756987
 					if(mainMessage != messageIn) {
 						messageDestroy(mainMessage);
 						mainMessage = NULL;
 					} else
 						messageReset(mainMessage);
b151ef55
 				}
04421a14
 			} /*else
 				cli_warnmsg("Discarded application not sent as attachment\n");*/
b151ef55
 			break;
 
 		case AUDIO:
 		case VIDEO:
 		case IMAGE:
 			break;
 
 		default:
 			cli_warnmsg("Message received with unknown mime encoding");
 			break;
 		}
 	}
 
d32343c3
 	if(aText && (textIn == NULL)) {
22f3b19b
 		/* Look for a bounce in the text (non mime encoded) portion */
 		const text *t;
0856891e
 
22f3b19b
 		for(t = aText; t; t = t->t_next) {
 			const line_t *l = t->t_line;
4b54f2e0
 			const text *lookahead, *topofbounce;
22f3b19b
 			const char *s;
4b54f2e0
 			bool inheader;
0856891e
 
22f3b19b
 			if(l == NULL)
 				continue;
 
 			s = lineGetData(l);
 
 			if(cli_filetype(s, strlen(s)) != CL_TYPE_MAIL)
 				continue;
 
 			/*
 			 * We've found what looks like the start of a bounce
 			 * message. Only bother saving if it really is a bounce
 			 * message, this helps to speed up scanning of ping-pong
 			 * messages that have lots of bounces within bounces in
 			 * them
 			 */
 			for(lookahead = t->t_next; lookahead; lookahead = lookahead->t_next) {
 				l = lookahead->t_line;
 
 				if(l == NULL)
 					break;
 				s = lineGetData(l);
 				if(strncasecmp(s, "Content-Type:", 13) == 0)
 					/*
 					 * Don't bother with plain/text or
 					 * plain/html
 					 */
 					if(strstr(s, "text/") == NULL)
0d9e07a9
 						/*
 						 * Don't bother to save the unuseful
 						 * part
 						 */
22f3b19b
 						break;
 			}
 
 			if(lookahead && (lookahead->t_line == NULL)) {
 				cli_dbgmsg("Non mime part bounce message is not mime encoded, so it will not be scanned\n");
 				t = lookahead;
 				/* look for next bounce message */
 				continue;
 			}
 
0d9e07a9
 			/*
 			 * Prescan the bounce message to see if there's likely
 			 * to be anything nasty.
 			 * This algorithm is hand crafted and may be breakable
 			 * so all submissions are welcome. It's best NOT to
 			 * remove this however you may be tempted, because it
 			 * significantly speeds up the scanning of multiple
 			 * bounces (i.e. bounces within many bounces)
 			 */
 			for(; lookahead; lookahead = lookahead->t_next) {
 				l = lookahead->t_line;
 
 				if(l) {
 					s = lineGetData(l);
 					if((strncasecmp(s, "Content-Type:", 13) == 0) &&
 					   (strstr(s, "multipart/") == NULL) &&
 					   (strstr(s, "message/rfc822") == NULL) &&
 					   (strstr(s, "text/plain") == NULL))
 						break;
 				}
 			}
 			if(lookahead == NULL) {
f60a8d41
 				cli_dbgmsg("cli_mbox: I believe it's plain text which must be clean\n");
0d9e07a9
 				/* nothing here, move along please */
 				break;
 			}
4b54f2e0
 			if((fb = fileblobCreate()) == NULL)
 				break;
 			cli_dbgmsg("Save non mime part bounce message\n");
 			fileblobSetFilename(fb, dir, "bounce");
 			fileblobAddData(fb, (unsigned char *)"Received: by clamd (bounce)\n", 28);
 
 			inheader = TRUE;
 			topofbounce = NULL;
 			for(;;) {
 				l = t->t_line;
 
 				if(l == NULL) {
 					if(inheader) {
 						inheader = FALSE;
 						topofbounce = t;
 					}
 				} else {
 					s = lineGetData(l);
 					fileblobAddData(fb, (unsigned char *)s, strlen(s));
 				}
 				fileblobAddData(fb, (unsigned char *)"\n", 1);
 				lookahead = t->t_next;
 				if(lookahead == NULL)
 					break;
 				t = lookahead;
 				l = t->t_line;
 				if((!inheader) && l) {
 					s = lineGetData(l);
 					if(cli_filetype(s, strlen(s)) == CL_TYPE_MAIL) {
 						cli_dbgmsg("Found the start of another bounce candidate\n");
 						break;
 					}
 				}
22f3b19b
 			}
4b54f2e0
 
 			fileblobDestroy(fb);
 			if(topofbounce)
 				t = topofbounce;
 			/*
 			 * Don't do this - it slows bugs.txt
 			 */
 			/*if(mainMessage)
 				mainMessage->bounce = NULL;*/
22f3b19b
 		}
d32343c3
 		textDestroy(aText);
 		aText = NULL;
 	}
 
565c449d
 	/*
 	 * No attachments - scan the text portions, often files
 	 * are hidden in HTML code
 	 */
 	cli_dbgmsg("%d multiparts found\n", multiparts);
 	for(i = 0; i < multiparts; i++) {
 		fb = messageToFileblob(messages[i], dir);
b151ef55
 
565c449d
 		if(fb) {
0e3b08fc
 			cli_dbgmsg("Saving multipart %d\n", i);
0bcad2b1
 
565c449d
 			fileblobDestroy(fb);
 		}
 	}
 
 	if(mainMessage) {
b151ef55
 		/*
565c449d
 		 * Look for uu-encoded main file
b151ef55
 		 */
565c449d
 		const text *t_line;
 
e82a5185
 		if((encodingLine(mainMessage) != NULL) &&
f1c33aa0
 			  ((t_line = bounceBegin(mainMessage)) != NULL)) {
0856891e
 			const text *t, *start;
0bcad2b1
 			/*
565c449d
 			 * Attempt to save the original (unbounced)
 			 * message - clamscan will find that in the
 			 * directory and call us again (with any luck)
8386482b
 			 * having found an e-mail message to handle.
565c449d
 			 *
 			 * This finds a lot of false positives, the
8386482b
 			 * search that a content type is in the
565c449d
 			 * bounce (i.e. it's after the bounce header)
8386482b
 			 * helps a bit.
 			 *
 			 * messageAddLine
565c449d
 			 * optimisation could help here, but needs
 			 * careful thought, do it with line numbers
 			 * would be best, since the current method in
 			 * messageAddLine of checking encoding first
 			 * must remain otherwise non bounce messages
 			 * won't be scanned
0bcad2b1
 			 */
0856891e
 			for(t = start = t_line; t; t = t->t_next) {
e82a5185
 				char cmd[RFC2821LENGTH + 1];
565c449d
 				const char *txt = lineGetData(t->t_line);
 
0856891e
 				if(txt == NULL)
 					continue;
 				if(cli_strtokbuf(txt, 0, ":", cmd) == NULL)
 					continue;
 
 				switch(tableFind(rfc821Table, cmd)) {
 					case CONTENT_TRANSFER_ENCODING:
 						if((strstr(txt, "7bit") == NULL) &&
 						   (strstr(txt, "8bit") == NULL))
 							break;
 						continue;
 					case CONTENT_DISPOSITION:
 						break;
 					case CONTENT_TYPE:
 						if(strstr(txt, "text/plain") != NULL)
 							t = NULL;
 						break;
 					default:
 						if(strcasecmp(cmd, "From") == 0)
 							start = t_line;
 						else if(strcasecmp(cmd, "Received") == 0)
 							start = t_line;
 						continue;
8386482b
 				}
0856891e
 				break;
565c449d
 			}
 			if(t && ((fb = fileblobCreate()) != NULL)) {
 				cli_dbgmsg("Found a bounce message\n");
 				fileblobSetFilename(fb, dir, "bounce");
cbe29191
 				if(textToFileblob(start, fb) == NULL)
 					cli_dbgmsg("Nothing new to save in the bounce message");
 				else
 					rc = 1;
565c449d
 				fileblobDestroy(fb);
e745ac7e
 			} else
 				cli_dbgmsg("Not found a bounce message\n");
565c449d
 		} else {
 			bool saveIt;
0bcad2b1
 
565c449d
 			if(messageGetMimeType(mainMessage) == MESSAGE)
15c8cace
 				/*
565c449d
 				 * Quick peek, if the encapsulated
 				 * message has no
 				 * content encoding statement don't
 				 * bother saving to scan, it's safe
15c8cace
 				 */
565c449d
 				saveIt = (encodingLine(mainMessage) != NULL);
 			else if((t_line = encodingLine(mainMessage)) != NULL) {
92915cee
 				/*
565c449d
 				 * Some bounces include the message
 				 * body without the headers.
4b54f2e0
 				 * FIXME: Unfortunately this generates a
565c449d
 				 * lot of false positives that a bounce
 				 * has been found when it hasn't.
92915cee
 				 */
565c449d
 				if((fb = fileblobCreate()) != NULL) {
b3a5cdd8
 					cli_dbgmsg("Found a bounce message with no header at '%s'\n",
 						lineGetData(t_line->t_line));
1e06e1ab
 					fileblobSetFilename(fb, dir, "bounce");
00615ec9
 					fileblobAddData(fb,
 						(const unsigned char *)"Received: by clamd (bounce)\n",
 						28);
5a01973c
 
565c449d
 					fb = textToFileblob(t_line, fb);
b759d5eb
 
565c449d
 					fileblobDestroy(fb);
b759d5eb
 				}
565c449d
 				saveIt = FALSE;
74c6f514
 			} else if(multiparts == 0)
565c449d
 				/*
 				 * Save the entire text portion,
 				 * since it it may be an HTML file with
 				 * a JavaScript virus
 				 */
 				saveIt = TRUE;
74c6f514
 			else
 				saveIt = FALSE;
b151ef55
 
565c449d
 			if(saveIt) {
 				cli_dbgmsg("Saving text part to scan\n");
 				/*
 				 * TODO: May be better to save aText
 				 */
 				saveTextPart(mainMessage, dir);
74c6f514
 				if(mainMessage != messageIn) {
 					messageDestroy(mainMessage);
 					mainMessage = NULL;
 				} else
 					messageReset(mainMessage);
 				rc = 1;
c6259ac5
 			}
b151ef55
 		}
565c449d
 	} else
 		rc = (multiparts) ? 1 : 2;	/* anything saved? */
b151ef55
 
2250ea69
 	if(mainMessage && (mainMessage != messageIn))
 		messageDestroy(mainMessage);
 
6613d595
 	if(messages)
 		free(messages);
 
68be129f
 	cli_dbgmsg("parseEmailBody() returning %d\n", rc);
b151ef55
 
68be129f
 	return rc;
b151ef55
 }
 
 /*
  * Is the current line the start of a new section?
  *
  * New sections start with --boundary
  */
 static int
 boundaryStart(const char *line, const char *boundary)
 {
273cd2bb
 	char *ptr, *out;
2ed1bc5a
 	int rc;
e82a5185
 	char buf[RFC2821LENGTH + 1];
ad642304
 
80a8c7d8
 	if(line == NULL)
 		return 0;	/* empty line */
 
c29ebe66
 	/*cli_dbgmsg("boundaryStart: line = '%s' boundary = '%s'\n", line, boundary);*/
ad642304
 
4b54f2e0
 	if((*line != '-') && (*line != '('))
 		return 0;
 
 	if(strchr(line, '-') == NULL)
 		return 0;
 
273cd2bb
 	if(strlen(line) <= sizeof(buf)) {
 		out = NULL;
 		ptr = rfc822comments(line, buf);
 	} else
 		out = ptr = rfc822comments(line, NULL);
 
ad642304
 	if(ptr == NULL)
aa479b7d
 		ptr = (char *)line;
ad642304
 
 	if(*ptr++ != '-') {
273cd2bb
 		if(out)
 			free(out);
80a8c7d8
 		return 0;
ad642304
 	}
80a8c7d8
 
b151ef55
 	/*
80a8c7d8
 	 * Gibe.B3 is broken, it has:
b151ef55
 	 *	boundary="---- =_NextPart_000_01C31177.9DC7C000"
 	 * but it's boundaries look like
 	 *	------ =_NextPart_000_01C31177.9DC7C000
80a8c7d8
 	 * notice the one too few '-'.
 	 * Presumably this is a deliberate exploitation of a bug in some mail
 	 * clients.
 	 *
 	 * The trouble is that this creates a lot of false positives for
 	 * boundary conditions, if we're too lax about matches. We do our level
 	 * best to avoid these false positives. For example if we have
 	 * boundary="1" we want to ensure that we don't break out of every line
 	 * that has -1 in it instead of starting --1. This needs some more work.
f60a8d41
 	 *
 	 * Look with and without RFC822 comments stripped, I've seen some
 	 * samples where () are taken as comments in boundaries and some where
 	 * they're not. Irrespective of whatever RFC2822 says we need to find
 	 * viruses in both types of mails
b151ef55
 	 */
f60a8d41
 	if((strstr(ptr, boundary) != NULL) || (strstr(line, boundary) != NULL))
2ed1bc5a
 		rc = 1;
 	else if(*ptr++ != '-')
 		rc = 0;
 	else
4ce9c996
 		rc = (strcasecmp(ptr, boundary) == 0);
2ed1bc5a
 
273cd2bb
 	if(out)
 		free(out);
2ed1bc5a
 
 	if(rc == 1)
 		cli_dbgmsg("boundaryStart: found %s in %s\n", boundary, line);
 
 	return rc;
b151ef55
 }
 
 /*
  * Is the current line the end?
  *
  * The message ends with with --boundary--
  */
 static int
 endOfMessage(const char *line, const char *boundary)
 {
 	size_t len;
 
98685ac1
 	if(line == NULL)
 		return 0;
c29ebe66
 	/*cli_dbgmsg("endOfMessage: line = '%s' boundary = '%s'\n", line, boundary);*/
b151ef55
 	if(*line++ != '-')
 		return 0;
 	if(*line++ != '-')
 		return 0;
 	len = strlen(boundary);
c6259ac5
 	if(strncasecmp(line, boundary, len) != 0)
 		return 0;
ef3cf57d
 	/*
 	 * Use < rather than == because some broken mails have white
 	 * space after the boundary
 	 */
39b5a552
 	if(strlen(line) < (len + 2))
b151ef55
 		return 0;
 	line = &line[len];
 	if(*line++ != '-')
 		return 0;
 	return *line == '-';
 }
 
 /*
  * Initialise the various lookup tables
  */
 static int
 initialiseTables(table_t **rfc821Table, table_t **subtypeTable)
 {
 	const struct tableinit *tableinit;
 
 	/*
 	 * Initialise the various look up tables
 	 */
 	*rfc821Table = tableCreate();
 	assert(*rfc821Table != NULL);
 
 	for(tableinit = rfc821headers; tableinit->key; tableinit++)
7b8fb055
 		if(tableInsert(*rfc821Table, tableinit->key, tableinit->value) < 0) {
 			tableDestroy(*rfc821Table);
4d9c0ca8
 			*rfc821Table = NULL;
b151ef55
 			return -1;
7b8fb055
 		}
b151ef55
 
 	*subtypeTable = tableCreate();
 	assert(*subtypeTable != NULL);
 
 	for(tableinit = mimeSubtypes; tableinit->key; tableinit++)
 		if(tableInsert(*subtypeTable, tableinit->key, tableinit->value) < 0) {
 			tableDestroy(*rfc821Table);
7b8fb055
 			tableDestroy(*subtypeTable);
4d9c0ca8
 			*rfc821Table = NULL;
 			*subtypeTable = NULL;
b151ef55
 			return -1;
 		}
 
 	return 0;
 }
 
 /*
0bcad2b1
  * If there's a HTML text version use that, otherwise
b151ef55
  * use the first text part, otherwise just use the
0bcad2b1
  * first one around. HTML text is most likely to include
  * a scripting worm
b151ef55
  *
  * If we can't find one, return -1
  */
 static int
 getTextPart(message *const messages[], size_t size)
 {
 	size_t i;
b4cb4486
 	int textpart = -1;
b151ef55
 
 	for(i = 0; i < size; i++) {
 		assert(messages[i] != NULL);
b4cb4486
 		if(messageGetMimeType(messages[i]) == TEXT) {
 			if(strcasecmp(messageGetMimeSubtype(messages[i]), "html") == 0)
 				return (int)i;
 			textpart = (int)i;
 		}
b151ef55
 	}
b4cb4486
 	return textpart;
b151ef55
 }
 
 /*
  * strip -
4d9c0ca8
  *	Remove the trailing spaces from a buffer. Don't call this directly,
  * always call strstrip() which is a wrapper to this routine to be used with
  * NUL terminated strings. This code looks a bit strange because of it's
  * heritage from code that worked on strings that weren't necessarily NUL
  * terminated.
  * TODO: rewrite for clamAV
  *
b151ef55
  * Returns it's new length (a la strlen)
  *
  * len must be int not size_t because of the >= 0 test, it is sizeof(buf)
  *	not strlen(buf)
  */
 static size_t
 strip(char *buf, int len)
 {
 	register char *ptr;
 	register size_t i;
 
 	if((buf == NULL) || (len <= 0))
4d9c0ca8
 		return 0;
b151ef55
 
 	i = strlen(buf);
 	if(len > (int)(i + 1))
4d9c0ca8
 		return i;
b151ef55
 	ptr = &buf[--len];
 
f0635204
 #if    defined(UNIX) || defined(C_LINUX) || defined(C_DARWIN) || defined(C_KFREEBSD_GNU) /* watch - it may be in shared text area */
b151ef55
 	do
 		if(*ptr)
 			*ptr = '\0';
6b93ea0c
 	while((--len >= 0) && (!isgraph(*--ptr)) && (*ptr != '\n') && (*ptr != '\r'));
b151ef55
 #else	/* more characters can be displayed on DOS */
 	do
 #ifndef	REAL_MODE_DOS
 		if(*ptr)	/* C8.0 puts into a text area */
 #endif
 			*ptr = '\0';
 	while((--len >= 0) && ((*--ptr == '\0') || (isspace((int)*ptr))));
 #endif
 	return((size_t)(len + 1));
 }
 
 /*
  * strstrip:
  *	Strip a given string
  */
3db105a2
 size_t
b151ef55
 strstrip(char *s)
 {
 	if(s == (char *)NULL)
 		return(0);
98685ac1
 
b151ef55
 	return(strip(s, strlen(s) + 1));
 }
 
 /*
10c3ed55
  * Some broken email headers use ';' at the end of a line to continue
  * to the next line and don't add a leading white space on the next line
b151ef55
  */
 static bool
 continuationMarker(const char *line)
 {
10c3ed55
 	const char *ptr;
 
 	if(line == NULL)
 		return FALSE;
 
 #ifdef	CL_DEBUG
 	cli_dbgmsg("continuationMarker(%s)\n", line);
 #endif
 
 	if(strlen(line) == 0)
 		return FALSE;
 
 	ptr = strchr(line, '\0');
 
 	assert(ptr != NULL);
 
 	while(ptr > line)
 		switch(*--ptr) {
 			case '\n':
 			case '\r':
 			case ' ':
 			case '\t':
 				continue;
 			case ';':
 				return TRUE;
 			default:
 				return FALSE;
 		}
 
b151ef55
 	return FALSE;
 }
 
 static int
 parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const char *arg)
 {
0cb54827
 	char *copy, *p;
 	const char *ptr;
56d8328d
 	int commandNumber;
f2b068fb
 
b151ef55
 	cli_dbgmsg("parseMimeHeader: cmd='%s', arg='%s'\n", cmd, arg);
56d8328d
 
273cd2bb
 	copy = rfc822comments(cmd, NULL);
0cb54827
 	if(copy) {
 		commandNumber = tableFind(rfc821Table, copy);
 		free(copy);
e9bdeb72
 	} else
 		commandNumber = tableFind(rfc821Table, cmd);
56d8328d
 
273cd2bb
 	copy = rfc822comments(arg, NULL);
b151ef55
 
0cb54827
 	if(copy)
 		ptr = copy;
 	else
 		ptr = arg;
ad3d1172
 
56d8328d
 	switch(commandNumber) {
b151ef55
 		case CONTENT_TYPE:
 			/*
 			 * Fix for non RFC1521 compliant mailers
 			 * that send content-type: Text instead
 			 * of content-type: Text/Plain, or
 			 * just simply "Content-Type:"
 			 */
a8c7e017
 			if(arg == NULL)
f1c33aa0
 				/*
 				 * According to section 4 of RFC1521:
 				 * "Note also that a subtype specification is
 				 * MANDATORY. There are no default subtypes"
 				 *
21cd233d
 				 * We have to break this and make an assumption
f1c33aa0
 				 * for the subtype because virus writers and
 				 * email client writers don't get it right
 				 */
 				 cli_warnmsg("Empty content-type received, no subtype specified, assuming text/plain; charset=us-ascii\n");
0cb54827
 			else if(strchr(ptr, '/') == NULL)
f1c33aa0
 				/*
 				 * Empty field, such as
 				 *	Content-Type:
 				 * which I believe is illegal according to
 				 * RFC1521
 				 */
0cb54827
 				cli_dbgmsg("Invalid content-type '%s' received, no subtype specified, assuming text/plain; charset=us-ascii\n", ptr);
b151ef55
 			else {
137740e1
 				int i;
6fd6d771
 				char *mimeArgs;	/* RHS of the ; */
 
b151ef55
 				/*
 				 * Some clients are broken and
 				 * put white space after the ;
 				 */
7e572372
 				if(*arg == '/') {
 					cli_warnmsg("Content-type '/' received, assuming application/octet-stream\n");
 					messageSetMimeType(m, "application");
 					messageSetMimeSubtype(m, "octet-stream");
 				} else {
 					/*
de509b8e
 					 * The content type could be in quotes:
 					 *	Content-Type: "multipart/mixed"
 					 * FIXME: this is a hack in that ignores
 					 *	the quotes, it doesn't handle
 					 *	them properly
7e572372
 					 */
0cb54827
 					while(isspace(*ptr))
 						ptr++;
 					if(ptr[0] == '\"')
 						ptr++;
de509b8e
 
0cb54827
 					if(ptr[0] != '/') {
e9bdeb72
 						char *s;
 						char *mimeType;	/* LHS of the ; */
0cb54827
 #ifdef CL_THREAD_SAFE
 						char *strptr;
 #endif
e9bdeb72
 
0cb54827
 						s = mimeType = cli_strtok(ptr, 0, ";");
de509b8e
 						/*
6fd6d771
 						 * Handle
 						 * Content-Type: foo/bar multipart/mixed
 						 * and
 						 * Content-Type: multipart/mixed foo/bar
de509b8e
 						 */
803055ea
 						if(s && *s) for(;;) {
897fd9c7
 #ifdef	CL_THREAD_SAFE
6fd6d771
 							int set = messageSetMimeType(m, strtok_r(s, "/", &strptr));
897fd9c7
 #else
 							int set = messageSetMimeType(m, strtok(s, "/"));
 #endif
6fd6d771
 
 							/*
 							 * Stephen White <stephen@earth.li>
 							 * Some clients put space after
 							 * the mime type but before
 							 * the ;
 							 */
897fd9c7
 #ifdef	CL_THREAD_SAFE
6fd6d771
 							s = strtok_r(NULL, ";", &strptr);
897fd9c7
 #else
 							s = strtok(NULL, ";");
 #endif
6fd6d771
 							if(s == NULL)
 								break;
 							if(set) {
e9bdeb72
 								size_t len = strstrip(s) - 1;
6fd6d771
 								if(s[len] == '\"') {
 									s[len] = '\0';
 									len = strstrip(s);
 								}
 								if(len) {
e9bdeb72
 									if(strchr(s, ' ')) {
 										char *t = cli_strtok(s, 0, " ");
6fd6d771
 
e9bdeb72
 										messageSetMimeSubtype(m, t);
 										free(t);
 									} else
 										messageSetMimeSubtype(m, s);
6fd6d771
 								}
de509b8e
 							}
6fd6d771
 
 							while(*s && !isspace(*s))
 								s++;
 							if(*s++ == '\0')
 								break;
 							if(*s == '\0')
 								break;
de509b8e
 						}
803055ea
 						if(mimeType)
 							free(mimeType);
e9bdeb72
 					}
7e572372
 				}
b151ef55
 
 				/*
0704dad8
 				 * Add in all rest of the the arguments.
 				 * e.g. if the header is this:
 				 * Content-Type:', arg='multipart/mixed; boundary=foo
 				 * we find the boundary argument set it
b151ef55
 				 */
137740e1
 				i = 1;
0cb54827
 				while((mimeArgs = cli_strtok(ptr, i++, ";")) != NULL) {
137740e1
 					cli_dbgmsg("mimeArgs = '%s'\n", mimeArgs);
 
6fd6d771
 					messageAddArguments(m, mimeArgs);
 					free(mimeArgs);
 				}
b151ef55
 			}
 			break;
 		case CONTENT_TRANSFER_ENCODING:
0cb54827
 			messageSetEncoding(m, ptr);
b151ef55
 			break;
 		case CONTENT_DISPOSITION:
0cb54827
 			p = cli_strtok(ptr, 0, ";");
 			if(p) {
 				if(*p) {
 					messageSetDispositionType(m, p);
 					free(p);
 					p = cli_strtok(ptr, 1, ";");
 					messageAddArgument(m, p);
 				}
 				free(p);
897fd9c7
 			}
55b1a5f8
 			if((p = (char *)messageFindArgument(m, "filename")) == NULL)
 				/*
 				 * Handle this type of header, without
 				 * a filename (e.g. some Worm.Torvil.D)
 				 *	Content-ID: <nRfkHdrKsAxRU>
 				 * Content-Transfer-Encoding: base64
 				 * Content-Disposition: attachment
 				 */
 				messageAddArgument(m, "filename=unknown");
 			else
 				free(p);
b151ef55
 	}
0cb54827
 	if(copy)
 		free(copy);
b151ef55
 
f2b068fb
 	return 0;
b151ef55
 }
 
68be129f
 /*
5a01973c
  * Save the text portion of the message
  */
 static void
 saveTextPart(message *m, const char *dir)
 {
1e06e1ab
 	fileblob *fb;
5a01973c
 
 	messageAddArgument(m, "filename=textportion");
1e06e1ab
 	if((fb = messageToFileblob(m, dir)) != NULL) {
5a01973c
 		/*
 		 * Save main part to scan that
 		 */
37819555
 		cli_dbgmsg("Saving main message\n");
5a01973c
 
1e06e1ab
 		fileblobDestroy(fb);
5a01973c
 	}
 }
 
90905415
 /*
e9bdeb72
  * Handle RFC822 comments in headers.
273cd2bb
  * If out == NULL, return a buffer without the comments, the caller must free
  *	the returned buffer
  * Return NULL on error or if the input * has no comments.
e9bdeb72
  * See secion 3.4.3 of RFC822
90905415
  * TODO: handle comments that go on to more than one line
  */
 static char *
273cd2bb
 rfc822comments(const char *in, char *out)
90905415
 {
 	const char *iptr;
273cd2bb
 	char *optr;
90905415
 	int backslash, inquote, commentlevel;
 
 	if(in == NULL)
e9bdeb72
 		return NULL;
90905415
 
 	if(strchr(in, '(') == NULL)
e9bdeb72
 		return NULL;
90905415
 
273cd2bb
 	assert(out != in);
 
 	if(out == NULL) {
 		out = cli_malloc(strlen(in) + 1);
 		if(out == NULL)
 			return NULL;
 	}
90905415
 
 	backslash = commentlevel = inquote = 0;
 	optr = out;
 
 	cli_dbgmsg("rfc822comments: contains a comment\n");
 
 	for(iptr = in; *iptr; iptr++)
 		if(backslash) {
0a94ffaf
 			if(commentlevel == 0)
 				*optr++ = *iptr;
90905415
 			backslash = 0;
 		} else switch(*iptr) {
 			case '\\':
 				backslash = 1;
 				break;
 			case '\"':
f60a8d41
 				*optr++ = '\"';
90905415
 				inquote = !inquote;
 				break;
 			case '(':
f60a8d41
 				if(inquote)
 					*optr++ = '(';
 				else
 					commentlevel++;
90905415
 				break;
 			case ')':
f60a8d41
 				if(inquote)
 					*optr++ = ')';
 				else if(commentlevel > 0)
90905415
 					commentlevel--;
 				break;
 			default:
 				if(commentlevel == 0)
 					*optr++ = *iptr;
 		}
 
 	if(backslash)	/* last character was a single backslash */
 		*optr++ = '\\';
 	*optr = '\0';
 
273cd2bb
 	/*strstrip(out);*/
90905415
 
 	cli_dbgmsg("rfc822comments '%s'=>'%s'\n", in, out);
 
 	return out;
 }
0674e2af
 
 /*
  * Handle RFC2047 encoding. Returns a malloc'd buffer that the caller must
  * free, or NULL on error
  */
 static char *
 rfc2047(const char *in)
 {
 	char *out, *pout;
 	size_t len;
 
95f98162
 	if((strstr(in, "=?") == NULL) || (strstr(in, "?=") == NULL))
0674e2af
 		return strdup(in);
 
 	cli_dbgmsg("rfc2047 '%s'\n", in);
 	out = cli_malloc(strlen(in) + 1);
 
 	if(out == NULL)
 		return NULL;
 
 	pout = out;
 
 	/* For each RFC2047 string */
 	while(*in) {
291ac47f
 		char encoding, *ptr, *enctext;
0674e2af
 		message *m;
 		blob *b;
 
 		/* Find next RFC2047 string */
 		while(*in) {
 			if((*in == '=') && (in[1] == '?')) {
 				in += 2;
 				break;
 			}
 			*pout++ = *in++;
 		}
 		/* Skip over charset, find encoding */
 		while((*in != '?') && *in)
 			in++;
 		if(*in == '\0')
 			break;
 		encoding = *++in;
 		encoding = tolower(encoding);
 
 		if((encoding != 'q') && (encoding != 'b')) {
e82a5185
 			cli_warnmsg("Unsupported RFC2047 encoding type '%c' - if you believe this file contains a virus, submit it to www.clamav.net\n", encoding);
c3400886
 			free(out);
 			out = NULL;
0674e2af
 			break;
 		}
 		/* Skip to encoded text */
 		if(*++in != '?')
 			break;
 		if(*++in == '\0')
 			break;
 
291ac47f
 		enctext = strdup(in);
 		if(enctext == NULL) {
 			free(out);
 			out = NULL;
 			break;
 		}
0674e2af
 		in = strstr(in, "?=");
291ac47f
 		if(in == NULL) {
 			free(enctext);
0674e2af
 			break;
291ac47f
 		}
0674e2af
 		in += 2;
 		ptr = strstr(enctext, "?=");
 		assert(ptr != NULL);
 		*ptr = '\0';
 		/*cli_dbgmsg("Need to decode '%s' with method '%c'\n", enctext, encoding);*/
 
 		m = messageCreate();
37819555
 		if(m == NULL)
0674e2af
 			break;
321d5c00
 		messageAddStr(m, enctext);
291ac47f
 		free(enctext);
4d9c0ca8
 		switch(encoding) {
0674e2af
 			case 'q':
 				messageSetEncoding(m, "quoted-printable");
 				break;
 			case 'b':
 				messageSetEncoding(m, "base64");
 				break;
 		}
 		b = messageToBlob(m);
 		len = blobGetDataSize(b);
 		cli_dbgmsg("Decoded as '%*.*s'\n", len, len, blobGetData(b));
 		memcpy(pout, blobGetData(b), len);
 		blobDestroy(b);
 		messageDestroy(m);
 		if(pout[len - 1] == '\n')
 			pout += len - 1;
 		else
 			pout += len;
 
 	}
5e5a162c
 	if(out == NULL)
 		return NULL;
 
 	*pout = '\0';
0674e2af
 
5e5a162c
 	cli_dbgmsg("rfc2047 returns '%s'\n", out);
0674e2af
 	return out;
 }
 
9a7398ee
 #ifdef	PARTIAL_DIR
 /*
  * Handle partial messages
  */
 static int
 rfc1341(message *m, const char *dir)
 {
 	fileblob *fb;
3a0946f5
 	char *arg, *id, *number, *total, *oldfilename;
d85c1fad
 	const char *tmpdir;
a7a6fff4
 	char pdir[NAME_MAX + 1];
d85c1fad
 
53ee0b60
 	id = (char *)messageFindArgument(m, "id");
 	if(id == NULL)
 		return -1;
 
f0635204
 #ifdef  C_CYGWIN
9180b8bb
 	if((tmpdir = getenv("TEMP")) == (char *)NULL)
 		if((tmpdir = getenv("TMP")) == (char *)NULL)
 			if((tmpdir = getenv("TMPDIR")) == (char *)NULL)
 				tmpdir = "C:\\";
d85c1fad
 #else
9180b8bb
 	if((tmpdir = getenv("TMPDIR")) == (char *)NULL)
 		if((tmpdir = getenv("TMP")) == (char *)NULL)
 			if((tmpdir = getenv("TEMP")) == (char *)NULL)
d85c1fad
 #ifdef	P_tmpdir
9180b8bb
 				tmpdir = P_tmpdir;
d85c1fad
 #else
9180b8bb
 				tmpdir = "/tmp";
d85c1fad
 #endif
 #endif
9a7398ee
 
a7a6fff4
 	snprintf(pdir, sizeof(pdir) - 1, "%s/clamav-partial", tmpdir);
d85c1fad
 
 	if((mkdir(pdir, 0700) < 0) && (errno != EEXIST)) {
 		cli_errmsg("Can't create the directory '%s'\n", pdir);
9a7398ee
 		return -1;
3a0946f5
 	} else {
 		struct stat statb;
 
d85c1fad
 		if(stat(pdir, &statb) < 0) {
 			cli_errmsg("Can't stat the directory '%s'\n", pdir);
3a0946f5
 			return -1;
 		}
 		if(statb.st_mode & 077)
 			cli_warnmsg("Insecure partial directory %s (mode 0%o)\n",
d85c1fad
 				pdir, statb.st_mode & 0777);
9a7398ee
 	}
 
 	number = (char *)messageFindArgument(m, "number");
 	if(number == NULL) {
 		free(id);
 		return -1;
 	}
 
 	oldfilename = (char *)messageFindArgument(m, "filename");
 	if(oldfilename == NULL)
 		oldfilename = (char *)messageFindArgument(m, "name");
 
 	arg = cli_malloc(10 + strlen(id) + strlen(number));
e82a5185
 	if(arg) {
 		sprintf(arg, "filename=%s%s", id, number);
 		messageAddArgument(m, arg);
 		free(arg);
 	}
9a7398ee
 
 	if(oldfilename) {
 		cli_warnmsg("Must reset to %s\n", oldfilename);
 		free(oldfilename);
 	}
 
d85c1fad
 	if((fb = messageToFileblob(m, pdir)) == NULL) {
9a7398ee
 		free(id);
 		free(number);
 		return -1;
 	}
 
 	fileblobDestroy(fb);
 
 	total = (char *)messageFindArgument(m, "total");
 	cli_dbgmsg("rfc1341: %s, %s of %s\n", id, number, (total) ? total : "?");
 	if(total) {
 		int n = atoi(number);
 		int t = atoi(total);
 		DIR *dd = NULL;
 
53ee0b60
 		free(total);
9a7398ee
 		/*
 		 * If it's the last one - reassemble it
138b73f6
 		 * FIXME: this assumes that we receive the parts in order
9a7398ee
 		 */
d85c1fad
 		if((n == t) && ((dd = opendir(pdir)) != NULL)) {
9a7398ee
 			FILE *fout;
 			char outname[NAME_MAX + 1];
e82a5185
 			time_t now;
9a7398ee
 
 			snprintf(outname, sizeof(outname) - 1, "%s/%s", dir, id);
 
 			cli_dbgmsg("outname: %s\n", outname);
 
 			fout = fopen(outname, "wb");
 			if(fout == NULL) {
138b73f6
 				cli_errmsg("Can't open '%s' for writing", outname);
9a7398ee
 				free(id);
 				free(number);
 				closedir(dd);
 				return -1;
 			}
 
e82a5185
 			time(&now);
9a7398ee
 			for(n = 1; n <= t; n++) {
 				char filename[NAME_MAX + 1];
3a0946f5
 				const struct dirent *dent;
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
a77dc192
 				union {
 					struct dirent d;
 					char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
 				} result;
3a0946f5
 #endif
9a7398ee
 
 				snprintf(filename, sizeof(filename), "%s%d", id, n);
2c7b958d
 
a77dc192
 #ifdef HAVE_READDIR_R_3
 				while((readdir_r(dd, &result.d, &dent) == 0) && dent) {
9a7398ee
 #elif defined(HAVE_READDIR_R_2)
a77dc192
 				while((dent = (struct dirent *)readdir_r(dd, &result.d))) {
2c7b958d
 #else	/*!HAVE_READDIR_R*/
9a7398ee
 				while((dent = readdir(dd))) {
 #endif
 					FILE *fin;
e82a5185
 					char buffer[BUFSIZ], fullname[NAME_MAX + 1];
9a7398ee
 					int nblanks;
d85c1fad
 					extern short cli_leavetemps_flag;
e82a5185
 					struct stat statb;
9a7398ee
 
a6d49269
 #ifndef  C_CYGWIN
9a7398ee
 					if(dent->d_ino == 0)
 						continue;
a6d49269
 #endif
9a7398ee
 
e82a5185
 					snprintf(fullname, sizeof(fullname) - 1,
 						"%s/%s", pdir, dent->d_name);
 
 					if(strncmp(filename, dent->d_name, strlen(filename)) != 0) {
 						if(!cli_leavetemps_flag)
 							continue;
 						if(stat(fullname, &statb) < 0)
 							continue;
 						if(now - statb.st_mtime > (time_t)(7 * 24 * 3600))
 							if(unlink(fullname) >= 0)
 								cli_warnmsg("removed old RFC1341 file %s\n", fullname);
9a7398ee
 						continue;
e82a5185
 					}
9a7398ee
 
e82a5185
 					fin = fopen(fullname, "rb");
9a7398ee
 					if(fin == NULL) {
e82a5185
 						cli_errmsg("Can't open '%s' for reading", fullname);
9a7398ee
 						fclose(fout);
 						unlink(outname);
 						free(id);
 						free(number);
 						closedir(dd);
 						return -1;
 					}
 					nblanks = 0;
d99b1840
 					while(fgets(buffer, sizeof(buffer) - 1, fin) != NULL)
9a7398ee
 						/*
 						 * Ensure that trailing newlines
 						 * aren't copied
 						 */
53ee0b60
 						if(buffer[0] == '\n')
9a7398ee
 							nblanks++;
53ee0b60
 						else {
9a7398ee
 							if(nblanks)
 								do
 									putc('\n', fout);
 								while(--nblanks > 0);
 							fputs(buffer, fout);
 						}
 					fclose(fin);
d85c1fad
 
 					/* don't unlink if leave temps */
 					if(!cli_leavetemps_flag)
e82a5185
 						unlink(fullname);
9a7398ee
 					break;
 				}
 				rewinddir(dd);
 			}
 			closedir(dd);
 			fclose(fout);
 		}
 	}
53ee0b60
 	free(number);
9a7398ee
 	free(id);
 
 	return 0;
 }
 #endif
 
f52d7358
 #if	defined(FOLLOWURLS) && (FOLLOWURLS > 0)
c5ed8336
 static void
 checkURLs(message *m, const char *dir)
 {
 	blob *b = messageToBlob(m);
 	size_t len;
6b93ea0c
 	table_t *t;
6da40aa1
 	int i, n;
314ff77b
 #if	defined(WITH_CURL) && defined(CL_THREAD_SAFE)
f52d7358
 	pthread_t tid[FOLLOWURLS];
 	struct arg args[FOLLOWURLS];
314ff77b
 #endif
6da40aa1
 	tag_arguments_t hrefs;
c5ed8336
 
 	if(b == NULL)
 		return;
 
 	len = blobGetDataSize(b);
 
e94471f4
 	if(len == 0) {
 		blobDestroy(b);
3eb12bae
 		return;
e94471f4
 	}
3eb12bae
 
6b93ea0c
 	/* TODO: make this size customisable */
 	if(len > 100*1024) {
 		cli_warnmsg("Viruses pointed to by URL not scanned in large message\n");
 		blobDestroy(b);
e94471f4
 		return;
6b93ea0c
 	}
 
e94471f4
 	blobClose(b);
6b93ea0c
 	t = tableCreate();
3a0ef2ee
 	if(t == NULL) {
 		blobDestroy(b);
 		return;
 	}
3eb12bae
 
a2d786fc
 	hrefs.count = 0;
 	hrefs.tag = hrefs.value = NULL;
6da40aa1
 
 	cli_dbgmsg("checkURLs: calling html_normalise_mem\n");
3a0ef2ee
 	if(!html_normalise_mem(blobGetData(b), len, NULL, &hrefs)) {
7d3d11d0
 		blobDestroy(b);
 		tableDestroy(t);
 		return;
3a0ef2ee
 	}
 	cli_dbgmsg("checkURLs: html_normalise_mem returned\n");
 
e745ac7e
 	/* TODO: Do we need to call remove_html_comments? */
6b93ea0c
 
6da40aa1
 	n = 0;
 
 	for(i = 0; i < hrefs.count; i++) {
a77dc192
 		const char *url = (const char *)hrefs.value[i];
6da40aa1
 
e82a5185
 		/*
 		 * TODO: If it's an image source, it'd be nice to note beacons
 		 *	where width="0" height="0", which needs support from
 		 *	the HTML normalise code
 		 */
6da40aa1
 		if(strncasecmp("http://", url, 7) == 0) {
 			char *ptr;
314ff77b
 #ifdef	WITH_CURL
 #ifndef	CL_THREAD_SAFE
 			struct arg arg;
 #endif
 
 #else	/*!WITH_CURL*/
bf6f653d
 #ifdef	CL_THREAD_SAFE
 			static pthread_mutex_t system_mutex = PTHREAD_MUTEX_INITIALIZER;
 #endif
6b93ea0c
 			struct stat statb;
 			char cmd[512];
314ff77b
 #endif	/*WITH_CURL*/
a95c894a
 			char name[NAME_MAX + 1];
6da40aa1
 
 			if(tableFind(t, url) == 1) {
 				cli_dbgmsg("URL %s already downloaded\n", url);
f2b068fb
 				continue;
 			}
e82a5185
 			/*
 			 * What about foreign character spoofing?
 			 * It would be useful be able to check if url
 			 *	is the same as the text displayed, e.g.
 			 *	<a href="http://dodgy.biz">www.paypal.com</a>
 			 *	but that needs support from HTML normalise
 			 */
 			if(strchr(url, '%') && strchr(url, '@'))
 				cli_warnmsg("Possible URL spoofing attempt noticed, but not yet handled (%s)\n", url);
 
f52d7358
 			if(n == FOLLOWURLS) {
e82a5185
 				cli_warnmsg("URL %s will not be scanned\n", url);
de617e3e
 				break;
 			}
e82a5185
 
6da40aa1
 			(void)tableInsert(t, url, 1);
 			cli_dbgmsg("Downloading URL %s to be scanned\n", url);
2176c0e5
 			strncpy(name, url, sizeof(name) - 1);
39d09964
 			name[sizeof(name) - 1] = '\0';
6da40aa1
 			for(ptr = name; *ptr; ptr++)
 				if(*ptr == '/')
 					*ptr = '_';
c5ed8336
 
da812a6a
 #ifdef	WITH_CURL
314ff77b
 #ifdef	CL_THREAD_SAFE
a95c894a
 			args[n].dir = dir;
 			args[n].url = url;
314ff77b
 			args[n].filename = strdup(name);
 			pthread_create(&tid[n], NULL, getURL, &args[n]);
 #else
6da40aa1
 			arg.url = url;
314ff77b
 			arg.dir = dir;
 			arg.filename = name;
 			getURL(&arg);
 #endif
 
3fa72383
 #else
 			/*
 			 * TODO: maximum size and timeouts
 			 */
2176c0e5
 			len = sizeof(cmd) - 26 - strlen(dir) - strlen(name);
 #ifdef	CL_DEBUG
e82a5185
 			snprintf(cmd, sizeof(cmd) - 1, "GET -t10 \"%.*s\" >%s/%s", len, url, dir, name);
2176c0e5
 #else
e82a5185
 			snprintf(cmd, sizeof(cmd) - 1, "GET -t10 \"%.*s\" >%s/%s 2>/dev/null", len, url, dir, name);
2176c0e5
 #endif
 			cmd[sizeof(cmd) - 1] = '\0';
 
c5ed8336
 			cli_dbgmsg("%s\n", cmd);
bf6f653d
 #ifdef	CL_THREAD_SAFE
 			pthread_mutex_lock(&system_mutex);
 #endif
c5ed8336
 			system(cmd);
bf6f653d
 #ifdef	CL_THREAD_SAFE
 			pthread_mutex_unlock(&system_mutex);
 #endif
 			snprintf(cmd, sizeof(cmd), "%s/%s", dir, name);
 			if(stat(cmd, &statb) >= 0)
 				if(statb.st_size == 0) {
6da40aa1
 					cli_warnmsg("URL %s failed to download\n", url);
bf6f653d
 					/*
 					 * Don't bother scanning an empty file
 					 */
 					(void)unlink(cmd);
 				}
3fa72383
 #endif
314ff77b
 			++n;
c5ed8336
 		}
 	}
 	blobDestroy(b);
f2b068fb
 	tableDestroy(t);
314ff77b
 
 #if	defined(WITH_CURL) && defined(CL_THREAD_SAFE)
f52d7358
 	assert(n <= FOLLOWURLS);
314ff77b
 	cli_dbgmsg("checkURLs: waiting for %d thread(s) to finish\n", n);
 	while(--n >= 0) {
 		pthread_join(tid[n], NULL);
 		free(args[n].filename);
 	}
 #endif
a95c894a
 	html_tag_arg_free(&hrefs);
c5ed8336
 }
 
e82a5185
 /*
  * Includes some Win32 patches by Gianluigi Tiesi <sherpya@netfarm.it>
  *
  * FIXME: Often WMF exploits work by sending people an email directing them
  *	to a page which displays a picture containing the exploit. This is not
  *	currently found, since only the HTML on the referred page is downloaded.
  *	It would be useful to scan the HTML for references to pictures and
  *	download them for scanning. But that will hit performance so there is
  *	an issue here.
  */
da812a6a
 #ifdef	WITH_CURL
314ff77b
 static void *
 #ifdef	CL_THREAD_SAFE
 getURL(void *a)
 #else
 getURL(struct arg *arg)
 #endif
3fa72383
 {
6b93ea0c
 	CURL *curl;
3fa72383
 	FILE *fp;
6b93ea0c
 	struct curl_slist *headers;
 	static int initialised = 0;
314ff77b
 #ifdef	CL_THREAD_SAFE
 	static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
 	struct arg *arg = (struct arg *)a;
 #endif
 	const char *url = arg->url;
 	const char *dir = arg->dir;
 	const char *filename = arg->filename;
0cb54827
 	char fout[NAME_MAX + 1];
9c8806fb
 #ifdef	CURLOPT_ERRORBUFFER
e82a5185
 	char errorbuffer[CURL_ERROR_SIZE];
 #elif	(LIBCURL_VERSION_NUM >= 0x070C00)
 	CURLcode res = CURLE_OK;
9c8806fb
 #endif
3fa72383
 
314ff77b
 #ifdef	CL_THREAD_SAFE
 	pthread_mutex_lock(&init_mutex);
 #endif
6b93ea0c
 	if(!initialised) {
e82a5185
 		if(curl_global_init(CURL_GLOBAL_ALL) != 0) {
314ff77b
 #ifdef	CL_THREAD_SAFE
 			pthread_mutex_unlock(&init_mutex);
 #endif
e82a5185
 			cli_errmsg("curl_global_init failed");
314ff77b
 			return NULL;
 		}
6b93ea0c
 		initialised = 1;
3fa72383
 	}
314ff77b
 #ifdef	CL_THREAD_SAFE
 	pthread_mutex_unlock(&init_mutex);
 #endif
 
6b93ea0c
 	/* easy isn't the word I'd use... */
 	curl = curl_easy_init();
e82a5185
 	if(curl == NULL) {
 		cli_errmsg("curl_easy_init failed");
314ff77b
 		return NULL;
e82a5185
 	}
da812a6a
 
6b93ea0c
 	(void)curl_easy_setopt(curl, CURLOPT_USERAGENT, "www.clamav.net");
 
e82a5185
 	if(curl_easy_setopt(curl, CURLOPT_URL, url) != 0) {
 		cli_errmsg("%s: curl_easy_setopt failed", url);
 		curl_easy_cleanup(curl);
314ff77b
 		return NULL;
e82a5185
 	}
6b93ea0c
 
a95c894a
 	snprintf(fout, NAME_MAX, "%s/%s", dir, filename);
3fa72383
 
e82a5185
 	fp = fopen(fout, "wb");
3fa72383
 
 	if(fp == NULL) {
138b73f6
 		cli_errmsg("Can't open '%s' for writing", fout);
da812a6a
 		curl_easy_cleanup(curl);
314ff77b
 		return NULL;
3fa72383
 	}
05ea2522
 #ifdef	CURLOPT_WRITEDATA
314ff77b
 	if(curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp) != 0) {
 		fclose(fp);
 		curl_easy_cleanup(curl);
 		return NULL;
 	}
05ea2522
 #else
 	if(curl_easy_setopt(curl, CURLOPT_FILE, fp) != 0) {
 		fclose(fp);
 		curl_easy_cleanup(curl);
 		return NULL;
 	}
 #endif
314ff77b
 
6b93ea0c
 	/*
3eb12bae
 	 * If an item is in squid's cache get it from there (TCP_HIT/200)
6b93ea0c
 	 * by default curl doesn't (TCP_CLIENT_REFRESH_MISS/200)
 	 */
 	headers = curl_slist_append(NULL, "Pragma:");
 	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
3fa72383
 
6b93ea0c
 	/* These should be customisable */
 	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
 	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
49674596
 #ifdef	CURLOPT_MAXFILESIZE
 	curl_easy_setopt(curl, CURLOPT_MAXFILESIZE, 50*1024);
 #endif
3fa72383
 
314ff77b
 #ifdef  CL_THREAD_SAFE
c07de365
 #ifdef	CURLOPT_DNS_USE_GLOBAL_CACHE
314ff77b
 	curl_easy_setopt(curl, CURLOPT_DNS_USE_GLOBAL_CACHE, 0);
 #endif
c07de365
 #endif
02406150
 
 	/*
 	 * Prevent password: prompting with older versions
 	 * FIXME: a better username?
 	 */
66df01fa
 	curl_easy_setopt(curl, CURLOPT_USERPWD, "username:password");
02406150
 
314ff77b
 	/*
 	 * FIXME: valgrind reports "pthread_mutex_unlock: mutex is not locked"
 	 * from gethostbyaddr_r within this. It may be a bug in libcurl
 	 * rather than this code, but I need to check, see Curl_resolv()
 	 * If pushed really hard it will sometimes say
 	 * Conditional jump or move depends on uninitialised value(s) and
 	 * quit. But the program seems to work OK without valgrind...
 	 * Perhaps Curl_resolv() isn't thread safe?
e82a5185
 	 *
 	 * I have seen segfaults in version 7.12.3. Version 7.14 seems OK.
314ff77b
 	 */
8386482b
 	/*
aa479b7d
 	 * On some C libraries (notably with FC3, glibc-2.3.3-74) you get a
e82a5185
 	 * memory leak here in getaddrinfo(), see
0856891e
 	 *	https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=139559
8386482b
 	 */
9c8806fb
 #ifdef	CURLOPT_ERRORBUFFER
e82a5185
 	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorbuffer);
 
 	if(curl_easy_perform(curl) != CURLE_OK)
9c8806fb
 		cli_warnmsg("URL %s failed to download: %s\n", url, errorbuffer);
e82a5185
 #elif	(LIBCURL_VERSION_NUM >= 0x070C00)
 	if((res = curl_easy_perform(curl)) != CURLE_OK)
 		cli_warnmsg("URL %s failed to download: %s\n", url,
 			curl_easy_strerror(res));
9c8806fb
 #else
e82a5185
 	if(curl_easy_perform(curl) != CURLE_OK)
6b93ea0c
 		cli_warnmsg("URL %s failed to download\n", url);
9c8806fb
 #endif
6b93ea0c
 
 	fclose(fp);
 	curl_easy_cleanup(curl);
e82a5185
 	curl_slist_free_all(headers);
314ff77b
 
 	return NULL;
3fa72383
 }
 #endif
 
 #else
 static void
 checkURLs(message *m, const char *dir)
 {
 }
 #endif
 
d1382234
 #ifdef HAVE_BACKTRACE
f2b068fb
 static void
98685ac1
 sigsegv(int sig)
 {
 	signal(SIGSEGV, SIG_DFL);
d1382234
 	print_trace(1);
98685ac1
 	exit(SIGSEGV);
 }
 
f2b068fb
 static void
98685ac1
 print_trace(int use_syslog)
 {
 	void *array[10];
 	size_t size;
 	char **strings;
 	size_t i;
 	pid_t pid = getpid();
 
 	size = backtrace(array, 10);
 	strings = backtrace_symbols(array, size);
 
 	if(use_syslog == 0)
 		cli_dbgmsg("Backtrace of pid %d:\n", pid);
735377bc
 	else
98685ac1
 		syslog(LOG_ERR, "Backtrace of pid %d:", pid);
 
 	for(i = 0; i < size; i++)
 		if(use_syslog)
60aec445
 			syslog(LOG_ERR, "bt[%u]: %s", i, strings[i]);
98685ac1
 		else
 			cli_dbgmsg("%s\n", strings[i]);
 
7d3d11d0
 	/* TODO: dump the current email */
 
98685ac1
 	free(strings);
 }
 #endif
00615ec9
 
82933497
 static bool
 usefulHeader(int commandNumber, const char *cmd)
 {
 	switch(commandNumber) {
 		case CONTENT_TRANSFER_ENCODING:
 		case CONTENT_DISPOSITION:
 		case CONTENT_TYPE:
 			return TRUE;
 		default:
 			if(strcasecmp(cmd, "From") == 0)
 				return TRUE;
 			else if(strcasecmp(cmd, "Received") == 0)
 				return TRUE;
 			else if(strcasecmp(cmd, "De") == 0)
 				return TRUE;
 	}
 
 	return FALSE;
 }
 
b3a5cdd8
 /*
9c8806fb
  * Like fgets but cope with end of line by "\n", "\r\n", "\n\r", "\r"
  */
 static char *
c840fa41
 getline_from_mbox(char *buffer, size_t len, FILE *fin)
9c8806fb
 {
 	char *ret;
 
 	if(feof(fin))
 		return NULL;
 
 	if((len == 0) || (buffer == NULL)) {
c840fa41
 		cli_errmsg("Invalid call to getline_from_mbox(). Report to bugs@clamav.net\n");
9c8806fb
 		return NULL;
 	}
 
 	ret = buffer;
 
 	do {
 		int c = getc(fin);
 
 		if(ferror(fin))
 			return NULL;
 
 		switch(c) {
 			case '\n':
 				*buffer++ = '\n';
 				c = getc(fin);
 				if((c != '\r') && !feof(fin))
 					ungetc(c, fin);
 				break;
 			default:
e82a5185
 				*buffer++ = (char)c;
9c8806fb
 				continue;
 			case EOF:
 				break;
 			case '\r':
 				*buffer++ = '\n';
 				c = getc(fin);
 				if((c != '\n') && !feof(fin))
 					ungetc(c, fin);
 				break;
 		}
 		break;
e82a5185
 	} while(--len > 1);
9c8806fb
 
 	if(len == 0) {
e82a5185
 		/* the email probably breaks RFC821 */
 		cli_warnmsg("getline_from_mbox: buffer overflow stopped - line lost\n");
9c8806fb
 		return NULL;
 	}
e82a5185
 	if(len == 1)
 		/* over flows will have appear on separate lines */
 		cli_dbgmsg("getline_from_mbox: buffer overflow stopped - line recovered\n");
9c8806fb
 	*buffer = '\0';
 
 	return ret;
 }