diff -u -r -X excluding.txt clamav-devel-latest-20041130/clamd/scanner.c clamav-devel-latest-20041130.pizzolato/clamd/scanner.c
--- clamav-devel-latest-20041130/clamd/scanner.c	2004-11-25 09:42:29.000000000 -0800
+++ clamav-devel-latest-20041130.pizzolato/clamd/scanner.c	2004-12-01 13:42:06.163513900 -0800
@@ -273,6 +273,8 @@
 	struct hostent *he;
 	struct cfgstruct *cpt;
 	FILE *tmp = NULL;
+	char *tempname = NULL;
+	char *tempdir = NULL;
 
 
     /* get min port */
@@ -371,7 +373,12 @@
 
     logg("*Accepted connection on port %d, fd %d\n", port, acceptd);
 
-    if((tmp = tmpfile()) == NULL) {
+    if((cpt = cfgopt(copt, "TemporaryDirectory")))
+	tempdir = cpt->strarg;
+    if(((tempname = tempnam(tempdir,"Msg")) == NULL) ||
+       ((tmp = fopen(tempname, "wb+")) == NULL)) {
+	if (tempname)
+	    free(tempname);        
 	shutdown(sockfd, 2);
 	close(sockfd);
 	close(acceptd);
@@ -401,8 +408,11 @@
 	    close(acceptd);
 	    mdprintf(odesc, "Temporary file -> write ERROR\n");
 	    logg("!ScanStream: Can't write to temporary file.\n");
-	    if(tmp)
+	    if(tmp) {
 		fclose(tmp);
+		remove(tempname);
+		free(tempname);
+	    }
 	    return -1;
 	}
 
@@ -427,8 +437,11 @@
 
     lseek(tmpd, 0, SEEK_SET);
     ret = cl_scandesc(tmpd, &virname, scanned, root, limits, options);
-    if(tmp)
+    if(tmp) {
 	fclose(tmp);
+	remove(tempname);
+	free(tempname);
+    }
 
     close(acceptd);
     close(sockfd);
diff -u -r -X excluding.txt clamav-devel-latest-20041130/libclamav/cvd.c clamav-devel-latest-20041130.pizzolato/libclamav/cvd.c
--- clamav-devel-latest-20041130/libclamav/cvd.c	2004-11-16 09:10:47.000000000 -0800
+++ clamav-devel-latest-20041130.pizzolato/libclamav/cvd.c	2004-12-01 13:42:06.193558000 -0800
@@ -363,9 +363,8 @@
 
     fseek(fd, 512, SEEK_SET);
 
-    dir = cli_gentemp(NULL);
-    if(mkdir(dir, 0700)) {
-	cli_errmsg("cli_cvdload():  Can't create temporary directory %s\n", dir);
+    if((dir = cli_gentempdir(NULL)) == NULL) {
+	cli_errmsg("cli_cvdload():  Can't create temporary directory\n");
 	return CL_ETMPDIR;
     }
 
@@ -382,18 +381,17 @@
 
 	    /* start */
 
-	    tmp = cli_gentemp(NULL);
-	    if((tmpd = fopen(tmp, "wb+")) == NULL) {
-		cli_errmsg("Can't create temporary file %s\n", tmp);
+	    if((tmp = cli_gentempstream(NULL, &tmpd)) == NULL) {
+		cli_errmsg("Can't create temporary file\n");
 		free(dir);
-		free(tmp);
 		return CL_ETMPFILE;
 	    }
 
 	    if(!(buffer = (char *) cli_malloc(FILEBUFF))) {
-		free(dir);
 		free(tmp);
 		fclose(tmpd);
+		cli_rmdirs(dir);
+		free(dir);
 		return CL_EMEM;
 	    }
 
@@ -407,16 +405,16 @@
 
 	    if(cli_untgz(fileno(tmpd), dir)) {
 		cli_errmsg("cli_cvdload(): Can't unpack CVD file.\n");
-		cli_rmdirs(dir);
-		free(dir);
 		fclose(tmpd);
-		unlink(tmp);
+		remove(tmp);
 		free(tmp);
+		cli_rmdirs(dir);
+		free(dir);
 		return CL_ECVDEXTR;
 	    }
 
 	    fclose(tmpd);
-	    unlink(tmp);
+	    remove(tmp);
 	    free(tmp);
 
 	    /* end */
diff -u -r -X excluding.txt clamav-devel-latest-20041130/libclamav/others.c clamav-devel-latest-20041130.pizzolato/libclamav/others.c
--- clamav-devel-latest-20041130/libclamav/others.c	2004-11-16 09:10:47.000000000 -0800
+++ clamav-devel-latest-20041130.pizzolato/libclamav/others.c	2004-12-01 14:21:50.022643100 -0800
@@ -343,7 +343,7 @@
     cli_leavetemps_flag = leavetemps;
 }
 
-char *cli_gentemp(const char *dir)
+static char *_cli_gentemp(const char *dir)
 {
 	char *name, *tmp;
         const char *mdir;
@@ -364,14 +364,10 @@
 
     name = (char*) cli_calloc(strlen(mdir) + 1 + 16 + 1 + 7, sizeof(char));
     if(name == NULL) {
-	cli_dbgmsg("cli_gentemp('%s'): out of memory\n", dir);
+	cli_dbgmsg("cli_gentemp('%s'): out of memory\n", mdir);
 	return NULL;
     }
 
-#ifdef CL_THREAD_SAFE
-    pthread_mutex_lock(&cli_gentemp_mutex);
-#endif
-
     memcpy(salt, oldmd5buff, 16);
 
     do {
@@ -384,6 +380,88 @@
 	free(tmp);
     } while(stat(name, &foo) != -1);
 
+    return(name);
+}
+
+char *cli_gentemp(const char *dir)
+{
+	char *name;
+
+#ifdef CL_THREAD_SAFE
+    pthread_mutex_lock(&cli_gentemp_mutex);
+#endif
+
+    name = _cli_gentemp(dir);
+
+#ifdef CL_THREAD_SAFE
+    pthread_mutex_unlock(&cli_gentemp_mutex);
+#endif
+
+    return(name);
+}
+
+char *cli_gentempdir(const char *dir)
+{
+	char *name;
+
+#ifdef CL_THREAD_SAFE
+    pthread_mutex_lock(&cli_gentemp_mutex);
+#endif
+
+    name = _cli_gentemp(dir);
+
+    if (name && mkdir(name, 0700)) {
+	cli_dbgmsg("cli_gentempdir(): can't create temp directory: %s\n", name);
+        free(name);
+        name = NULL;
+    }
+
+#ifdef CL_THREAD_SAFE
+    pthread_mutex_unlock(&cli_gentemp_mutex);
+#endif
+
+    return(name);
+}
+
+char *cli_gentempdesc(const char *dir, int *fd)
+{
+	char *name;
+
+#ifdef CL_THREAD_SAFE
+    pthread_mutex_lock(&cli_gentemp_mutex);
+#endif
+
+    name = _cli_gentemp(dir);
+
+    if (name && ((*fd = open(name, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU)) < 0)) {
+	cli_dbgmsg("cli_gentempdesc(): can't create temp file: %s\n", name);
+        free(name);
+        name = NULL;
+    }
+
+#ifdef CL_THREAD_SAFE
+    pthread_mutex_unlock(&cli_gentemp_mutex);
+#endif
+
+    return(name);
+}
+
+char *cli_gentempstream(const char *dir, FILE **fs)
+{
+	char *name;
+
+#ifdef CL_THREAD_SAFE
+    pthread_mutex_lock(&cli_gentemp_mutex);
+#endif
+
+    name = _cli_gentemp(dir);
+
+    if (name && ((*fs = fopen(name, "wb+")) == NULL)) {
+	cli_dbgmsg("cli_gentempstream(): can't create temp file: %s\n", name);
+        free(name);
+        name = NULL;
+    }
+
 #ifdef CL_THREAD_SAFE
     pthread_mutex_unlock(&cli_gentemp_mutex);
 #endif
diff -u -r -X excluding.txt clamav-devel-latest-20041130/libclamav/others.h clamav-devel-latest-20041130.pizzolato/libclamav/others.h
--- clamav-devel-latest-20041130/libclamav/others.h	2004-09-01 07:14:21.000000000 -0700
+++ clamav-devel-latest-20041130.pizzolato/libclamav/others.h	2004-12-01 13:42:06.263660900 -0800
@@ -36,6 +36,9 @@
 int cli_writen(int fd, void *buff, unsigned int count);
 int32_t cli_readint32(const char *buff);
 char *cli_gentemp(const char *dir);
+char *cli_gentempdir(const char *dir);
+char *cli_gentempdesc(const char *dir, int *fd);
+char *cli_gentempstream(const char *dir, FILE **fs);
 unsigned int cli_rndnum(unsigned int max);
 int cli_memstr(const char *haystack, int hs, const char *needle, int ns);
 
diff -u -r -X excluding.txt clamav-devel-latest-20041130/libclamav/pe.c clamav-devel-latest-20041130.pizzolato/libclamav/pe.c
--- clamav-devel-latest-20041130/libclamav/pe.c	2004-11-26 12:11:03.000000000 -0800
+++ clamav-devel-latest-20041130.pizzolato/libclamav/pe.c	2004-12-01 13:42:06.293705000 -0800
@@ -697,10 +697,8 @@
 		oldep = EC32(optional_hdr.AddressOfEntryPoint) + 161 + 6 + cli_readint32(buff+163);
 		cli_dbgmsg("FSG: found old EP @%x\n", oldep);
 
-		tempfile = cli_gentemp(NULL);
-		if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU)) < 0) {
-		    cli_dbgmsg("FSG: Can't create file %s\n", tempfile);
-		    free(tempfile);
+		if((tempfile = cli_gentempdesc(NULL, &ndesc)) == NULL) {
+		    cli_dbgmsg("FSG: Can't create file\n");
 		    free(section_hdr);
 		    free(src);
 		    free(dest);
@@ -889,10 +887,8 @@
 		oldep = EC32(optional_hdr.AddressOfEntryPoint) + gp + 6 + cli_readint32(src+gp+2+oldep);
 		cli_dbgmsg("FSG: found old EP @%x\n", oldep);
 
-		tempfile = cli_gentemp(NULL);
-		if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU)) < 0) {
-		    cli_dbgmsg("FSG: Can't create file %s\n", tempfile);
-		    free(tempfile);
+		if((tempfile = cli_gentempdesc(NULL, &ndesc)) == NULL) {
+		    cli_dbgmsg("FSG: Can't create file\n");
 		    free(section_hdr);
 		    free(src);
 		    free(dest);
@@ -1090,10 +1086,8 @@
 		int ndesc;
 
 	    if(cli_leavetemps_flag) {
-		tempfile = cli_gentemp(NULL);
-		if((ndesc = open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU)) < 0) {
-		    cli_dbgmsg("UPX/FSG: Can't create file %s\n", tempfile);
-		    free(tempfile);
+		if((tempfile = cli_gentempdesc(NULL, &ndesc)) == NULL) {
+		    cli_dbgmsg("UPX/FSG: Can't create file\n");
 		    free(section_hdr);
 		    free(src);
 		    free(dest);
@@ -1175,10 +1169,8 @@
 		}
 	    }
 
-	    tempfile = cli_gentemp(NULL);
-	    if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU)) < 0) {
-		cli_dbgmsg("Petite: Can't create file %s\n", tempfile);
-		free(tempfile);
+	    if((tempfile = cli_gentempdesc(NULL, &ndesc)) == NULL) {
+		cli_dbgmsg("Petite: Can't create file\n");
 		free(section_hdr);
 		free(dest);
 		return CL_EIO;
diff -u -r -X excluding.txt clamav-devel-latest-20041130/libclamav/scanners.c clamav-devel-latest-20041130.pizzolato/libclamav/scanners.c
--- clamav-devel-latest-20041130/libclamav/scanners.c	2004-11-27 10:56:39.000000000 -0800
+++ clamav-devel-latest-20041130.pizzolato/libclamav/scanners.c	2004-12-01 14:19:20.563260300 -0800
@@ -42,12 +42,6 @@
 
 #include <mspack.h>
 
-#ifdef CL_THREAD_SAFE
-#  include <pthread.h>
-pthread_mutex_t cli_scanrar_mutex = PTHREAD_MUTEX_INITIALIZER;
-#endif
-int cli_scanrar_inuse = 0;
-
 extern short cli_leavetemps_flag;
 
 extern int cli_mbox(const char *dir, int desc, unsigned int options); /* FIXME */
@@ -108,17 +102,10 @@
 static int cli_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec);
 
 
-#ifdef CL_THREAD_SAFE
-static void cli_unlock_mutex(void *mtx)
-{
-    cli_dbgmsg("Pthread cancelled. Unlocking mutex.\n");
-    pthread_mutex_unlock(mtx);
-}
-#endif
-
 static int cli_scanrar(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
 {
 	FILE *tmp = NULL;
+	char *tempname = NULL;
 	int files = 0, fd, ret = CL_CLEAN, afiles;
 	ArchiveList_struct *rarlist = NULL;
 	ArchiveList_struct *rarlist_head = NULL;
@@ -129,17 +116,7 @@
     cli_dbgmsg("in scanrar()\n");
 
 
-#ifdef CL_THREAD_SAFE
-    pthread_cleanup_push(cli_unlock_mutex, &cli_scanrar_mutex);
-    pthread_mutex_lock(&cli_scanrar_mutex);
-    cli_scanrar_inuse = 1;
-#endif
-
     if(! (afiles = urarlib_list(desc, (ArchiveList_struct *) &rarlist))) {
-#ifdef CL_THREAD_SAFE
-	pthread_mutex_unlock(&cli_scanrar_mutex);
-	cli_scanrar_inuse = 0;
-#endif
 	return CL_ERAR;
     }
 
@@ -209,12 +186,9 @@
             continue;
         }
 
-	if((tmp = tmpfile()) == NULL) {
+	
+	if((tempname = cli_gentempstream(NULL, &tmp)) == NULL) {
 	    cli_dbgmsg("RAR: Can't generate temporary file.\n");
-#ifdef CL_THREAD_SAFE
-	    pthread_mutex_unlock(&cli_scanrar_mutex);
-	    cli_scanrar_inuse = 0;
-#endif
 	    return CL_ETMPFILE;
 	}
 	fd = fileno(tmp);
@@ -225,6 +199,9 @@
 		cli_dbgmsg("RAR: Can't write to file.\n");
 		fclose(tmp);
 		tmp = NULL;
+		remove(tempname);
+		free(tempname);
+		tempname = NULL;
 		ret = CL_ERAR;
 		if(rar_data_ptr) {
 		    free(rar_data_ptr);
@@ -240,11 +217,9 @@
 	    if(fflush(tmp) != 0) {
 		cli_dbgmsg("RAR: fflush() failed: %s\n", strerror(errno));
 		fclose(tmp);
+		remove(tempname);
+		free(tempname);
 		urarlib_freelist(rarlist_head);
-#ifdef CL_THREAD_SAFE
-		pthread_mutex_unlock(&cli_scanrar_mutex);
-		cli_scanrar_inuse = 0;
-#endif
 		return CL_EFSYNC;
 	    }
 
@@ -253,11 +228,9 @@
 		cli_dbgmsg("RAR: Infected with %s\n", *virname);
 
 		fclose(tmp);
+		remove(tempname);
+		free(tempname);
 		urarlib_freelist(rarlist);
-#ifdef CL_THREAD_SAFE
-		pthread_mutex_unlock(&cli_scanrar_mutex);
-		cli_scanrar_inuse = 0;
-#endif
   		return ret;
 	    }
 
@@ -265,22 +238,23 @@
 	    cli_dbgmsg("RAR: Can't decompress file %s\n", rarlist->item.Name);
 	    fclose(tmp);
 	    tmp = NULL;
+	    remove(tempname);
+	    free(tempname);
+	    tempname = NULL;
 	    ret = CL_ERAR; /* WinRAR 3.0 ? */
 	    break;
 	}
 
 	fclose(tmp);
 	tmp = NULL;
+	remove(tempname);
+	free(tempname);
+	tempname = NULL;
 	rarlist = rarlist->next;
 	files++;
     }
 
     urarlib_freelist(rarlist_head);
-#ifdef CL_THREAD_SAFE
-    pthread_mutex_unlock(&cli_scanrar_mutex);
-    cli_scanrar_inuse = 0;
-    pthread_cleanup_pop(0);
-#endif
     
     cli_dbgmsg("RAR: Exit code: %d\n", ret);
 
@@ -704,7 +678,7 @@
 		ret = cli_scanfile(tempname, virname, scanned, root, limits, options, arec, mrec);
 	    }
 	    if(!cli_leavetemps_flag)
-		unlink(tempname);
+		remove(tempname);
 	    free(tempname);
 	    if(ret == CL_VIRUS)
 		break;
@@ -727,9 +701,8 @@
 
     cli_dbgmsg("in cli_scanhtml()\n");
 
-    tempname = cli_gentemp(NULL);
-    if(mkdir(tempname, 0700)) {
-        cli_dbgmsg("ScanHTML -> Can't create temporary directory %s\n", tempname);
+    if((tempname = cli_gentempdir(NULL)) == NULL) {
+        cli_dbgmsg("ScanHTML -> Can't create temporary directory\n");
         return CL_ETMPDIR;
     }
 
@@ -995,9 +968,8 @@
     cli_dbgmsg("in cli_scanole2()\n");
 
     /* generate the temporary directory */
-    dir = cli_gentemp(NULL);
-    if(mkdir(dir, 0700)) {
-	cli_dbgmsg("OLE2: Can't create temporary directory %s\n", dir);
+    if((dir = cli_gentempdir(NULL)) == NULL) {
+	cli_dbgmsg("OLE2: Can't create temporary directory\n");
 	return CL_ETMPDIR;
     }
 
@@ -1030,9 +1002,8 @@
     cli_dbgmsg("in cli_scantar()\n");
 
     /* generate temporary directory */
-    dir = cli_gentemp(NULL);
-    if(mkdir(dir, 0700)) {
-	cli_errmsg("Tar: Can't create temporary directory %s\n", dir);
+    if((dir = cli_gentempdir(NULL)) == NULL) {
+	cli_errmsg("Tar: Can't create temporary directory\n");
 	return CL_ETMPDIR;
     }
 
@@ -1057,10 +1028,8 @@
     cli_dbgmsg("in cli_scanbinhex()\n");
 
     /* generate temporary directory */
-    dir = cli_gentemp(NULL);
-
-    if(mkdir(dir, 0700)) {
-	cli_errmsg("Binhex: Can't create temporary directory %s\n", dir);
+    if((dir = cli_gentempdir(NULL)) == NULL) {
+	cli_errmsg("Binhex: Can't create temporary directory\n");
 	return CL_ETMPDIR;
     }
 
@@ -1084,9 +1053,8 @@
 
     cli_dbgmsg("in cli_scanmschm()\n");	
 
-    tempname = cli_gentemp(NULL);
-    if(mkdir(tempname, 0700)) {
-	cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname);
+    if((tempname = cli_gentempdir(NULL)) == NULL) {
+	cli_dbgmsg("CHM: Can't create temporary directory\n");
 	return CL_ETMPDIR;
     }
 
@@ -1107,9 +1075,8 @@
 
     cli_dbgmsg("in cli_scanscrenc()\n");
 
-    tempname = cli_gentemp(NULL);
-    if(mkdir(tempname, 0700)) {
-	cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname);
+    if((tempname = cli_gentempdir(NULL)) == NULL) {
+	cli_dbgmsg("CHM: Can't create temporary directory\n");
 	return CL_ETMPDIR;
     }
 
@@ -1132,10 +1099,8 @@
     cli_dbgmsg("Starting cli_scanmail(), mrec == %d, arec == %d\n", *mrec, *arec);
 
     /* generate the temporary directory */
-    dir = cli_gentemp(NULL);
-    if(mkdir(dir, 0700)) {
-	cli_dbgmsg("Mail: Can't create temporary directory %s\n", dir);
-	free(dir);
+    if((dir = cli_gentempdir(NULL)) == NULL) {
+	cli_dbgmsg("Mail: Can't create temporary directory\n");
 	return CL_ETMPDIR;
     }
 
@@ -1210,7 +1175,7 @@
 
     switch(type) {
 	case CL_TYPE_RAR:
-	    if(!DISABLE_RAR && SCAN_ARCHIVE && !cli_scanrar_inuse)
+	    if(!DISABLE_RAR && SCAN_ARCHIVE)
 		ret = cli_scanrar(desc, virname, scanned, root, limits, options, arec, mrec);
 	    break;
 
diff -u -r -X excluding.txt clamav-devel-latest-20041130/libclamav/vba_extract.c clamav-devel-latest-20041130.pizzolato/libclamav/vba_extract.c
--- clamav-devel-latest-20041130/libclamav/vba_extract.c	2004-11-16 09:10:47.000000000 -0800
+++ clamav-devel-latest-20041130.pizzolato/libclamav/vba_extract.c	2004-12-01 13:42:06.373822600 -0800
@@ -803,10 +803,8 @@
 	char *out_dir;
 	
 	/* Create a directory to store the extracted OLE2 objects */
-	out_dir = cli_gentemp(NULL);
-	if(mkdir(out_dir, 0700)) {
-	    printf("ScanOLE2 -> Can't create temporary directory %s\n", out_dir);
-	    free(out_dir);
+	if((out_dir = cli_gentempdir(NULL)) == NULL) {
+	    printf("ScanOLE2 -> Can't create temporary directory\n");
 	    close(fd);
 	    return NULL;
 	}
