Browse code

Extract script to file

git-svn: trunk@2348

Nigel Horne authored on 2006/10/08 21:58:25
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Sun Oct  8 13:57:33 BST 2006 (njh)
2
+----------------------------------
3
+  * libclamav/js.c:	Finished first draft of the extraction phase
4
+
1 5
 Sun Oct  8 12:00:28 BST 2006 (njh)
2 6
 ----------------------------------
3 7
   * libclamav/js.[ch]:	Created place holders for the frontend to NGS
... ...
@@ -19,7 +19,7 @@
19 19
  * Save the JavaScript embedded in an HTML file, then run the script, saving
20 20
  * the output in a file that is to be scanned, then remove the script file
21 21
  */
22
-static	char	const	rcsid[] = "$Id: js.c,v 1.2 2006/10/08 11:05:12 njh Exp $";
22
+static	char	const	rcsid[] = "$Id: js.c,v 1.3 2006/10/08 12:57:06 njh Exp $";
23 23
 
24 24
 #if HAVE_CONFIG_H
25 25
 #include "clamav-config.h"
... ...
@@ -27,17 +27,33 @@ static	char	const	rcsid[] = "$Id: js.c,v 1.2 2006/10/08 11:05:12 njh Exp $";
27 27
 
28 28
 #ifdef	CL_EXPERIMENTAL
29 29
 
30
+#if	HAVE_MMAP
31
+
30 32
 #include "clamav.h"
31 33
 #include "others.h"
32 34
 #include <memory.h>
33 35
 #include <string.h>
34
-
35
-#if	HAVE_MMAP
36
+#include <limits.h>
37
+#include <errno.h>
38
+#include <ctype.h>
36 39
 
37 40
 #if HAVE_SYS_MMAN_H
38 41
 #include <sys/mman.h>
39 42
 #endif
40 43
 
44
+/* Maximum filenames under various systems - njh */
45
+#ifndef	NAME_MAX	/* e.g. Linux */
46
+# ifdef	MAXNAMELEN	/* e.g. Solaris */
47
+#   define	NAME_MAX	MAXNAMELEN
48
+# else
49
+#   ifdef	FILENAME_MAX	/* e.g. SCO */
50
+#     define	NAME_MAX	FILENAME_MAX
51
+#   else
52
+#     define	NAME_MAX	256
53
+#   endif
54
+# endif
55
+#endif
56
+
41 57
 static	const	char	*cli_pmemstr(const char *haystack, size_t hs, const char *needle, size_t ns);
42 58
 
43 59
 int
... ...
@@ -49,6 +65,8 @@ cli_scanjs(const char *dir, int desc)
49 49
 	const char *p;
50 50
 	long bytesleft;
51 51
 	int done_header;
52
+	FILE *fout;
53
+	char script_filename[NAME_MAX + 1];
52 54
 
53 55
 	cli_dbgmsg("in cli_scanjs(%s)\n", dir);
54 56
 
... ...
@@ -72,6 +90,7 @@ cli_scanjs(const char *dir, int desc)
72 72
 	p = buf;
73 73
 	bytesleft = size;
74 74
 	done_header = 0;
75
+	fout = NULL;
75 76
 
76 77
 	while(p < &buf[size]) {
77 78
 		const char *q = cli_pmemstr(p, bytesleft, "<script", 7);
... ...
@@ -120,16 +139,43 @@ cli_scanjs(const char *dir, int desc)
120 120
 					break;
121 121
 				}
122 122
 			}
123
-			/*
124
-			 * if(!done_header) {
125
-			 * 	EMIT
126
-			 *		function main()
127
-			 *		{
128
-			 *	END_EMIT
129
-			 *	done_header = true;
130
-			 * }
131
-			 */
132
-			/*putchar(tolower(*p));*/
123
+			if(!done_header) {
124
+				int fd;
125
+
126
+				snprintf(script_filename, sizeof(script_filename), "%s/jsXXXXXX", dir);
127
+#if	defined(C_LINUX) || defined(C_BSD) || defined(HAVE_MKSTEMP) || defined(C_SOLARIS) || defined(C_CYGWIN)
128
+				fd = mkstemp(script_filename);
129
+				fout = fdopen(fd, "wb");
130
+				if(fout == NULL)
131
+					close(fd);
132
+#elif	defined(C_WINDOWS)
133
+				if(_mktemp(script_filename) == NULL) {
134
+					/* mktemp only allows 26 files */
135
+					char *name = cli_gentemp(dir);
136
+					if(name == NULL)
137
+						fout = NULL;
138
+					else {
139
+						strcpy(script_filename, name);
140
+						free(name);
141
+						fout = fopen(script_filename, "wb");
142
+					}
143
+				} else
144
+					fout = fopen(script_filename, "wb");
145
+#else
146
+				mktemp(script_filename);
147
+				fout = fopen(script_filename, "wb");
148
+#endif
149
+
150
+				if(fout == NULL) {
151
+					cli_errmsg("cli_scanjs: can't create temporary file %s: %s\n", script_filename, strerror(errno));
152
+					munmap(buf, size);
153
+					return CL_ETMPFILE;
154
+				}
155
+				fputs("function main()\n{\n", fout);
156
+				done_header = 1;
157
+			}
158
+			putc(tolower(*p), fout);
159
+
133 160
 			p++;
134 161
 			bytesleft--;
135 162
 		}
... ...
@@ -137,17 +183,14 @@ cli_scanjs(const char *dir, int desc)
137 137
 
138 138
 	if(!done_header)
139 139
 		cli_dbgmsg("No javascript was detected\n");
140
+	else if(fout == NULL)
141
+		cli_errmsg("cli_scanjs: fout == NULL\n");
140 142
 	else {
141
-		/*
142
-		 * EMIT
143
-		 *	}
144
-		 *
145
-		 *	main();
146
-		 * END_EMIT
147
-		 * Run NGS on the script file
148
-		 */
143
+		fputs("\n}\nmain();\n", fout);
144
+		fclose(fout);
145
+		/* TODO: NGS on the script file */
149 146
 	}
150
-	/* unlink the script file */
147
+	unlink(script_filename);
151 148
 
152 149
 	munmap(buf, size);
153 150
 	return CL_CLEAN;