Browse code

use If-Modified-Since in --no-dns mode

git-svn: trunk@1218

Tomasz Kojm authored on 2004/12/27 13:04:28
Showing 5 changed files
... ...
@@ -152,6 +152,7 @@ Matt Sullivan <matt*sullivan.gen.nz>
152 152
 Joe Talbott <josepht*cstone.net>
153 153
 Gernot Tenchio <g.tenchio*telco-tech.de>
154 154
 Michael L Torrie <torriem*chem.byu.edu>
155
+Reini Urban <rurban*x-ray.at>
155 156
 Laurent Wacrenier <lwa*teaser.fr>
156 157
 David Woakes <david*mitredata.co.uk>
157 158
 Dale Woolridge <dwoolridge*drh.net>
... ...
@@ -1,3 +1,8 @@
1
+Mon Dec 27 05:01:54 CET 2004 (tk)
2
+---------------------------------
3
+  * freshclam/manager.c: use If-Modified-Since in --no-dns mode (based on code
4
+			 by Reini Urban <rurban*x-ray.at>)
5
+
1 6
 Mon Dec 27 01:09:20 CET 2004 (tk)
2 7
 ---------------------------------
3 8
   * libclamav/scanners.c: Add missing ArchiveBlockMax rule for recursion limit
... ...
@@ -428,9 +428,8 @@ void help(void)
428 428
     mprintf("    --version            -V              print version number and exit\n");
429 429
     mprintf("    --verbose            -v              be verbose\n");
430 430
     mprintf("    --debug                              enable debug messages\n");
431
-    mprintf("    --quiet                              be quiet, output only error messages\n");
431
+    mprintf("    --quiet                              only output error messages\n");
432 432
     mprintf("    --stdout                             write to stdout instead of stderr\n");
433
-    mprintf("                                         (this help is always written to stdout)\n");
434 433
     mprintf("\n");
435 434
     mprintf("    --config-file=FILE                   read configuration from FILE.\n");
436 435
     mprintf("    --log=FILE           -l FILE         log into FILE\n");
... ...
@@ -441,7 +440,7 @@ void help(void)
441 441
     mprintf("    --checks=#n          -c #n           number of checks per day, 1 <= n <= 50\n");
442 442
     mprintf("    --datadir=DIRECTORY                  download new databases into DIRECTORY\n");
443 443
 #ifdef BUILD_CLAMD
444
-    mprintf("    --daemon-notify[=/path/clamd.conf]  send RELOAD command to clamd\n");
444
+    mprintf("    --daemon-notify[=/path/clamd.conf]   send RELOAD command to clamd\n");
445 445
 #endif
446 446
     mprintf("    --on-update-execute=COMMAND          execute COMMAND after successful update\n");
447 447
     mprintf("    --on-error-execute=COMMAND           execute COMMAND if errors occured\n");
... ...
@@ -204,7 +204,7 @@ int downloaddb(const char *localname, const char *remotename, const char *hostna
204 204
 {
205 205
 	struct cl_cvd *current, *remote;
206 206
 	struct cfgstruct *cpt;
207
-	int hostfd, nodb = 0, dbver = -1, ret, port = 0;
207
+	int hostfd, nodb = 0, dbver = -1, ret, port = 0, ims = -1;
208 208
 	char  *tempname, ipaddr[16], *pt;
209 209
 	const char *proxy = NULL, *user = NULL, *pass = NULL;
210 210
 	int flevel = cl_retflevel();
... ...
@@ -285,7 +285,18 @@ int downloaddb(const char *localname, const char *remotename, const char *hostna
285 285
 	if(!ip[0])
286 286
 	    strcpy(ip, ipaddr);
287 287
 
288
-	if(!(remote = remote_cvdhead(remotename, hostfd, hostname, proxy, user, pass))) {
288
+	remote = remote_cvdhead(remotename, hostfd, hostname, proxy, user, pass, &ims);
289
+
290
+	if(!nodb && !ims) {
291
+	    mprintf("%s is up to date (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder);
292
+	    logg("%s is up to date (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder);
293
+	    if(current)
294
+		cl_cvdfree(current);
295
+
296
+	    return 1;
297
+	}
298
+
299
+	if(!remote) {
289 300
 	    mprintf("@Can't read %s header from %s (%s)\n", remotename, hostname, ipaddr);
290 301
 	    close(hostfd);
291 302
 	    if(current)
... ...
@@ -477,14 +488,23 @@ int wwwconnect(const char *server, const char *proxy, int pport, char *ip)
477 477
     return -2;
478 478
 }
479 479
 
480
-/* njh@bandsman.co.uk: added proxy support */
481
-/* TODO: use a HEAD instruction to see if the file has been changed */
482
-struct cl_cvd *remote_cvdhead(const char *file, int socketfd, const char *hostname, const char *proxy, const char *user, const char *pass)
480
+int Rfc2822DateTime(char *buf, time_t mtime)
481
+{
482
+	struct tm *time;
483
+
484
+    time = gmtime(&mtime);
485
+    return strftime(buf, 36, "%a, %d %b %Y %X GMT", time);
486
+}
487
+
488
+struct cl_cvd *remote_cvdhead(const char *file, int socketfd, const char *hostname, const char *proxy, const char *user, const char *pass, int *ims)
483 489
 {
484 490
 	char cmd[512], head[513], buffer[FILEBUFF], *ch, *tmp;
485 491
 	int i, j, bread, cnt;
486 492
 	char *remotename = NULL, *authorization = NULL;
487 493
 	struct cl_cvd *cvd;
494
+	char last_modified[36];
495
+	struct stat sb;
496
+
488 497
 
489 498
     if(proxy) {
490 499
         remotename = mmalloc(strlen(hostname) + 8);
... ...
@@ -504,8 +524,16 @@ struct cl_cvd *remote_cvdhead(const char *file, int socketfd, const char *hostna
504 504
         }
505 505
     }
506 506
 
507
-    mprintf("Reading CVD header (%s): ", file);
507
+    if(stat(file, &sb) != -1) {
508
+	Rfc2822DateTime(last_modified, sb.st_mtime);
509
+    } else {
510
+	    time_t mtime = 1104119530;
511
+	Rfc2822DateTime(last_modified, mtime);
512
+    }
513
+
514
+    mprintf("*If-Modified-Since: %s\n", last_modified);
508 515
 
516
+    mprintf("Reading CVD header (%s): ", file);
509 517
 #ifdef	NO_SNPRINTF
510 518
     sprintf(cmd, "GET %s/%s HTTP/1.1\r\n"
511 519
 	"Host: %s\r\n%s"
... ...
@@ -513,7 +541,8 @@ struct cl_cvd *remote_cvdhead(const char *file, int socketfd, const char *hostna
513 513
 	"Cache-Control: no-cache\r\n"
514 514
 	"Connection: close\r\n"
515 515
 	"Range: bytes=0-511\r\n"
516
-	"\r\n", (remotename != NULL)?remotename:"", file, hostname, (authorization != NULL)?authorization:"");
516
+        "If-Modified-Since: %s\r\n"
517
+        "\r\n", (remotename != NULL)?remotename:"", file, hostname, (authorization != NULL)?authorization:"", last_modified);
517 518
 #else
518 519
     snprintf(cmd, sizeof(cmd), "GET %s/%s HTTP/1.1\r\n"
519 520
 	"Host: %s\r\n%s"
... ...
@@ -521,7 +550,8 @@ struct cl_cvd *remote_cvdhead(const char *file, int socketfd, const char *hostna
521 521
 	"Cache-Control: no-cache\r\n"
522 522
 	"Connection: close\r\n"
523 523
 	"Range: bytes=0-511\r\n"
524
-	"\r\n", (remotename != NULL)?remotename:"", file, hostname, (authorization != NULL)?authorization:"");
524
+        "If-Modified-Since: %s\r\n"
525
+        "\r\n", (remotename != NULL)?remotename:"", file, hostname, (authorization != NULL)?authorization:"", last_modified);
525 526
 #endif
526 527
     write(socketfd, cmd, strlen(cmd));
527 528
 
... ...
@@ -541,9 +571,19 @@ struct cl_cvd *remote_cvdhead(const char *file, int socketfd, const char *hostna
541 541
 	return NULL;
542 542
     }
543 543
 
544
-    if ((strstr(buffer, "HTTP/1.1 404")) != NULL) { 
545
-      mprintf("@CVD file not found on remote server\n");
546
-      return NULL;
544
+    if((strstr(buffer, "HTTP/1.1 404")) != NULL) { 
545
+	mprintf("@CVD file not found on remote server\n");
546
+	return NULL;
547
+    }
548
+
549
+    /* check whether the resource is up-to-date */
550
+    if((strstr(buffer, "HTTP/1.1 304")) != NULL) { 
551
+
552
+	*ims = 0;
553
+	mprintf("OK (IMS)\n");
554
+	return NULL;
555
+    } else {
556
+	*ims = 1;
547 557
     }
548 558
 
549 559
     ch = buffer;
... ...
@@ -576,8 +616,6 @@ struct cl_cvd *remote_cvdhead(const char *file, int socketfd, const char *hostna
576 576
     return cvd;
577 577
 }
578 578
 
579
-/* njh@bandsman.co.uk: added proxy support */
580
-/* TODO: use a HEAD instruction to see if the file has been changed */
581 579
 int get_database(const char *dbfile, int socketfd, const char *file, const char *hostname, const char *proxy, const char *user, const char *pass)
582 580
 {
583 581
 	char cmd[512], buffer[FILEBUFF], *ch;
... ...
@@ -28,7 +28,7 @@ int downloaddb(const char *localname, const char *remotename, const char *hostna
28 28
 
29 29
 int wwwconnect(const char *server, const char *proxy, int pport, char *ip);
30 30
 
31
-struct cl_cvd *remote_cvdhead(const char *file, int socketfd, const char *hostname, const char *proxy, const char *user, const char *pass);
31
+struct cl_cvd *remote_cvdhead(const char *file, int socketfd, const char *hostname, const char *proxy, const char *user, const char *pass, int *ims);
32 32
 
33 33
 int get_database(const char *dbfile, int socketfd, const char *file, const char *hostname, const char *proxy, const char *user, const char *pass);
34 34