Browse code

Rar support fix

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

Tomasz Kojm authored on 2004/03/11 17:33:45
Showing 8 changed files
... ...
@@ -1,3 +1,9 @@
1
+Thu Mar 11 00:16:11 CET 2004 (tk)
2
+---------------------------------
3
+  * libclamav: rar support: due to a bug only first file in archive was
4
+	       scanned. Fixed and improved by Dirk Mueller <dmuell*gmx.net>.
5
+  * freshclam, clamscan: fixed logfile permissions (Dirk Mueller)
6
+
1 7
 Wed Mar 10 22:14:45 GMT 2004 (njh)
2 8
 ----------------------------------
3 9
   * libclamav/mbox.c:	Fixed segfault which happens when a UNIX mailbox
... ...
@@ -102,7 +102,7 @@ int logg(const char *str, ...)
102 102
 	pthread_mutex_lock(&logg_mutex);
103 103
 
104 104
 	if(!log_fd) {
105
-	    old_umask = umask(0036);
105
+	    old_umask = umask(0037);
106 106
 	    if((log_fd = fopen(logfile, "a")) == NULL) {
107 107
 		umask(old_umask);
108 108
 		pthread_mutex_unlock(&logg_mutex);
... ...
@@ -1,6 +1,5 @@
1 1
 /*
2
- *  Copyright (C) 1999-2003 Tomasz Kojm <zolw@konarski.edu.pl>
3
- *			    Magnus Ekdahl <magnus@debian.org>
2
+ *  Copyright (C) 1999 - 2004 Tomasz Kojm <tkojm@clamav.net>
4 3
  *
5 4
  *  This program is free software; you can redistribute it and/or modify
6 5
  *  it under the terms of the GNU General Public License as published by
... ...
@@ -127,6 +126,8 @@ int logg(const char *str, ...)
127 127
 {
128 128
 	va_list args;
129 129
 	static FILE *fd = NULL;
130
+	mode_t old_umask;
131
+
130 132
 
131 133
     if(logfile) {
132 134
 
... ...
@@ -136,10 +137,13 @@ int logg(const char *str, ...)
136 136
 	    fd = NULL;
137 137
 	}
138 138
 	if(!fd) {
139
+            old_umask = umask(0037);
139 140
 	    if((fd = fopen(logfile, "a")) == NULL) {
141
+                umask(old_umask);
140 142
 		mprintf("!LOGGER: Can't open %s for writing: %s.\n", logfile, strerror(errno));
141 143
 		return 1;
142 144
 	    }
145
+	    umask(old_umask);
143 146
 	}
144 147
 	if (str == NULL) {
145 148
 	    return 0;
... ...
@@ -172,7 +176,7 @@ void *mmalloc(size_t size)
172 172
     alloc = malloc(size);
173 173
 
174 174
     if(!alloc) {
175
-	printf("CRITICAL: Can't allocate memory (%ld bytes).\n", size);
175
+	printf("CRITICAL: Can't allocate memory (%ld bytes).\n", (long int) size);
176 176
 	exit(71);
177 177
 	return NULL;
178 178
     } else return alloc;
... ...
@@ -185,7 +189,7 @@ void *mcalloc(size_t nmemb, size_t size)
185 185
     alloc = calloc(nmemb, size);
186 186
 
187 187
     if(!alloc) {
188
-	printf("CRITICAL: Can't allocate memory (%ld bytes).\n", nmemb * size);
188
+	printf("CRITICAL: Can't allocate memory (%ld bytes).\n", (long int) nmemb * size);
189 189
 	exit(70);
190 190
 	return NULL;
191 191
     } else return alloc;
... ...
@@ -116,14 +116,14 @@ struct cl_cvd {
116 116
 /* file scanning */
117 117
 extern int cl_scanbuff(const char *buffer, unsigned int length, char **virname, const struct cl_node *root);
118 118
 
119
-extern int cl_scandesc(int desc, char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options);
119
+extern int cl_scandesc(int desc, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options);
120 120
 
121
-extern int cl_scanfile(const char *filename, char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options);
121
+extern int cl_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options);
122 122
 
123 123
 /* database loading */
124 124
 extern int cl_loaddb(const char *filename, struct cl_node **root, int *virnum);
125 125
 extern int cl_loaddbdir(const char *dirname, struct cl_node **root, int *virnum);
126
-extern char *cl_retdbdir(void);
126
+extern const char *cl_retdbdir(void);
127 127
 extern int cl_retflevel(void);
128 128
 
129 129
 extern struct cl_cvd *cl_cvdhead(const char *file);
... ...
@@ -144,8 +144,8 @@ extern int cl_buildtrie(struct cl_node *root);
144 144
 
145 145
 extern void cl_freetrie(struct cl_node *root);
146 146
 
147
-extern char *cl_strerror(int clerror);
148
-extern char *cl_perror(int clerror); /* deprecated */
147
+extern const char *cl_strerror(int clerror);
148
+extern const char *cl_perror(int clerror); /* deprecated */
149 149
 
150 150
 extern char *cl_md5buff(const char *buffer, unsigned int length);
151 151
 
... ...
@@ -90,7 +90,7 @@ int cl_retflevel(void)
90 90
     return CL_FLEVEL;
91 91
 }
92 92
 
93
-char *cl_strerror(int clerror)
93
+const char *cl_strerror(int clerror)
94 94
 {
95 95
     switch(clerror) {
96 96
 	case CL_CLEAN:
... ...
@@ -142,7 +142,7 @@ char *cl_strerror(int clerror)
142 142
     }
143 143
 }
144 144
 
145
-char *cl_perror(int clerror)
145
+const char *cl_perror(int clerror)
146 146
 {
147 147
     return cl_strerror(clerror);
148 148
 }
... ...
@@ -291,7 +291,8 @@ unsigned int cl_rndnum(unsigned int max)
291 291
 /* it uses MD5 to avoid potential races in tmp */
292 292
 char *cl_gentemp(const char *dir)
293 293
 {
294
-	char *name, *mdir, *tmp;
294
+	char *name, *tmp;
295
+        const char *mdir;
295 296
 	unsigned char salt[32];
296 297
 	int cnt=0, i;
297 298
 	struct stat foo;
... ...
@@ -312,7 +313,7 @@ char *cl_gentemp(const char *dir)
312 312
 	for(i = 0; i < 32; i++)
313 313
 	    salt[i] = cl_rndnum(255);
314 314
 
315
-	tmp = cl_md5buff(salt, 32);
315
+	tmp = cl_md5buff(( char* ) salt, 32);
316 316
 	strncat(name, tmp, 16);
317 317
 	free(tmp);
318 318
     } while(stat(name, &foo) != -1);
... ...
@@ -38,7 +38,7 @@
38 38
 #include "str.h"
39 39
 #include "defaults.h"
40 40
 
41
-int cli_parse_add(struct cl_node *root, const char *virname, const char *hexstr, int sigid, int parts, int partno)
41
+static int cli_parse_add(struct cl_node *root, const char *virname, const char *hexstr, int sigid, int parts, int partno)
42 42
 {
43 43
 	struct cli_patt *new;
44 44
 	const char *pt;
... ...
@@ -170,7 +170,7 @@ int cl_loaddb(const char *filename, struct cl_node **root, int *virnum)
170 170
 	    (*root)->partsigs++;
171 171
 	    sigid++;
172 172
 	    parts = 0;
173
-	    for(i = 0; i < strlen(pt); i++)
173
+	    for(i = 0; i < (int) strlen(pt); i++)
174 174
 		if(pt[i] == '*')
175 175
 		    parts++;
176 176
 
... ...
@@ -218,7 +218,7 @@ int cl_loaddb(const char *filename, struct cl_node **root, int *virnum)
218 218
     return 0;
219 219
 }
220 220
 
221
-char *cl_retdbdir(void)
221
+const char *cl_retdbdir(void)
222 222
 {
223 223
     return DATADIR;
224 224
 }
... ...
@@ -70,9 +70,9 @@ typedef enum {
70 70
 
71 71
 struct cl_magic_s {
72 72
     int offset;
73
-    char *magic;
73
+    const char *magic;
74 74
     size_t length;
75
-    char *descr;
75
+    const char *descr;
76 76
     cl_file_t type;
77 77
 };
78 78
 
... ...
@@ -97,7 +97,7 @@ static const struct cl_magic_s cl_magic[] = {
97 97
     {-1, NULL,              0, NULL,              CL_UNKNOWN_TYPE}
98 98
 };
99 99
 
100
-cl_file_t cl_filetype(const char *buf, size_t buflen)
100
+static cl_file_t cl_filetype(const char *buf, size_t buflen)
101 101
 {
102 102
 	int i;
103 103
 
... ...
@@ -113,9 +113,9 @@ cl_file_t cl_filetype(const char *buf, size_t buflen)
113 113
     return CL_UNKNOWN_TYPE;
114 114
 }
115 115
 
116
-int cli_magic_scandesc(int desc, char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev);
116
+static int cli_magic_scandesc(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev);
117 117
 
118
-int cli_scandesc(int desc, char **virname, long int *scanned, const struct 
118
+static int cli_scandesc(int desc, const char **virname, long int *scanned, const struct 
119 119
 cl_node *root)
120 120
 {
121 121
  	char *buffer, *buff, *endbl, *pt;
... ...
@@ -163,18 +163,19 @@ cl_node *root)
163 163
 }
164 164
 
165 165
 #ifdef CL_THREAD_SAFE
166
-void cli_unlock_mutex(void *mtx)
166
+static void cli_unlock_mutex(void *mtx)
167 167
 {
168 168
     cli_dbgmsg("Pthread cancelled. Unlocking mutex.\n");
169 169
     pthread_mutex_unlock(mtx);
170 170
 }
171 171
 #endif
172 172
 
173
-int cli_scanrar(int desc, char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
173
+static int cli_scanrar(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
174 174
 {
175 175
 	FILE *tmp = NULL;
176 176
 	int files = 0, fd, ret = CL_CLEAN;
177 177
 	ArchiveList_struct *rarlist = NULL;
178
+	ArchiveList_struct *rarlist_head = NULL;
178 179
 	char *rar_data_ptr;
179 180
 	unsigned long rar_data_size;
180 181
 
... ...
@@ -195,6 +196,8 @@ int cli_scanrar(int desc, char **virname, long int *scanned, const struct cl_nod
195 195
 	return CL_ERAR;
196 196
     }
197 197
 
198
+    rarlist_head = rarlist;
199
+
198 200
     while(rarlist) {
199 201
 
200 202
 	if(limits) {
... ...
@@ -213,6 +216,12 @@ int cli_scanrar(int desc, char **virname, long int *scanned, const struct cl_nod
213 213
 	    }
214 214
 	}
215 215
 
216
+        if(!!( rarlist->item.FileAttr & RAR_FENTRY_ATTR_DIRECTORY)) {
217
+            rarlist = rarlist->next;
218
+            files++;
219
+            continue;
220
+        }
221
+
216 222
 	if((tmp = tmpfile()) == NULL) {
217 223
 	    cli_dbgmsg("RAR -> Can't generate tmpfile().\n");
218 224
 #ifdef CL_THREAD_SAFE
... ...
@@ -223,9 +232,9 @@ int cli_scanrar(int desc, char **virname, long int *scanned, const struct cl_nod
223 223
 	}
224 224
 	fd = fileno(tmp);
225 225
 
226
-	if(urarlib_get(&rar_data_ptr, &rar_data_size, rarlist->item.Name, desc, "clam")) {
226
+	if( urarlib_get(&rar_data_ptr, &rar_data_size, rarlist->item.Name, desc, "clam")) {
227 227
 	    cli_dbgmsg("RAR -> Extracted: %s, size: %d\n", rarlist->item.Name, rar_data_size);
228
-	    if(fwrite(rar_data_ptr, rar_data_size, 1, tmp) != 1) {
228
+	    if(fwrite(rar_data_ptr, 1, rar_data_size, tmp) != rar_data_size) {
229 229
 		cli_dbgmsg("RAR -> Can't write() file.\n");
230 230
 		fclose(tmp);
231 231
 		tmp = NULL;
... ...
@@ -244,7 +253,7 @@ int cli_scanrar(int desc, char **virname, long int *scanned, const struct cl_nod
244 244
 	    if(fflush(tmp) != 0) {
245 245
 		cli_dbgmsg("fflush() failed: %s\n", strerror(errno));
246 246
 		fclose(tmp);
247
-		urarlib_freelist(rarlist);
247
+		urarlib_freelist(rarlist_head);
248 248
 #ifdef CL_THREAD_SAFE
249 249
 		pthread_mutex_unlock(&cli_scanrar_mutex);
250 250
 		cli_scanrar_inuse = 0;
... ...
@@ -253,9 +262,8 @@ int cli_scanrar(int desc, char **virname, long int *scanned, const struct cl_nod
253 253
 	    }
254 254
 
255 255
 	    lseek(fd, 0, SEEK_SET);
256
-	    if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, reclev)) == CL_CLEAN ) {
257
-		if(ret == CL_VIRUS)
258
-		    cli_dbgmsg("RAR -> Found %s virus.\n", *virname);
256
+	    if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, reclev)) == CL_VIRUS ) {
257
+		cli_dbgmsg("RAR -> Found %s virus.\n", *virname);
259 258
 
260 259
 		fclose(tmp);
261 260
 		urarlib_freelist(rarlist);
... ...
@@ -263,7 +271,7 @@ int cli_scanrar(int desc, char **virname, long int *scanned, const struct cl_nod
263 263
 		pthread_mutex_unlock(&cli_scanrar_mutex);
264 264
 		cli_scanrar_inuse = 0;
265 265
 #endif
266
-		return ret;
266
+  		return ret;
267 267
 	    }
268 268
 
269 269
 	} else {
... ...
@@ -279,7 +287,7 @@ int cli_scanrar(int desc, char **virname, long int *scanned, const struct cl_nod
279 279
 	files++;
280 280
     }
281 281
 
282
-    urarlib_freelist(rarlist);
282
+    urarlib_freelist(rarlist_head);
283 283
 #ifdef CL_THREAD_SAFE
284 284
     pthread_mutex_unlock(&cli_scanrar_mutex);
285 285
     cli_scanrar_inuse = 0;
... ...
@@ -289,7 +297,7 @@ int cli_scanrar(int desc, char **virname, long int *scanned, const struct cl_nod
289 289
 }
290 290
 
291 291
 #ifdef HAVE_ZLIB_H
292
-int cli_scanzip(int desc, char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
292
+static int cli_scanzip(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
293 293
 {
294 294
 	ZZIP_DIR *zdir;
295 295
 	ZZIP_DIRENT zdirent;
... ...
@@ -441,7 +449,7 @@ int cli_scanzip(int desc, char **virname, long int *scanned, const struct cl_nod
441 441
     return ret;
442 442
 }
443 443
 
444
-int cli_scangzip(int desc, char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
444
+static int cli_scangzip(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
445 445
 {
446 446
 	int fd, bytes, ret = CL_CLEAN;
447 447
 	long int size = 0;
... ...
@@ -475,7 +483,7 @@ int cli_scangzip(int desc, char **virname, long int *scanned, const struct cl_no
475 475
 
476 476
 	if(limits)
477 477
 	    if(limits->maxfilesize && (size + FILEBUFF > limits->maxfilesize)) {
478
-		cli_dbgmsg("Gzip->desc(%d): Size exceeded (stopped at %d, max: %d)\n", desc, size, limits->maxfilesize);
478
+		cli_dbgmsg("Gzip->desc(%d): Size exceeded (stopped at %ld, max: %ld)\n", desc, size, limits->maxfilesize);
479 479
 		/* ret = CL_EMAXSIZE; */
480 480
 		break;
481 481
 	    }
... ...
@@ -517,7 +525,7 @@ int cli_scangzip(int desc, char **virname, long int *scanned, const struct cl_no
517 517
 #define BZ2_bzRead bzRead
518 518
 #endif
519 519
 
520
-int cli_scanbzip(int desc, char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
520
+static int cli_scanbzip(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
521 521
 {
522 522
 	int fd, bytes, ret = CL_CLEAN, bzerror = 0;
523 523
 	short memlim = 0;
... ...
@@ -563,7 +571,7 @@ int cli_scanbzip(int desc, char **virname, long int *scanned, const struct cl_no
563 563
 
564 564
 	if(limits)
565 565
 	    if(limits->maxfilesize && (size + FILEBUFF > limits->maxfilesize)) {
566
-		cli_dbgmsg("Bzip2->desc(%d): Size exceeded (stopped at %d, max: %d)\n", desc, size, limits->maxfilesize);
566
+		cli_dbgmsg("Bzip2->desc(%d): Size exceeded (stopped at %ld, max: %ld)\n", desc, size, limits->maxfilesize);
567 567
 		/* ret = CL_EMAXSIZE; */
568 568
 		break;
569 569
 	    }
... ...
@@ -598,7 +606,7 @@ int cli_scanbzip(int desc, char **virname, long int *scanned, const struct cl_no
598 598
 }
599 599
 #endif
600 600
 
601
-int cli_scanole2(int desc, char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
601
+static int cli_scanole2(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
602 602
 {
603 603
 	const char *tmpdir;
604 604
 	char *dir, *fullname;
... ...
@@ -674,7 +682,7 @@ int cli_scanole2(int desc, char **virname, long int *scanned, const struct cl_no
674 674
 	free(dir);
675 675
 	return ret;
676 676
 }
677
-int cli_scandir(char *dirname, char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
677
+static int cli_scandir(char *dirname, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
678 678
 {
679 679
 	DIR *dd;
680 680
 	struct dirent *dent;
... ...
@@ -716,7 +724,7 @@ int cli_scandir(char *dirname, char **virname, long int *scanned, const struct c
716 716
     return 0;
717 717
 }
718 718
 
719
-int cli_scanmail(int desc, char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
719
+static int cli_scanmail(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
720 720
 {
721 721
 	const char *tmpdir;
722 722
 	char *dir;
... ...
@@ -758,7 +766,7 @@ int cli_scanmail(int desc, char **virname, long int *scanned, const struct cl_no
758 758
 	return ret;
759 759
 }
760 760
 
761
-int cli_magic_scandesc(int desc, char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
761
+static int cli_magic_scandesc(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, int *reclev)
762 762
 {
763 763
 	char magic[MAGIC_BUFFER_SIZE+1];
764 764
 	int ret = CL_CLEAN;
... ...
@@ -816,13 +824,13 @@ int cli_magic_scandesc(int desc, char **virname, long int *scanned, const struct
816 816
 		}
817 817
 		break;
818 818
 
819
-#ifdef HAVE_BZLIB_H
820 819
 	    case CL_BZFILE:
820
+#ifdef HAVE_BZLIB_H
821 821
 		if(SCAN_ARCHIVE) {
822 822
 		    ret = cli_scanbzip(desc, virname, scanned, root, limits, options, reclev);
823 823
 		}
824
-		break;
825 824
 #endif
825
+		break;
826 826
 
827 827
 	    case CL_MAILFILE:
828 828
 		if (SCAN_MAIL) {
... ...
@@ -834,6 +842,7 @@ int cli_magic_scandesc(int desc, char **virname, long int *scanned, const struct
834 834
 		if(SCAN_OLE2) {
835 835
 		    ret = cli_scanole2(desc, virname, scanned, root, limits, options, reclev);
836 836
 		}
837
+            case CL_UNKNOWN_TYPE:
837 838
 		break;
838 839
 	}
839 840
 
... ...
@@ -851,14 +860,14 @@ int cli_magic_scandesc(int desc, char **virname, long int *scanned, const struct
851 851
     return ret;
852 852
 }
853 853
 
854
-int cl_scandesc(int desc, char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options)
854
+int cl_scandesc(int desc, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options)
855 855
 {
856 856
 	int reclev = 0;
857 857
 
858 858
     return cli_magic_scandesc(desc, virname, scanned, root, limits, options, &reclev);
859 859
 }
860 860
 
861
-int cl_scanfile(const char *filename, char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options)
861
+int cl_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options)
862 862
 {
863 863
 	int fd, ret;
864 864
 
... ...
@@ -105,6 +105,7 @@ typedef unsigned short   UWORD;
105 105
 typedef unsigned long    UDWORD;
106 106
 #endif
107 107
 
108
+#define RAR_FENTRY_ATTR_DIRECTORY ( 0x10 | 0x4000 )
108 109
 
109 110
 /* This structure is used for listing archive content                       */
110 111
 struct RAR20_archive_entry                  /* These infos about files are  */