Browse code

various cleanups; fix bb#577 (move cli_* out of clamav.h)

git-svn: trunk@3196

Tomasz Kojm authored on 2007/09/01 04:55:09
Showing 16 changed files
... ...
@@ -1,3 +1,7 @@
1
+Fri Aug 31 21:02:46 CEST 2007 (tk)
2
+----------------------------------
3
+  * libclamav: various cleanups; fix bb#577 (move cli_* out of clamav.h)
4
+
1 5
 Fri Aug 31 09:02:23 BST 2007 (njh)
2 6
 ----------------------------------
3 7
   * clamav-milter:	Bug 642
... ...
@@ -109,22 +109,6 @@ extern "C"
109 109
 #define cl_node		cl_engine
110 110
 #define cl_perror	cl_strerror
111 111
 
112
-/* internal structures */
113
-struct cli_md5_node {
114
-    char *virname;
115
-    unsigned char *md5;
116
-    unsigned int size;
117
-    unsigned short fp;
118
-    struct cli_md5_node *next;
119
-};
120
-
121
-struct cli_meta_node {
122
-    int csize, size, method;
123
-    unsigned int crc32, fileno, encrypted, maxdepth;
124
-    char *filename, *virname;
125
-    struct cli_meta_node *next;
126
-};
127
-
128 112
 struct cl_engine {
129 113
     unsigned int refcount; /* reference counter */
130 114
     unsigned short ncore;
... ...
@@ -135,16 +119,16 @@ struct cl_engine {
135 135
     void **root;
136 136
 
137 137
     /* MD5 */
138
-    struct cli_md5_node **md5_hlist;
138
+    void **md5_hlist;
139 139
 
140 140
     /* B-M matcher for MD5 sigs for PE sections */
141 141
     void *md5_sect;
142 142
 
143 143
     /* Zip metadata */
144
-    struct cli_meta_node *zip_mlist;
144
+    void *zip_mlist;
145 145
 
146 146
     /* RAR metadata */
147
-    struct cli_meta_node *rar_mlist;
147
+    void *rar_mlist;
148 148
 
149 149
     /* NodalCore database handle */
150 150
     void *ncdb;
... ...
@@ -45,23 +45,29 @@
45 45
 
46 46
 int cli_untgz(int fd, const char *destdir)
47 47
 {
48
-	char *fullname, osize[13], name[101], type;
48
+	char *path, osize[13], name[101], type;
49 49
 	char block[TAR_BLOCKSIZE];
50
-	int nbytes, nread, nwritten, in_block = 0;
51
-	unsigned int size;
50
+	int nbytes, nread, nwritten, in_block = 0, fdd;
51
+	unsigned int size, pathlen = strlen(destdir) + 100 + 5;
52 52
 	FILE *outfile = NULL;
53 53
 	gzFile *infile;
54 54
 
55
+
55 56
     cli_dbgmsg("in cli_untgz()\n");
56 57
 
57
-    if((infile = gzdopen(fd, "rb")) == NULL) {
58
-	cli_errmsg("Can't gzdopen() descriptor %d, errno = %d\n", fd, errno);
58
+    if((fdd = dup(fd)) == -1) {
59
+	cli_errmsg("cli_untgz: Can't duplicate descriptor %d\n", fd);
60
+	return -1;
61
+    }
62
+
63
+    if((infile = gzdopen(fdd, "rb")) == NULL) {
64
+	cli_errmsg("cli_untgz: Can't gzdopen() descriptor %d, errno = %d\n", fdd, errno);
59 65
 	return -1;
60 66
     }
61 67
 
62
-    fullname = (char *) cli_calloc(sizeof(char), strlen(destdir) + 100 + 5);
63
-    if(!fullname) {
64
-	cli_errmsg("cli_untgz: Can't allocate memory for fullname\n");
68
+    path = (char *) cli_calloc(sizeof(char), pathlen);
69
+    if(!path) {
70
+	cli_errmsg("cli_untgz: Can't allocate memory for path\n");
65 71
 	return -1;
66 72
     }
67 73
 
... ...
@@ -69,12 +75,12 @@ int cli_untgz(int fd, const char *destdir)
69 69
 
70 70
 	nread = gzread(infile, block, TAR_BLOCKSIZE);
71 71
 
72
-	if(!in_block && nread == 0)
72
+	if(!in_block && !nread)
73 73
 	    break;
74 74
 
75 75
 	if(nread != TAR_BLOCKSIZE) {
76
-	    cli_errmsg("Incomplete block read.\n");
77
-	    free(fullname);
76
+	    cli_errmsg("cli_untgz: Incomplete block read\n");
77
+	    free(path);
78 78
 	    gzclose(infile);
79 79
 	    return -1;
80 80
 	}
... ...
@@ -87,16 +93,14 @@ int cli_untgz(int fd, const char *destdir)
87 87
 	    name[100] = '\0';
88 88
 
89 89
 	    if(strchr(name, '/')) {
90
-		cli_errmsg("Slash separators are not allowed in CVD.\n");
91
-		free(fullname);
90
+		cli_errmsg("cli_untgz: Slash separators are not allowed in CVD\n");
91
+		free(path);
92 92
 	        gzclose(infile);
93 93
 		return -1;
94 94
 	    }
95 95
 
96
-	    strcpy(fullname, destdir);
97
-	    strcat(fullname, "/");
98
-	    strcat(fullname, name);
99
-	    cli_dbgmsg("Unpacking %s\n",fullname);
96
+	    snprintf(path, pathlen, "%s/%s", destdir, name);
97
+	    cli_dbgmsg("cli_untgz: Unpacking %s\n", path);
100 98
 	    type = block[156];
101 99
 
102 100
 	    switch(type) {
... ...
@@ -104,32 +108,31 @@ int cli_untgz(int fd, const char *destdir)
104 104
 		case '\0':
105 105
 		    break;
106 106
 		case '5':
107
-		    cli_errmsg("Directories in CVD are not supported.\n");
108
-		    free(fullname);
107
+		    cli_errmsg("cli_untgz: Directories are not supported in CVD\n");
108
+		    free(path);
109 109
 	            gzclose(infile);
110 110
 		    return -1;
111 111
 		default:
112
-		    cli_errmsg("Unknown type flag %c.\n",type);
113
-		    free(fullname);
112
+		    cli_errmsg("cli_untgz: Unknown type flag '%c'\n", type);
113
+		    free(path);
114 114
 	            gzclose(infile);
115 115
 		    return -1;
116 116
 	    }
117
-
118 117
 	    in_block = 1;
119 118
 
120 119
 	    if(outfile) {
121 120
 		if(fclose(outfile)) {
122
-		    cli_errmsg("Cannot close file %s.\n", fullname);
123
-		    free(fullname);
121
+		    cli_errmsg("cli_untgz: Cannot close file %s\n", path);
122
+		    free(path);
124 123
 	            gzclose(infile);
125 124
 		    return -1;
126 125
 		}
127 126
 		outfile = NULL;
128 127
 	    }
129 128
 
130
-	    if(!(outfile = fopen(fullname, "wb"))) {
131
-		cli_errmsg("Cannot create file %s.\n", fullname);
132
-		free(fullname);
129
+	    if(!(outfile = fopen(path, "wb"))) {
130
+		cli_errmsg("cli_untgz: Cannot create file %s\n", path);
131
+		free(path);
133 132
 	        gzclose(infile);
134 133
 		return -1;
135 134
 	    }
... ...
@@ -138,8 +141,8 @@ int cli_untgz(int fd, const char *destdir)
138 138
 	    osize[12] = '\0';
139 139
 
140 140
 	    if((sscanf(osize, "%o", &size)) == 0) {
141
-		cli_errmsg("Invalid size in header.\n");
142
-		free(fullname);
141
+		cli_errmsg("cli_untgz: Invalid size in header\n");
142
+		free(path);
143 143
 	        gzclose(infile);
144 144
 		fclose(outfile);
145 145
 		return -1;
... ...
@@ -150,8 +153,8 @@ int cli_untgz(int fd, const char *destdir)
150 150
 	    nwritten = fwrite(block, 1, nbytes, outfile);
151 151
 
152 152
 	    if(nwritten != nbytes) {
153
-		cli_errmsg("Wrote %d instead of %d (%s).\n", nwritten, nbytes, fullname);
154
-		free(fullname);
153
+		cli_errmsg("cli_untgz: Wrote %d instead of %d (%s)\n", nwritten, nbytes, path);
154
+		free(path);
155 155
 	        gzclose(infile);
156 156
 		return -1;
157 157
 	    }
... ...
@@ -166,34 +169,34 @@ int cli_untgz(int fd, const char *destdir)
166 166
 	fclose(outfile);
167 167
 
168 168
     gzclose(infile);
169
-    free(fullname);
169
+    free(path);
170 170
     return 0;
171 171
 }
172 172
 
173 173
 struct cl_cvd *cl_cvdparse(const char *head)
174 174
 {
175
-	char *pt;
176 175
 	struct cl_cvd *cvd;
176
+	char *pt;
177
+
177 178
 
178 179
     if(strncmp(head, "ClamAV-VDB:", 11)) {
179
-	cli_dbgmsg("Not a CVD head.\n");
180
+	cli_errmsg("cli_cvdparse: Not a CVD file\n");
180 181
 	return NULL;
181 182
     }
182 183
 
183
-    cvd = (struct cl_cvd *) cli_calloc(1, sizeof(struct cl_cvd));
184
-    if(!cvd) {
184
+    if(!(cvd = (struct cl_cvd *) cli_malloc(sizeof(struct cl_cvd)))) {
185 185
 	cli_errmsg("cl_cvdparse: Can't allocate memory for cvd\n");
186 186
 	return NULL;
187 187
     }
188 188
 
189 189
     if(!(cvd->time = cli_strtok(head, 1, ":"))) {
190
-	cli_errmsg("CVD -> Can't extract time from header.\n");
190
+	cli_errmsg("cli_cvdparse: Can't parse the creation time\n");
191 191
 	free(cvd);
192 192
 	return NULL;
193 193
     }
194 194
 
195 195
     if(!(pt = cli_strtok(head, 2, ":"))) {
196
-	cli_errmsg("CVD -> Can't extract version from header.\n");
196
+	cli_errmsg("cli_cvdparse: Can't parse the version number\n");
197 197
 	free(cvd->time);
198 198
 	free(cvd);
199 199
 	return NULL;
... ...
@@ -202,7 +205,7 @@ struct cl_cvd *cl_cvdparse(const char *head)
202 202
     free(pt);
203 203
 
204 204
     if(!(pt = cli_strtok(head, 3, ":"))) {
205
-	cli_errmsg("CVD -> Can't extract signature number from header.\n");
205
+	cli_errmsg("cli_cvdparse: Can't parse the number of signatures\n");
206 206
 	free(cvd->time);
207 207
 	free(cvd);
208 208
 	return NULL;
... ...
@@ -211,7 +214,7 @@ struct cl_cvd *cl_cvdparse(const char *head)
211 211
     free(pt);
212 212
 
213 213
     if(!(pt = cli_strtok(head, 4, ":"))) {
214
-	cli_errmsg("CVD -> Can't extract functionality level from header.\n");
214
+	cli_errmsg("cli_cvdparse: Can't parse the functionality level\n");
215 215
 	free(cvd->time);
216 216
 	free(cvd);
217 217
 	return NULL;
... ...
@@ -220,14 +223,14 @@ struct cl_cvd *cl_cvdparse(const char *head)
220 220
     free(pt);
221 221
 
222 222
     if(!(cvd->md5 = cli_strtok(head, 5, ":"))) {
223
-	cli_errmsg("CVD -> Can't extract MD5 checksum from header.\n");
223
+	cli_errmsg("cli_cvdparse: Can't parse the MD5 checksum\n");
224 224
 	free(cvd->time);
225 225
 	free(cvd);
226 226
 	return NULL;
227 227
     }
228 228
 
229 229
     if(!(cvd->dsig = cli_strtok(head, 6, ":"))) {
230
-	cli_errmsg("CVD -> Can't extract digital signature from header.\n");
230
+	cli_errmsg("cli_cvdparse: Can't parse the digital signature\n");
231 231
 	free(cvd->time);
232 232
 	free(cvd->md5);
233 233
 	free(cvd);
... ...
@@ -235,7 +238,7 @@ struct cl_cvd *cl_cvdparse(const char *head)
235 235
     }
236 236
 
237 237
     if(!(cvd->builder = cli_strtok(head, 7, ":"))) {
238
-	cli_errmsg("CVD -> Can't extract builder name from header.\n");
238
+	cli_errmsg("cli_cvdparse: Can't parse the builder name\n");
239 239
 	free(cvd->time);
240 240
 	free(cvd->md5);
241 241
 	free(cvd->dsig);
... ...
@@ -246,9 +249,10 @@ struct cl_cvd *cl_cvdparse(const char *head)
246 246
     if((pt = cli_strtok(head, 8, ":"))) {
247 247
 	cvd->stime = atoi(pt);
248 248
 	free(pt);
249
-    } else
250
-	cli_dbgmsg("CVD -> No creation time in seconds (old file format)\n");
251
-
249
+    } else {
250
+	cli_dbgmsg("cli_cvdparse: No creation time in seconds (old file format)\n");
251
+	cvd->stime = 0;
252
+    }
252 253
 
253 254
     return cvd;
254 255
 }
... ...
@@ -262,12 +266,12 @@ struct cl_cvd *cl_cvdhead(const char *file)
262 262
 
263 263
 
264 264
     if((fs = fopen(file, "rb")) == NULL) {
265
-	cli_dbgmsg("Can't open CVD file %s\n", file);
265
+	cli_errmsg("cl_cvdhead: Can't open file %s\n", file);
266 266
 	return NULL;
267 267
     }
268 268
 
269 269
     if(!(bread = fread(head, 1, 512, fs))) {
270
-	cli_errmsg("Can't read CVD header of %s\n", file);
270
+	cli_errmsg("cl_cvdhead: Can't read CVD header in %s\n", file);
271 271
 	fclose(fs);
272 272
 	return NULL;
273 273
     }
... ...
@@ -298,9 +302,10 @@ static int cli_cvdverify(FILE *fs, struct cl_cvd *cvdpt)
298 298
 	char *md5, head[513];
299 299
 	int i;
300 300
 
301
+
301 302
     fseek(fs, 0, SEEK_SET);
302 303
     if(fread(head, 1, 512, fs) != 512) {
303
-	cli_dbgmsg("Can't read CVD head from stream\n");
304
+	cli_errmsg("cli_cvdverify: Can't read CVD header\n");
304 305
 	return CL_ECVD;
305 306
     }
306 307
 
... ...
@@ -317,7 +322,7 @@ static int cli_cvdverify(FILE *fs, struct cl_cvd *cvdpt)
317 317
     cli_dbgmsg("MD5(.tar.gz) = %s\n", md5);
318 318
 
319 319
     if(strncmp(md5, cvd->md5, 32)) {
320
-	cli_dbgmsg("MD5 verification error.\n");
320
+	cli_dbgmsg("cli_cvdverify: MD5 verification error\n");
321 321
 	free(md5);
322 322
 	cl_cvdfree(cvd);
323 323
 	return CL_EMD5;
... ...
@@ -325,7 +330,7 @@ static int cli_cvdverify(FILE *fs, struct cl_cvd *cvdpt)
325 325
 
326 326
 #ifdef HAVE_GMP
327 327
     if(cli_versig(md5, cvd->dsig)) {
328
-	cli_dbgmsg("Digital signature verification error.\n");
328
+	cli_dbgmsg("cli_cvdverify: Digital signature verification error\n");
329 329
 	free(md5);
330 330
 	cl_cvdfree(cvd);
331 331
 	return CL_EDSIG;
... ...
@@ -342,8 +347,9 @@ int cl_cvdverify(const char *file)
342 342
 	FILE *fs;
343 343
 	int ret;
344 344
 
345
+
345 346
     if((fs = fopen(file, "rb")) == NULL) {
346
-	cli_errmsg("Can't open CVD file %s\n", file);
347
+	cli_errmsg("cl_cvdverify: Can't open file %s\n", file);
347 348
 	return CL_EOPEN;
348 349
     }
349 350
 
... ...
@@ -357,7 +363,7 @@ int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short
357 357
 {
358 358
         char *dir;
359 359
 	struct cl_cvd cvd;
360
-	int ret, fd;
360
+	int ret;
361 361
 	time_t s_time;
362 362
 
363 363
 
... ...
@@ -372,8 +378,8 @@ int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short
372 372
 	time(&s_time);
373 373
 	if((int) s_time - cvd.stime > 604800) {
374 374
 	    cli_warnmsg("**************************************************\n");
375
-	    cli_warnmsg("***  The virus database is older than 7 days.  ***\n");
376
-	    cli_warnmsg("***        Please update it IMMEDIATELY!       ***\n");
375
+	    cli_warnmsg("***  The virus database is older than 7 days!  ***\n");
376
+	    cli_warnmsg("***   Please update it as soon as possible.    ***\n");
377 377
 	    cli_warnmsg("**************************************************\n");
378 378
 	}
379 379
     }
... ...
@@ -385,14 +391,8 @@ int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short
385 385
 	cli_warnmsg("***********************************************************\n");
386 386
     }
387 387
 
388
-    if((fd = dup(fileno(fs))) == -1) {
389
-	cli_errmsg("cli_cvdload(): Can't duplicate descriptor %d\n", fileno(fs));
390
-	return CL_EIO;
391
-    }
392
-
393
-    if(lseek(fd, 512, SEEK_SET) == -1) {
394
-	cli_errmsg("cli_cvdload(): Can't lseek descriptor %d\n", fd);
395
-	close(fd);
388
+    if(fseek(fs, 512, SEEK_SET) == -1) {
389
+	cli_errmsg("cli_cvdload(): fseek(fs, 512, SEEK_SET) failed\n");
396 390
 	return CL_EIO;
397 391
     }
398 392
 
... ...
@@ -400,12 +400,10 @@ int cli_cvdload(FILE *fs, struct cl_engine **engine, unsigned int *signo, short
400 400
     if(mkdir(dir, 0700)) {
401 401
 	cli_errmsg("cli_cvdload(): Can't create temporary directory %s\n", dir);
402 402
 	free(dir);
403
-	close(fd);
404 403
 	return CL_ETMPDIR;
405 404
     }
406 405
 
407
-    if(cli_untgz(fd, dir)) {
408
-	close(fd);
406
+    if(cli_untgz(fileno(fs), dir)) {
409 407
 	cli_errmsg("cli_cvdload(): Can't unpack CVD file.\n");
410 408
 	free(dir);
411 409
 	return CL_ECVDEXTR;
... ...
@@ -100,7 +100,6 @@ cli_scanjs(const char *dir, int desc)
100 100
 	int created_output, done_header, rc;
101 101
 	FILE *fout;
102 102
 	char script_filename[NAME_MAX + 1];
103
-	extern short cli_leavetemps_flag;
104 103
 
105 104
 	cli_dbgmsg("in cli_scanjs(%s)\n", dir);
106 105
 
... ...
@@ -48,8 +48,6 @@
48 48
 
49 49
 static cli_file_t targettab[CL_TARGET_TABLE_SIZE] = { 0, CL_TYPE_MSEXE, CL_TYPE_MSOLE2, CL_TYPE_HTML, CL_TYPE_MAIL, CL_TYPE_GRAPHICS, CL_TYPE_ELF };
50 50
 
51
-extern short cli_debug_flag;
52
-
53 51
 int cli_scanbuff(const unsigned char *buffer, uint32_t length, const char **virname, const struct cl_engine *engine, cli_file_t ftype)
54 52
 {
55 53
 	int ret = CL_CLEAN;
... ...
@@ -53,6 +53,21 @@ struct cli_matcher {
53 53
     uint32_t ac_partsigs, ac_nodes, ac_patterns;
54 54
 };
55 55
 
56
+struct cli_md5_node {
57
+    char *virname;
58
+    unsigned char *md5;
59
+    unsigned int size;
60
+    unsigned short fp;
61
+    struct cli_md5_node *next;
62
+};
63
+
64
+struct cli_meta_node {
65
+    int csize, size, method;
66
+    unsigned int crc32, fileno, encrypted, maxdepth;
67
+    char *filename, *virname;
68
+    struct cli_meta_node *next;
69
+};
70
+
56 71
 #define CL_TARGET_TABLE_SIZE 7
57 72
 
58 73
 struct cli_target_info {
... ...
@@ -3772,7 +3772,6 @@ rfc1341(message *m, const char *dir)
3772 3772
 					char buffer[BUFSIZ], fullname[NAME_MAX + 1];
3773 3773
 					int nblanks;
3774 3774
 					struct stat statb;
3775
-					extern short cli_leavetemps_flag;
3776 3775
 
3777 3776
 #ifndef  C_CYGWIN
3778 3777
 					if(dent->d_ino == 0)
... ...
@@ -38,8 +38,6 @@
38 38
 #include "matcher.h"
39 39
 #include "scanners.h"
40 40
 
41
-extern short cli_leavetemps_flag;
42
-
43 41
 #ifndef O_BINARY
44 42
 #define O_BINARY 0
45 43
 #endif
... ...
@@ -58,7 +58,7 @@
58 58
 
59 59
 #ifdef CL_THREAD_SAFE
60 60
 #  include <pthread.h>
61
-static pthread_mutex_t cli_gentempname_mutex = PTHREAD_MUTEX_INITIALIZER;
61
+static pthread_mutex_t cli_gentemp_mutex = PTHREAD_MUTEX_INITIALIZER;
62 62
 #endif
63 63
 
64 64
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
... ...
@@ -82,55 +82,38 @@ static pthread_mutex_t cli_gentempname_mutex = PTHREAD_MUTEX_INITIALIZER;
82 82
 
83 83
 #define CL_FLEVEL 21 /* don't touch it */
84 84
 
85
-short cli_debug_flag = 0, cli_leavetemps_flag = 0;
85
+uint8_t cli_debug_flag = 0, cli_leavetemps_flag = 0;
86 86
 
87 87
 static unsigned char name_salt[16] = { 16, 38, 97, 12, 8, 4, 72, 196, 217, 144, 33, 124, 18, 11, 17, 253 };
88 88
 
89 89
 
90
+#define MSGCODE(x)					    \
91
+	va_list args;					    \
92
+	int len = sizeof(x) - 1;			    \
93
+	char buff[BUFSIZ];				    \
94
+    strncpy(buff, x, len);				    \
95
+    va_start(args, str);				    \
96
+    vsnprintf(buff + len, sizeof(buff) - len, str, args);   \
97
+    buff[sizeof(buff) - 1] = '\0';			    \
98
+    fputs(buff, stderr);				    \
99
+    va_end(args)
100
+
101
+
90 102
 void cli_warnmsg(const char *str, ...)
91 103
 {
92
-	va_list args;
93
-	int sz = sizeof("LibClamAV Warning: ") - 1;
94
-	char buff[256];
95
-
96
-    strncpy(buff, "LibClamAV Warning: ", sz);
97
-    va_start(args, str);
98
-    vsnprintf(buff + sz, sizeof(buff) - sz, str, args);
99
-    buff[sizeof(buff) - 1] = '\0';
100
-    fputs(buff, stderr);
101
-    va_end(args);
104
+    MSGCODE("LibClamAV Warning: ");
102 105
 }
103 106
 
104 107
 void cli_errmsg(const char *str, ...)
105 108
 {
106
-	va_list args;
107
-	int sz = sizeof("LibClamAV Error: ") - 1;
108
-	char buff[256];
109
-
110
-    strncpy(buff, "LibClamAV Error: ", sz);
111
-    va_start(args, str);
112
-    vsnprintf(buff + sz, sizeof(buff) - sz, str, args);
113
-    buff[sizeof(buff) - 1] = '\0';
114
-    fputs(buff, stderr);
115
-    va_end(args);
109
+    MSGCODE("LibClamAV Error: ");
116 110
 }
117 111
 
118 112
 void cli_dbgmsg(const char *str, ...)
119 113
 {
120
-
121 114
     if(cli_debug_flag) {
122
-	    va_list args;
123
-	    int sz = sizeof("LibClamAV debug: ") - 1;
124
-	    char buff[BUFSIZ];
125
-
126
-	memcpy(buff, "LibClamAV debug: ", sz);
127
-	va_start(args, str);
128
-	vsnprintf(buff + sz, sizeof(buff) - sz, str, args);
129
-	buff[sizeof(buff) - 1] = '\0';
130
-	fputs(buff, stderr);
131
-	va_end(args);
132
-    } else
133
-	return;
115
+	MSGCODE("LibClamAV debug: ");
116
+    }
134 117
 }
135 118
 
136 119
 void cl_debug(void)
... ...
@@ -333,7 +316,6 @@ void *cli_malloc(size_t size)
333 333
     if(!alloc) {
334 334
 	cli_errmsg("cli_malloc(): Can't allocate memory (%u bytes).\n", size);
335 335
 	perror("malloc_problem");
336
-	/* _exit(1); */
337 336
 	return NULL;
338 337
     } else return alloc;
339 338
 }
... ...
@@ -357,7 +339,6 @@ void *cli_calloc(size_t nmemb, size_t size)
357 357
     if(!alloc) {
358 358
 	cli_errmsg("cli_calloc(): Can't allocate memory (%u bytes).\n", nmemb * size);
359 359
 	perror("calloc_problem");
360
-	/* _exit(1); */
361 360
 	return NULL;
362 361
     } else return alloc;
363 362
 }
... ...
@@ -455,7 +436,7 @@ void cl_settempdir(const char *dir, short leavetemps)
455 455
     cli_leavetemps_flag = leavetemps;
456 456
 }
457 457
 
458
-static char *cli_gentempname(const char *dir)
458
+char *cli_gentemp(const char *dir)
459 459
 {
460 460
 	char *name, *tmp;
461 461
         const char *mdir;
... ...
@@ -474,12 +455,12 @@ static char *cli_gentempname(const char *dir)
474 474
 
475 475
     name = (char *) cli_calloc(strlen(mdir) + 1 + 32 + 1 + 7, sizeof(char));
476 476
     if(!name) {
477
-	cli_dbgmsg("cli_gentempname('%s'): out of memory\n", mdir);
477
+	cli_dbgmsg("cli_gentemp('%s'): out of memory\n", mdir);
478 478
 	return NULL;
479 479
     }
480 480
 
481 481
 #ifdef CL_THREAD_SAFE
482
-    pthread_mutex_lock(&cli_gentempname_mutex);
482
+    pthread_mutex_lock(&cli_gentemp_mutex);
483 483
 #endif
484 484
 
485 485
     memcpy(salt, name_salt, 16);
... ...
@@ -490,12 +471,12 @@ static char *cli_gentempname(const char *dir)
490 490
     tmp = cli_md5buff(salt, 48, name_salt);
491 491
 
492 492
 #ifdef CL_THREAD_SAFE
493
-    pthread_mutex_unlock(&cli_gentempname_mutex);
493
+    pthread_mutex_unlock(&cli_gentemp_mutex);
494 494
 #endif
495 495
 
496 496
     if(!tmp) {
497 497
 	free(name);
498
-	cli_dbgmsg("cli_gentempname('%s'): out of memory\n", mdir);
498
+	cli_dbgmsg("cli_gentemp('%s'): out of memory\n", mdir);
499 499
 	return NULL;
500 500
     }
501 501
 
... ...
@@ -510,65 +491,21 @@ static char *cli_gentempname(const char *dir)
510 510
     return(name);
511 511
 }
512 512
 
513
-char *cli_gentemp(const char *dir)
514
-{
515
-	char *name;
516
-
517
-    name = cli_gentempname(dir);
518
-
519
-    return(name);
520
-}
521
-
522
-
523
-char *cli_gentempdir(const char *dir)
524
-{
525
-	char *name;
526
-
527
-    name = cli_gentempname(dir);
528
-
529
-    if(name && mkdir(name, 0700)) {
530
-	cli_dbgmsg("cli_gentempdir(): can't create temp directory: %s\n", name);
531
-        free(name);
532
-        name = NULL;
533
-    }
534
-
535
-    return(name);
536
-}
537
-
538
-char *cli_gentempdesc(const char *dir, int *fd)
513
+int cli_gentempfd(const char *dir, char **name, int *fd)
539 514
 {
540
-	char *name;
541 515
 
542
-    name = cli_gentempname(dir);
516
+    *name = cli_gentemp(dir);
517
+    if(!*name)
518
+	return CL_EMEM;
543 519
 
544
-    if(name && ((*fd = open(name, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU)) < 0)) {
545
-	cli_dbgmsg("cli_gentempdesc(): can't create temp file: %s\n", name);
546
-        free(name);
547
-        name = NULL;
520
+    *fd = open(*name, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU);
521
+    if(*fd == -1) {
522
+	cli_errmsg("cli_gentempfd: Can't create temporary file %s: %s\n", *name, strerror(errno));
523
+	free(*name);
524
+	return CL_EIO;
548 525
     }
549 526
 
550
-    return(name);
551
-}
552
-
553
-char *cli_gentempstream(const char *dir, FILE **fs)
554
-{
555
-	char *name;
556
-	mode_t omask;
557
-
558
-
559
-    name = cli_gentempname(dir);
560
-    if(!name)
561
-	return NULL;
562
-
563
-    omask = umask(077);
564
-    if((*fs = fopen(name, "wb+")) == NULL) {
565
-	cli_dbgmsg("cli_gentempstream(): can't create temp file: %s\n", name);
566
-        free(name);
567
-        name = NULL;
568
-    }
569
-    umask(omask);
570
-
571
-    return name;
527
+    return CL_SUCCESS;
572 528
 }
573 529
 
574 530
 #ifdef	C_WINDOWS
... ...
@@ -591,13 +528,13 @@ cli_rmdirs(const char *name)
591 591
 
592 592
 
593 593
     if(stat(name, &statb) < 0) {
594
-	cli_warnmsg("Can't locate %s: %s\n", name, strerror(errno));
594
+	cli_warnmsg("cli_rmdirs: Can't locate %s: %s\n", name, strerror(errno));
595 595
 	return -1;
596 596
     }
597 597
 
598 598
     if(!S_ISDIR(statb.st_mode)) {
599 599
 	if(unlink(name) < 0) {
600
-	    cli_warnmsg("Can't remove %s: %s\n", name, strerror(errno));
600
+	    cli_warnmsg("cli_rmdirs: Can't remove %s: %s\n", name, strerror(errno));
601 601
 	    return -1;
602 602
 	}
603 603
 	return 0;
... ...
@@ -615,23 +552,23 @@ cli_rmdirs(const char *name)
615 615
 #else
616 616
     while((dent = readdir(dd)) != NULL) {
617 617
 #endif
618
-	    char *fname;
618
+	    char *path;
619 619
 
620 620
 	if(strcmp(dent->d_name, ".") == 0)
621 621
 	    continue;
622 622
 	if(strcmp(dent->d_name, "..") == 0)
623 623
 	    continue;
624 624
 
625
-	fname = cli_malloc(strlen(name) + strlen(dent->d_name) + 2);
625
+	path = cli_malloc(strlen(name) + strlen(dent->d_name) + 2);
626 626
 
627
-	if(fname == NULL) {
627
+	if(path == NULL) {
628 628
 	    closedir(dd);
629 629
 	    return -1;
630 630
 	}
631 631
 
632
-	sprintf(fname, "%s\\%s", name, dent->d_name);
633
-	rc = cli_rmdirs(fname);
634
-	free(fname);
632
+	sprintf(path, "%s\\%s", name, dent->d_name);
633
+	rc = cli_rmdirs(path);
634
+	free(path);
635 635
 	if(rc != 0)
636 636
 	    break;
637 637
     }
... ...
@@ -639,7 +576,7 @@ cli_rmdirs(const char *name)
639 639
     closedir(dd);
640 640
 
641 641
     if(rmdir(name) < 0) {
642
-	cli_errmsg("Can't remove temporary directory %s: %s\n", name, strerror(errno));
642
+	cli_errmsg("cli_rmdirs: Can't remove temporary directory %s: %s\n", name, strerror(errno));
643 643
 	return -1;
644 644
     }
645 645
 
... ...
@@ -657,8 +594,7 @@ int cli_rmdirs(const char *dirname)
657 657
 	} result;
658 658
 #endif
659 659
 	struct stat maind, statbuf;
660
-	char *fname;
661
-	int ret;
660
+	char *path;
662 661
 
663 662
 
664 663
     chmod(dirname, 0700);
... ...
@@ -666,7 +602,7 @@ int cli_rmdirs(const char *dirname)
666 666
 	while(stat(dirname, &maind) != -1) {
667 667
 	    if(!rmdir(dirname)) break;
668 668
 	    if(errno != ENOTEMPTY && errno != EEXIST && errno != EBADF) {
669
-		cli_errmsg("Can't remove temporary directory %s: %s\n", dirname, strerror(errno));
669
+		cli_errmsg("cli_rmdirs: Can't remove temporary directory %s: %s\n", dirname, strerror(errno));
670 670
 		closedir(dd);
671 671
 		return -1;
672 672
 	    }
... ...
@@ -683,50 +619,47 @@ int cli_rmdirs(const char *dirname)
683 683
 #endif
684 684
 		{
685 685
 		    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
686
-			fname = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
687
-			if(!fname) {
686
+			path = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
687
+			if(!path) {
688 688
 			    closedir(dd);
689 689
 			    return -1;
690 690
 			}
691 691
 
692 692
 #ifdef	C_WINDOWS
693
-			sprintf(fname, "%s\\%s", dirname, dent->d_name);
693
+			sprintf(path, "%s\\%s", dirname, dent->d_name);
694 694
 #else
695
-			sprintf(fname, "%s/%s", dirname, dent->d_name);
695
+			sprintf(path, "%s/%s", dirname, dent->d_name);
696 696
 #endif
697 697
 
698 698
 			/* stat the file */
699
-			if(lstat(fname, &statbuf) != -1) {
699
+			if(lstat(path, &statbuf) != -1) {
700 700
 			    if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
701
-				if(rmdir(fname) == -1) { /* can't be deleted */
701
+				if(rmdir(path) == -1) { /* can't be deleted */
702 702
 				    if(errno == EACCES) {
703
-					cli_errmsg("Can't remove some temporary directories due to access problem.\n");
703
+					cli_errmsg("cli_rmdirs: Can't remove some temporary directories due to access problem.\n");
704 704
 					closedir(dd);
705
-					free(fname);
705
+					free(path);
706 706
 					return -1;
707 707
 				    }
708
-				    ret = cli_rmdirs(fname);
709
-				    if(ret) {
710
-					cli_warnmsg("Can't remove directory %s\n", fname);
711
-					free(fname);
708
+				    if(cli_rmdirs(path)) {
709
+					cli_warnmsg("cli_rmdirs: Can't remove nested directory %s\n", path);
710
+					free(path);
712 711
 					closedir(dd);
713 712
 					return -1;
714 713
 				    }
715 714
 				}
716 715
 			    } else
717
-				if(unlink(fname) < 0) {
718
-				    cli_warnmsg("Couldn't remove %s: %s\n", fname, strerror(errno));
719
-				    free(fname);
716
+				if(unlink(path) < 0) {
717
+				    cli_warnmsg("cli_rmdirs: Couldn't remove %s: %s\n", path, strerror(errno));
718
+				    free(path);
720 719
 				    closedir(dd);
721 720
 				    return -1;
722 721
 				}
723 722
 			}
724
-
725
-			free(fname);
723
+			free(path);
726 724
 		    }
727 725
 		}
728 726
 	    }
729
-
730 727
 	    rewinddir(dd);
731 728
 	}
732 729
 
... ...
@@ -30,6 +30,8 @@
30 30
 #include "clamav.h"
31 31
 #include "dconf.h"
32 32
 
33
+extern uint8_t cli_debug_flag, cli_leavetemps_flag;
34
+
33 35
 /*
34 36
  * CLI_ISCONTAINED(buf1, size1, buf2, size2) checks if buf2 is contained
35 37
  * within buf1.
... ...
@@ -191,9 +193,7 @@ char *cli_md5file(const char *filename);
191 191
 int cli_readn(int fd, void *buff, unsigned int count);
192 192
 int cli_writen(int fd, const void *buff, unsigned int count);
193 193
 char *cli_gentemp(const char *dir);
194
-char *cli_gentempdir(const char *dir);
195
-char *cli_gentempdesc(const char *dir, int *fd);
196
-char *cli_gentempstream(const char *dir, FILE **fs);
194
+int cli_gentempfd(const char *dir, char **name, int *fd);
197 195
 unsigned int cli_rndnum(unsigned int max);
198 196
 int cli_filecopy(const char *src, const char *dest);
199 197
 bitset_t *cli_bitset_init(void);
... ...
@@ -80,7 +80,6 @@
80 80
 #define PEALIGN(o,a) (((a))?(((o)/(a))*(a)):(o))
81 81
 #define PESALIGN(o,a) (((a))?(((o)/(a)+((o)%(a)!=0))*(a)):(o))
82 82
 
83
-extern short cli_leavetemps_flag;
84 83
 
85 84
 struct offset_list {
86 85
     uint32_t offset;
... ...
@@ -738,7 +738,7 @@ static int cli_loadhdb(FILE *fd, struct cl_engine **engine, unsigned int *signo,
738 738
 	} else {
739 739
 	    if(!(*engine)->md5_hlist) {
740 740
 		cli_dbgmsg("cli_loadhdb: Initializing MD5 list structure\n");
741
-		(*engine)->md5_hlist = (struct cli_md5_node **) cli_calloc(256, sizeof(struct cli_md5_node *));
741
+		(*engine)->md5_hlist = cli_calloc(256, sizeof(struct cli_md5_node *));
742 742
 		if(!(*engine)->md5_hlist) {
743 743
 		    free(new->virname);
744 744
 		    free(new->md5);
... ...
@@ -124,8 +124,6 @@ static const short int hextable[256] = {
124 124
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
125 125
 };
126 126
 
127
-extern int short cli_leavetemps_flag;
128
-
129 127
 static void init_rtf_state(struct rtf_state* state)
130 128
 {
131 129
 	*state = base_state;
... ...
@@ -267,6 +265,7 @@ static int rtf_object_process(struct rtf_state* state, const unsigned char* inpu
267 267
 	const unsigned char* out_data;
268 268
 	size_t out_cnt = 0;
269 269
 	size_t i;
270
+	int ret;
270 271
 
271 272
 	if(!data || !len)
272 273
 		return 0;
... ...
@@ -394,9 +393,8 @@ static int rtf_object_process(struct rtf_state* state, const unsigned char* inpu
394 394
 							    out_data += i;
395 395
 							    data->bread=0;
396 396
 							    cli_dbgmsg("Dumping rtf embedded object of size:%ld\n",data->desc_len);
397
-					    		    data->name = cli_gentempdesc(data->tmpdir, &data->fd);
398
-							    if(!data->name || data->fd < 0)
399
-								    return CL_ETMPFILE;
397
+							    if((ret = cli_gentempfd(data->tmpdir, &data->name, &data->fd)))
398
+								    return ret;
400 399
 							    data->internal_state = DUMP_DATA;
401 400
 	    						    cli_dbgmsg("RTF: next state: DUMP_DATA\n");
402 401
 						    }
... ...
@@ -49,7 +49,6 @@
49 49
 #define	O_BINARY	0
50 50
 #endif
51 51
 
52
-extern short cli_leavetemps_flag;
53 52
 
54 53
 #define DCONF_ARCH  ctx->dconf->archive
55 54
 #define DCONF_DOC   ctx->dconf->doc
... ...
@@ -103,6 +102,7 @@ extern short cli_leavetemps_flag;
103 103
 
104 104
 #define MAX_MAIL_RECURSION  15
105 105
 
106
+
106 107
 int cli_scannulsft(int desc, cli_ctx *ctx, off_t offset); /* FIXME */
107 108
 static int cli_scanfile(const char *filename, cli_ctx *ctx);
108 109
 
... ...
@@ -428,9 +428,8 @@ static int cli_scanzip(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
428 428
 	zip_dir *zdir;
429 429
 	zip_dirent zdirent;
430 430
 	zip_file *zfp;
431
-	FILE *tmp = NULL;
432 431
 	char *tmpname = NULL, *buff;
433
-	int fd, bytes, ret = CL_CLEAN;
432
+	int fd = -1, bytes, ret = CL_CLEAN;
434 433
 	unsigned long int size = 0;
435 434
 	unsigned int files = 0, encrypted, bfcnt;
436 435
 	struct stat source;
... ...
@@ -610,17 +609,13 @@ static int cli_scanzip(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
610 610
 	while(1) {
611 611
 	    fail = 0;
612 612
 
613
-	    /* generate temporary file and get its descriptor */
614
-	    if((tmpname = cli_gentempstream(NULL, &tmp)) == NULL) {
615
-		cli_dbgmsg("Zip: Can't generate tmpfile().\n");
616
-		ret = CL_ETMPFILE;
613
+	    if((ret = cli_gentempfd(NULL, &tmpname, &fd)))
617 614
 		break;
618
-	    }
619 615
 
620 616
 	    size = 0;
621 617
 	    while((bytes = zip_file_read(zfp, buff, FILEBUFF)) > 0) {
622 618
 		size += bytes;
623
-		if(fwrite(buff, 1, bytes, tmp) != (size_t) bytes) {
619
+		if(cli_writen(fd, buff, bytes) != bytes) {
624 620
 		    cli_dbgmsg("Zip: Can't write to file.\n");
625 621
 		    ret = CL_EIO;
626 622
 		    break;
... ...
@@ -643,14 +638,11 @@ static int cli_scanzip(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
643 643
 	    }
644 644
 
645 645
 	    if(!fail) {
646
-		if(fflush(tmp) != 0) {
647
-		    cli_dbgmsg("Zip: fflush() failed\n");
646
+		if(fsync(fd) == -1) {
647
+		    cli_dbgmsg("Zip: fsync() failed\n");
648 648
 		    ret = CL_EFSYNC;
649 649
 		    break;
650 650
 		}
651
-
652
-		fd = fileno(tmp);
653
-
654 651
 		lseek(fd, 0, SEEK_SET);
655 652
 
656 653
 		if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS ) {
... ...
@@ -658,16 +650,13 @@ static int cli_scanzip(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
658 658
 		    ret = CL_VIRUS;
659 659
 		    break;
660 660
 		}
661
-
662 661
 	    }
663 662
 
664
-	    if(tmp) {
665
-		fclose(tmp);
666
-		if(!cli_leavetemps_flag)
667
-		    unlink(tmpname);
668
-		free(tmpname);
669
-		tmp = NULL;
670
-	    }
663
+	    close(fd);
664
+	    if(!cli_leavetemps_flag)
665
+		unlink(tmpname);
666
+	    free(tmpname);
667
+	    fd = -1;
671 668
 
672 669
 	    if(zfp->bf[bfcnt] == -1)
673 670
 		break;
... ...
@@ -689,12 +678,11 @@ static int cli_scanzip(int desc, cli_ctx *ctx, off_t sfx_offset, uint32_t *sfx_c
689 689
     }
690 690
 
691 691
     zip_dir_close(zdir);
692
-    if(tmp) {
693
-	fclose(tmp);
692
+    if(fd != -1) {
693
+	close(fd);
694 694
 	if(!cli_leavetemps_flag)
695 695
 	    unlink(tmpname);
696 696
 	free(tmpname);
697
-	tmp = NULL;
698 697
     }
699 698
 
700 699
     free(buff);
... ...
@@ -706,7 +694,6 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
706 706
 	int fd, bytes, ret = CL_CLEAN;
707 707
 	unsigned long int size = 0;
708 708
 	char *buff;
709
-	FILE *tmp = NULL;
710 709
 	char *tmpname;
711 710
 	gzFile gd;
712 711
 
... ...
@@ -718,17 +705,16 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
718 718
 	return CL_EGZIP;
719 719
     }
720 720
 
721
-    if((tmpname = cli_gentempstream(NULL, &tmp)) == NULL) {
721
+    if((ret = cli_gentempfd(NULL, &tmpname, &fd))) {
722 722
 	cli_dbgmsg("GZip: Can't generate temporary file.\n");
723 723
 	gzclose(gd);
724
-	return CL_ETMPFILE;
724
+	return ret;
725 725
     }
726
-    fd = fileno(tmp);
727 726
 
728 727
     if(!(buff = (char *) cli_malloc(FILEBUFF))) {
729 728
 	cli_dbgmsg("GZip: Unable to malloc %u bytes.\n", FILEBUFF);
730 729
 	gzclose(gd);
731
-	fclose(tmp);
730
+	close(fd);
732 731
 	if(!cli_leavetemps_flag)
733 732
 	    unlink(tmpname);
734 733
 	free(tmpname);	
... ...
@@ -750,7 +736,7 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
750 750
 
751 751
 	if(cli_writen(fd, buff, bytes) != bytes) {
752 752
 	    cli_dbgmsg("GZip: Can't write to file.\n");
753
-	    fclose(tmp);
753
+	    close(fd);
754 754
 	    if(!cli_leavetemps_flag)
755 755
 		unlink(tmpname);
756 756
 	    free(tmpname);	
... ...
@@ -764,7 +750,7 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
764 764
     gzclose(gd);
765 765
 
766 766
     if(ret == CL_VIRUS) {
767
-	fclose(tmp);
767
+	close(fd);
768 768
 	if(!cli_leavetemps_flag)
769 769
 	    unlink(tmpname);
770 770
 	free(tmpname);	
... ...
@@ -773,7 +759,7 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
773 773
 
774 774
     if(fsync(fd) == -1) {
775 775
 	cli_dbgmsg("GZip: Can't synchronise descriptor %d\n", fd);
776
-	fclose(tmp);
776
+	close(fd);
777 777
 	if(!cli_leavetemps_flag)
778 778
 	    unlink(tmpname);
779 779
 	free(tmpname);	
... ...
@@ -783,13 +769,13 @@ static int cli_scangzip(int desc, cli_ctx *ctx)
783 783
     lseek(fd, 0, SEEK_SET);
784 784
     if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS ) {
785 785
 	cli_dbgmsg("GZip: Infected with %s\n", *ctx->virname);
786
-	fclose(tmp);
786
+	close(fd);
787 787
 	if(!cli_leavetemps_flag)
788 788
 	    unlink(tmpname);
789 789
 	free(tmpname);	
790 790
 	return CL_VIRUS;
791 791
     }
792
-    fclose(tmp);
792
+    close(fd);
793 793
     if(!cli_leavetemps_flag)
794 794
 	unlink(tmpname);
795 795
     free(tmpname);	
... ...
@@ -812,7 +798,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
812 812
 	short memlim = 0;
813 813
 	unsigned long int size = 0;
814 814
 	char *buff;
815
-	FILE *fs, *tmp = NULL;
815
+	FILE *fs;
816 816
 	char *tmpname;
817 817
 	BZFILE *bfd;
818 818
 
... ...
@@ -832,17 +818,16 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
832 832
 	return CL_EBZIP;
833 833
     }
834 834
 
835
-    if((tmpname = cli_gentempstream(NULL, &tmp)) == NULL) {
835
+    if((ret = cli_gentempfd(NULL, &tmpname, &fd))) {
836 836
 	cli_dbgmsg("Bzip: Can't generate temporary file.\n");
837 837
 	BZ2_bzReadClose(&bzerror, bfd);
838 838
 	fclose(fs);
839
-	return CL_ETMPFILE;
839
+	return ret;
840 840
     }
841
-    fd = fileno(tmp);
842 841
 
843 842
     if(!(buff = (char *) cli_malloc(FILEBUFF))) {
844 843
 	cli_dbgmsg("Bzip: Unable to malloc %u bytes.\n", FILEBUFF);
845
-	fclose(tmp);
844
+	close(fd);
846 845
 	if(!cli_leavetemps_flag)
847 846
 	    unlink(tmpname);
848 847
 	free(tmpname);	
... ...
@@ -867,7 +852,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
867 867
 	if(cli_writen(fd, buff, bytes) != bytes) {
868 868
 	    cli_dbgmsg("Bzip: Can't write to file.\n");
869 869
 	    BZ2_bzReadClose(&bzerror, bfd);
870
-	    fclose(tmp);
870
+	    close(fd);
871 871
 	    if(!cli_leavetemps_flag)
872 872
 		unlink(tmpname);
873 873
 	    free(tmpname);	
... ...
@@ -881,7 +866,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
881 881
     BZ2_bzReadClose(&bzerror, bfd);
882 882
 
883 883
     if(ret == CL_VIRUS) {
884
-	fclose(tmp);
884
+	close(fd);
885 885
 	if(!cli_leavetemps_flag)
886 886
 	    unlink(tmpname);
887 887
 	free(tmpname);	
... ...
@@ -891,7 +876,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
891 891
 
892 892
     if(fsync(fd) == -1) {
893 893
 	cli_dbgmsg("Bzip: Synchronisation failed for descriptor %d\n", fd);
894
-	fclose(tmp);
894
+	close(fd);
895 895
 	if(!cli_leavetemps_flag)
896 896
 	    unlink(tmpname);
897 897
 	free(tmpname);	
... ...
@@ -903,7 +888,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
903 903
     if((ret = cli_magic_scandesc(fd, ctx)) == CL_VIRUS ) {
904 904
 	cli_dbgmsg("Bzip: Infected with %s\n", *ctx->virname);
905 905
     }
906
-    fclose(tmp);
906
+    close(fd);
907 907
     if(!cli_leavetemps_flag)
908 908
 	unlink(tmpname);
909 909
     free(tmpname);	
... ...
@@ -913,6 +898,7 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
913 913
 }
914 914
 #endif
915 915
 
916
+/*
916 917
 static int cli_scanszdd(int desc, cli_ctx *ctx)
917 918
 {
918 919
 	int fd, ret = CL_CLEAN, dcpy;
... ...
@@ -976,6 +962,7 @@ static int cli_scanszdd(int desc, cli_ctx *ctx)
976 976
     free(tmpname);	
977 977
     return ret;
978 978
 }
979
+*/
979 980
 
980 981
 static int cli_scanmscab(int desc, cli_ctx *ctx, off_t sfx_offset)
981 982
 {
... ...
@@ -2088,12 +2075,12 @@ int cli_magic_scandesc(int desc, cli_ctx *ctx)
2088 2088
 	    if(SCAN_ARCHIVE)
2089 2089
 		ret = cli_scannulsft(desc, ctx, 0);
2090 2090
 	    break;
2091
-
2091
+/*
2092 2092
 	case CL_TYPE_MSSZDD:
2093 2093
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_SZDD))
2094 2094
 		ret = cli_scanszdd(desc, ctx);
2095 2095
 	    break;
2096
-
2096
+*/
2097 2097
 	case CL_TYPE_MSCAB:
2098 2098
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_CAB))
2099 2099
 		ret = cli_scanmscab(desc, ctx, 0);
... ...
@@ -51,8 +51,6 @@
51 51
 #define EC32(x) le32_to_host(x) /* Convert little endian to host */
52 52
 #define EC16(x) le16_to_host(x) /* Convert little endian to host */
53 53
 
54
-extern short cli_leavetemps_flag;
55
-
56 54
 static const char *langcodes[] = {
57 55
     "",   "EN", "FR", "GE", "SP", "IT", "SW", "DA", "NO", "FI", "AM",
58 56
     "SF", "SG", "PO", "TU", "IC", "RU", "HU", "DU", "BL", "AU", "BG",
... ...
@@ -541,19 +539,19 @@ int cli_scansis(int desc, cli_ctx *ctx)
541 541
     cli_dbgmsg("SIS: Number of files: %d\n", nfiles);
542 542
     cli_dbgmsg("SIS: Offset of files records: %d\n", EC32(file_hdr.pfiles));
543 543
 
544
-    if(!(dir = cli_gentempdir(NULL))) {
545
-	cli_errmsg("SIS: Can't generate temporary directory\n");
546
-	munmap(mfile, length);
547
-	return CL_ETMPDIR;
548
-    }
549
-
550 544
     if((frecord = EC32(file_hdr.pfiles)) >= length) {
551 545
 	cli_errmsg("SIS: Broken file structure (frecord)\n");
552 546
 	munmap(mfile, length);
553
-	free(dir);
554 547
 	return CL_EFORMAT;
555 548
     }
556 549
 
550
+    dir = cli_gentemp(NULL);
551
+    if(!dir || mkdir(dir, 0700) == -1) {
552
+	cli_errmsg("SIS: Can't create temporary directory %s\n", dir ? dir : "");
553
+	munmap(mfile, length);
554
+	return CL_ETMPDIR;
555
+    }
556
+
557 557
     for(i = 0; i < nfiles; i++) {
558 558
 
559 559
 	cli_dbgmsg("SIS: -----\n");
... ...
@@ -60,7 +60,6 @@ static	int	tnef_header(FILE *fp, uint8_t *part, uint16_t *type, uint16_t *tag, i
60 60
 #define host16(v)	le16_to_host(v)
61 61
 #define host32(v)	le32_to_host(v)
62 62
 
63
-extern	short	cli_debug_flag;
64 63
 
65 64
 int
66 65
 cli_tnef(const char *dir, int desc)