Browse code

Tidy up multipart handling

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@723 77e5149b-7576-45b1-b177-96237e5ba77b

Nigel Horne authored on 2004/08/05 04:00:43
Showing 3 changed files
... ...
@@ -1,3 +1,7 @@
1
+Wed Aug  4 19:59:54 BST 2004 (njh)
2
+----------------------------------
3
+  * libclamav:	Improved the decoding of multipart messages and MIME headers
4
+
1 5
 Wed Aug  4 20:01:26 CEST 2004 (tk)
2 6
 ----------------------------------
3 7
   * libclamav: fix compilation error with Sun's compiler (reported by
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: mbox.c,v $
20
+ * Revision 1.91  2004/08/04 18:59:19  nigelhorne
21
+ * Tidy up multipart handling
22
+ *
20 23
  * Revision 1.90  2004/07/26 17:02:56  nigelhorne
21 24
  * Fix crash when debugging on SPARC
22 25
  *
... ...
@@ -258,7 +261,7 @@
258 258
  * Compilable under SCO; removed duplicate code with message.c
259 259
  *
260 260
  */
261
-static	char	const	rcsid[] = "$Id: mbox.c,v 1.90 2004/07/26 17:02:56 nigelhorne Exp $";
261
+static	char	const	rcsid[] = "$Id: mbox.c,v 1.91 2004/08/04 18:59:19 nigelhorne Exp $";
262 262
 
263 263
 #if HAVE_CONFIG_H
264 264
 #include "clamav-config.h"
... ...
@@ -601,7 +604,6 @@ cli_mbox(const char *dir, int desc)
601 601
 static message *
602 602
 parseEmailHeaders(message *m, const table_t *rfc821Table, bool destroy)
603 603
 {
604
-	bool inContinuationHeader = FALSE;	/* state machine: ugh */
605 604
 	bool inHeader = TRUE;
606 605
 	text *t;
607 606
 	message *ret;
... ...
@@ -640,13 +642,7 @@ parseEmailHeaders(message *m, const table_t *rfc821Table, bool destroy)
640 640
 		 * a continuation of the previous entry.
641 641
 		 */
642 642
 		if(inHeader && buffer &&
643
-		  ((buffer[0] == '\t') || (buffer[0] == ' ')))
644
-			inContinuationHeader = TRUE;
645
-
646
-		if(inContinuationHeader) {
647
-			if(!continuationMarker(buffer))
648
-				inContinuationHeader = FALSE;	 /* no more args */
649
-
643
+		  ((buffer[0] == '\t') || (buffer[0] == ' '))) {
650 644
 			/*
651 645
 			 * Add all the arguments on the line
652 646
 			 */
... ...
@@ -664,11 +660,11 @@ parseEmailHeaders(message *m, const table_t *rfc821Table, bool destroy)
664 664
 			 */
665 665
 			if(buffer == NULL) {
666 666
 				cli_dbgmsg("End of header information\n");
667
-				inContinuationHeader = inHeader = FALSE;
668
-			} else if(parseEmailHeader(ret, buffer, rfc821Table) == CONTENT_TYPE)
669
-				inContinuationHeader = continuationMarker(buffer);
670
-			if(buffer)
667
+				inHeader = FALSE;
668
+			} else {
669
+				(void)parseEmailHeader(ret, buffer, rfc821Table);
671 670
 				free(buffer);
671
+			}
672 672
 		} else {
673 673
 			/*cli_dbgmsg("Add line to body '%s'\n", buffer);*/
674 674
 			if(messageAddLine(ret, buffer, 0) < 0)
... ...
@@ -731,9 +727,8 @@ parseEmailHeader(message *m, const char *line, const table_t *rfc821Table)
731 731
  * This function parses the body of mainMessage and saves its attachments in dir
732 732
  *
733 733
  * mainMessage is the buffer to be parsed, it contains an e-mail's body, without
734
- * any headers. First
735
- * time of calling it'll be
736
- *	the whole message. Later it'll be parts of a multipart message
734
+ * any headers. First time of calling it'll be
735
+ * the whole message. Later it'll be parts of a multipart message
737 736
  * textIn is the plain text message being built up so far
738 737
  * blobsIn contains the array of attachments found so far
739 738
  *
... ...
@@ -876,10 +871,8 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
876 876
 				message **m;
877 877
 
878 878
 				m = cli_realloc(messages, ((multiparts + 1) * sizeof(message *)));
879
-				if(m == NULL) {
880
-					multiparts--;
879
+				if(m == NULL)
881 880
 					break;
882
-				}
883 881
 				messages = m;
884 882
 
885 883
 				aMessage = messages[multiparts] = messageCreate();
... ...
@@ -1164,7 +1157,9 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
1164 1164
 					bool addAttachment = FALSE;
1165 1165
 					bool addToText = FALSE;
1166 1166
 					const char *dtype;
1167
+#if	0
1167 1168
 					message *body;
1169
+#endif
1168 1170
 
1169 1171
 					aMessage = messages[i];
1170 1172
 
... ...
@@ -1320,6 +1315,7 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
1320 1320
 						 * be an attachment
1321 1321
 						 */
1322 1322
 						cli_dbgmsg("Found multipart inside multipart\n");
1323
+#if	0
1323 1324
 						if(aMessage) {
1324 1325
 							body = parseEmailHeaders(aMessage, rfc821Table, TRUE);
1325 1326
 							if(body) {
... ...
@@ -1339,6 +1335,18 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
1339 1339
 
1340 1340
 								mainMessage = body;
1341 1341
 							}
1342
+#else
1343
+						if(aMessage) {
1344
+							/*
1345
+							 * The headers were parsed when reading in the
1346
+							 * whole multipart section
1347
+							 */
1348
+							rc = parseEmailBody(aMessage, blobs, nBlobs, aText, dir, rfc821Table, subtypeTable);
1349
+							cli_dbgmsg("Finished recursion\n");
1350
+							assert(aMessage == messages[i]);
1351
+							messageDestroy(messages[i]);
1352
+							messages[i] = NULL;
1353
+#endif
1342 1354
 						} else {
1343 1355
 							rc = parseEmailBody(NULL, blobs, nBlobs, NULL, dir, rfc821Table, subtypeTable);
1344 1356
 							if(mainMessage && (mainMessage != messageIn))
... ...
@@ -1598,6 +1606,11 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
1598 1598
 		}
1599 1599
 	}
1600 1600
 
1601
+	if(aText && (textIn == NULL)) {
1602
+		textDestroy(aText);
1603
+		aText = NULL;
1604
+	}
1605
+
1601 1606
 	cli_dbgmsg("%d attachments found\n", nBlobs);
1602 1607
 
1603 1608
 	if(nBlobs == 0) {
... ...
@@ -1719,6 +1732,9 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
1719 1719
 
1720 1720
 				if(saveIt) {
1721 1721
 					cli_dbgmsg("Saving text part to scan\n");
1722
+					/*
1723
+					 * TODO: May be better to save aText
1724
+					 */
1722 1725
 					saveTextPart(mainMessage, dir);
1723 1726
 				}
1724 1727
 			}
... ...
@@ -1739,9 +1755,6 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
1739 1739
 		}
1740 1740
 	}
1741 1741
 
1742
-	if(aText && (textIn == NULL))
1743
-		textDestroy(aText);
1744
-
1745 1742
 	/* Already done */
1746 1743
 	if(blobs && (blobsIn == NULL))
1747 1744
 		blobArrayDestroy(blobs, nBlobs);
... ...
@@ -1980,7 +1993,7 @@ continuationMarker(const char *line)
1980 1980
 static int
1981 1981
 parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const char *arg)
1982 1982
 {
1983
-	int type = tableFind(rfc821Table, cmd);
1983
+	const int type = tableFind(rfc821Table, cmd);
1984 1984
 #ifdef CL_THREAD_SAFE
1985 1985
 	char *strptr;
1986 1986
 #endif
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: message.c,v $
20
+ * Revision 1.69  2004/08/04 18:59:19  nigelhorne
21
+ * Tidy up multipart handling
22
+ *
20 23
  * Revision 1.68  2004/07/30 11:50:39  nigelhorne
21 24
  * Code tidy
22 25
  *
... ...
@@ -201,7 +204,7 @@
201 201
  * uuencodebegin() no longer static
202 202
  *
203 203
  */
204
-static	char	const	rcsid[] = "$Id: message.c,v 1.68 2004/07/30 11:50:39 nigelhorne Exp $";
204
+static	char	const	rcsid[] = "$Id: message.c,v 1.69 2004/08/04 18:59:19 nigelhorne Exp $";
205 205
 
206 206
 #if HAVE_CONFIG_H
207 207
 #include "clamav-config.h"
... ...
@@ -658,12 +661,22 @@ messageAddArguments(message *m, const char *s)
658 658
 
659 659
 			*ptr = '\0';
660 660
 
661
+#if	0
661 662
 			field = cli_malloc(strlen(key) + strlen(data) + 2);
662 663
 			if(field)
663 664
 				sprintf(field, "%s=%s", key, data);
664 665
 
665 666
 			free((char *)key);
666 667
 			free(data);
668
+#else
669
+			field = cli_realloc((char *)key, strlen(key) + strlen(data) + 2);
670
+			if(field) {
671
+				strcat(field, "=");
672
+				strcat(field, data);
673
+			} else
674
+				free((char *)key);
675
+			free(data);
676
+#endif
667 677
 		} else {
668 678
 			size_t len;
669 679