b151ef55 |
/* |
b9194b42 |
* Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net> |
b151ef55 |
* HTTP/1.1 compliance by Arkadiusz Miskiewicz <misiek@pld.org.pl>
* Proxy support by Nigel Horne <njh@bandsman.co.uk>
* Proxy authorization support by Gernot Tenchio <g.tenchio@telco-tech.de>
* (uses fmt_base64() from libowfat (http://www.fefe.de))
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
|
8b242bb9 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
b151ef55 |
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <clamav.h> |
0539b2a8 |
#include <errno.h> |
b151ef55 |
#include "options.h"
#include "defaults.h"
#include "manager.h"
#include "notify.h" |
36f2038b |
#include "memory.h" |
f91f55e0 |
#include "output.h" |
d2f07436 |
#include "misc.h" |
f91f55e0 |
#include "../libclamav/others.h" |
881069d7 |
#include "../libclamav/str.h" /* cli_strtok */
#include "dns.h"
|
b151ef55 |
|
026ebd88 |
int downloadmanager(const struct cfgstruct *copt, const struct optstruct *opt, const char *hostname) |
b151ef55 |
{
time_t currtime; |
e1b40b1b |
int ret, updated = 0, signo = 0, ttl = -1;
char ipaddr[16], *dnsreply = NULL, *pt; |
0d98d74c |
struct cfgstruct *cpt; |
0539b2a8 |
char *localip = NULL; |
622a7127 |
const char *arg = NULL; |
e1b40b1b |
#ifdef HAVE_RESOLV_H
const char *dnsdbinfo;
#endif |
b151ef55 |
time(&currtime); |
442d8407 |
mprintf("ClamAV update process started at %s", ctime(&currtime));
logg("ClamAV update process started at %s", ctime(&currtime)); |
b151ef55 |
|
442d8407 |
#ifndef HAVE_GMP
mprintf("SECURITY WARNING: NO SUPPORT FOR DIGITAL SIGNATURES\n"); |
4a5581d9 |
mprintf("See the FAQ at http://www.clamav.net/faq.html for an explanation.\n"); |
442d8407 |
logg("SECURITY WARNING: NO SUPPORT FOR DIGITAL SIGNATURES\n"); |
4a5581d9 |
logg("See the FAQ at http://www.clamav.net/faq.html for an explanation.\n"); |
442d8407 |
#endif |
b151ef55 |
|
e1b40b1b |
#ifdef HAVE_RESOLV_H
if((cpt = cfgopt(copt, "DNSDatabaseInfo")))
dnsdbinfo = cpt->strarg;
else
dnsdbinfo = "current.cvd.clamav.net";
if(optl(opt, "no-dns")) {
dnsreply = NULL;
} else {
if((dnsreply = txtquery(dnsdbinfo, &ttl))) {
mprintf("*TTL: %d\n", ttl);
if((pt = cli_strtok(dnsreply, 3, ":"))) {
int rt;
time_t ct;
rt = atoi(pt);
free(pt);
time(&ct);
if((int) ct - rt > 10800) {
mprintf("WARNING: DNS record is older than 3 hours.\n");
logg("WARNING: DNS record is older than 3 hours.\n");
free(dnsreply);
dnsreply = NULL;
}
} else {
free(dnsreply);
dnsreply = NULL;
}
|
d2f07436 |
if(dnsreply) {
int vwarning = 1;
if((pt = cli_strtok(dnsreply, 4, ":"))) {
if(*pt == '0')
vwarning = 0;
free(pt);
}
if((pt = cli_strtok(dnsreply, 0, ":"))) {
mprintf("*Software version from DNS: %s\n", pt);
if(vwarning && !strstr(cl_retver(), "devel") && !strstr(cl_retver(), "rc")) {
if(strcmp(cl_retver(), pt)) {
mprintf("WARNING: Your ClamAV installation is OUTDATED!\n");
mprintf("WARNING: Local version: %s Recommended version: %s\n", cl_retver(), pt);
mprintf("DON'T PANIC! Read http://www.clamav.net/faq.html\n");
logg("WARNING: Your ClamAV installation is OUTDATED!\n");
logg("WARNING: Local version: %s Recommended version: %s\n", cl_retver(), pt);
logg("DON'T PANIC! Read http://www.clamav.net/faq.html\n");
} |
e1b40b1b |
} |
d2f07436 |
free(pt); |
e1b40b1b |
}
} else {
if(dnsreply) {
free(dnsreply);
dnsreply = NULL;
}
}
}
if(!dnsreply) {
mprintf("WARNING: Invalid DNS reply. Falling back to HTTP mode.\n");
logg("WARNING: Invalid DNS reply. Falling back to HTTP mode.\n");
}
}
#endif /* HAVE_RESOLV_H */ |
34f1f7dd |
|
0539b2a8 |
if(optl(opt, "localip")) {
localip = getargl(opt, "localip");
} else if((cpt = cfgopt(copt, "LocalIPAddress"))) {
localip = cpt->strarg;
}
|
1f29c033 |
memset(ipaddr, 0, sizeof(ipaddr));
|
0539b2a8 |
if((ret = downloaddb(DB1NAME, "main.cvd", hostname, ipaddr, &signo, copt, dnsreply, localip)) > 50) { |
e1b40b1b |
if(dnsreply)
free(dnsreply);
|
442d8407 |
return ret; |
e1b40b1b |
} else if(ret == 0) |
442d8407 |
updated = 1; |
b151ef55 |
|
29ca066f |
/* if ipaddr[0] != 0 it will use it to connect to the web host */ |
0539b2a8 |
if((ret = downloaddb(DB2NAME, "daily.cvd", hostname, ipaddr, &signo, copt, dnsreply, localip)) > 50) { |
e1b40b1b |
if(dnsreply)
free(dnsreply);
|
442d8407 |
return ret; |
e1b40b1b |
} else if(ret == 0) |
442d8407 |
updated = 1;
|
e1b40b1b |
if(dnsreply)
free(dnsreply);
|
442d8407 |
if(updated) { |
0d98d74c |
if(cfgopt(copt, "HTTPProxyServer")) { |
b3748b64 |
mprintf("Database updated (%d signatures) from %s\n", signo, hostname);
logg("Database updated (%d signatures) from %s\n", signo, hostname); |
a0faaedf |
} else { |
b3748b64 |
mprintf("Database updated (%d signatures) from %s (IP: %s)\n", signo, hostname, ipaddr);
logg("Database updated (%d signatures) from %s (IP: %s)\n", signo, hostname, ipaddr); |
a0faaedf |
} |
442d8407 |
#ifdef BUILD_CLAMD |
026ebd88 |
if(optl(opt, "daemon-notify")) {
const char *clamav_conf = getargl(opt, "daemon-notify");
if(!clamav_conf) |
09b431f0 |
clamav_conf = CONFDIR"/clamd.conf"; |
026ebd88 |
notify(clamav_conf);
} else if((cpt = cfgopt(copt, "NotifyClamd"))) { |
0d98d74c |
const char *clamav_conf = cpt->strarg; |
442d8407 |
if(!clamav_conf) |
09b431f0 |
clamav_conf = CONFDIR"/clamd.conf"; |
442d8407 |
notify(clamav_conf); |
b151ef55 |
} |
442d8407 |
#endif
|
026ebd88 |
if(optl(opt, "on-update-execute")) |
622a7127 |
arg = getargl(opt, "on-update-execute"); |
026ebd88 |
else if((cpt = cfgopt(copt, "OnUpdateExecute"))) |
622a7127 |
arg = cpt->strarg;
if(arg) {
if(optc(opt, 'd')) |
9c8806fb |
execute("OnUpdateExecute", arg); |
622a7127 |
else |
9c8806fb |
system(arg); |
622a7127 |
} |
442d8407 |
return 0;
} else
return 1;
}
|
0539b2a8 |
int downloaddb(const char *localname, const char *remotename, const char *hostname, char *ip, int *signo, const struct cfgstruct *copt, const char *dnsreply, char *localip) |
442d8407 |
{
struct cl_cvd *current, *remote; |
0d98d74c |
struct cfgstruct *cpt; |
66c64a38 |
int hostfd, nodb = 0, dbver = -1, ret, port = 0, ims = -1; |
e1b40b1b |
char *tempname, ipaddr[16], *pt; |
0d98d74c |
const char *proxy = NULL, *user = NULL, *pass = NULL; |
ead674a2 |
int flevel = cl_retflevel(); |
b151ef55 |
|
881069d7 |
|
442d8407 |
if((current = cl_cvdhead(localname)) == NULL)
nodb = 1; |
b151ef55 |
|
e1b40b1b |
if(!nodb && dnsreply) {
int field = 0; |
34f1f7dd |
|
e1b40b1b |
if(!strcmp(remotename, "main.cvd")) {
field = 1;
} else if(!strcmp(remotename, "daily.cvd")) {
field = 2;
} else {
mprintf("WARNING: Unknown database name (%s) passed.\n", remotename);
logg("WARNING: Unknown database name (%s) passed.\n", remotename);
} |
5e8d60b3 |
|
e1b40b1b |
if(field && (pt = cli_strtok(dnsreply, field, ":"))) {
if(!isnumb(pt)) {
mprintf("WARNING: Broken database version in TXT record.\n");
logg("WARNING: Broken database version in TXT record.\n"); |
881069d7 |
} else { |
e1b40b1b |
dbver = atoi(pt);
mprintf("*%s version from DNS: %d\n", remotename, dbver); |
881069d7 |
} |
e1b40b1b |
free(pt);
} else {
mprintf("WARNING: Invalid DNS reply. Falling back to HTTP mode.\n");
logg("WARNING: Invalid DNS reply. Falling back to HTTP mode.\n"); |
0d98d74c |
} |
e1b40b1b |
|
0d98d74c |
} |
b151ef55 |
|
881069d7 |
/* Initialize proxy settings */ |
0d98d74c |
if((cpt = cfgopt(copt, "HTTPProxyServer"))) {
proxy = cpt->strarg; |
b151ef55 |
if(strncasecmp(proxy, "http://", 7) == 0)
proxy += 7; |
881069d7 |
if((cpt = cfgopt(copt, "HTTPProxyUsername"))) {
user = cpt->strarg;
if((cpt = cfgopt(copt, "HTTPProxyPassword"))) {
pass = cpt->strarg;
} else {
mprintf("HTTPProxyUsername requires HTTPProxyPassword\n");
if(current)
cl_cvdfree(current);
return 56;
}
}
if((cpt = cfgopt(copt, "HTTPProxyPort")))
port = cpt->numarg;
|
442d8407 |
mprintf("Connecting via %s\n", proxy); |
0d98d74c |
}
|
881069d7 |
memset(ipaddr, 0, sizeof(ipaddr)); |
b151ef55 |
|
881069d7 |
if(!nodb && dbver == -1) {
if(ip[0]) /* use ip to connect */ |
0539b2a8 |
hostfd = wwwconnect(ip, proxy, port, ipaddr, localip); |
881069d7 |
else |
0539b2a8 |
hostfd = wwwconnect(hostname, proxy, port, ipaddr, localip); |
881069d7 |
if(hostfd < 0) { |
a774f003 |
mprintf("@No servers could be reached. Giving up\n"); |
881069d7 |
if(current)
cl_cvdfree(current);
return 52;
} else { |
b3748b64 |
mprintf("*Connected to %s (IP: %s).\n", hostname, ipaddr); |
e2505fdf |
mprintf("*Trying to retrieve http://%s/%s\n", hostname, remotename); |
881069d7 |
} |
b151ef55 |
|
881069d7 |
if(!ip[0])
strcpy(ip, ipaddr); |
b151ef55 |
|
66c64a38 |
remote = remote_cvdhead(remotename, hostfd, hostname, proxy, user, pass, &ims);
if(!nodb && !ims) {
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);
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); |
b9194b42 |
*signo += current->sigs; |
e483f51a |
close(hostfd); |
b9194b42 |
cl_cvdfree(current); |
66c64a38 |
return 1;
}
if(!remote) { |
b3748b64 |
mprintf("@Can't read %s header from %s (IP: %s)\n", remotename, hostname, ipaddr); |
881069d7 |
close(hostfd);
if(current)
cl_cvdfree(current);
return 58;
} |
b151ef55 |
|
881069d7 |
dbver = remote->version;
cl_cvdfree(remote); |
b151ef55 |
close(hostfd);
}
|
881069d7 |
if(!nodb && (current->version >= dbver)) { |
442d8407 |
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);
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); |
ead674a2 |
if(flevel < current->fl) {
/* display warning even for already installed database */ |
d5e37688 |
mprintf("WARNING: Your ClamAV installation is OUTDATED!\n");
mprintf("WARNING: Current functionality level = %d, recommended = %d\n", flevel, current->fl);
mprintf("DON'T PANIC! Read http://www.clamav.net/faq.html\n");
logg("WARNING: Your ClamAV installation is OUTDATED!\n");
logg("WARNING: Current functionality level = %d, recommended = %d\n", flevel, current->fl);
logg("DON'T PANIC! Read http://www.clamav.net/faq.html\n"); |
ead674a2 |
}
|
a46476f7 |
*signo += current->sigs; |
442d8407 |
cl_cvdfree(current);
return 1;
}
if(current)
cl_cvdfree(current);
|
34f1f7dd |
if(ipaddr[0]) { |
881069d7 |
/* use ipaddr in order to connect to the same mirror */ |
0539b2a8 |
hostfd = wwwconnect(ipaddr, proxy, port, NULL, localip); |
34f1f7dd |
} else { |
0539b2a8 |
hostfd = wwwconnect(hostname, proxy, port, ipaddr, localip); |
34f1f7dd |
if(!ip[0])
strcpy(ip, ipaddr);
} |
b151ef55 |
if(hostfd < 0) { |
881069d7 |
if(ipaddr[0]) |
b3748b64 |
mprintf("Connection with %s (IP: %s) failed.\n", hostname, ipaddr); |
881069d7 |
else |
a774f003 |
mprintf("Connection with %s failed.\n", hostname); |
b151ef55 |
return 52;
};
|
18fc7513 |
/* the temporary file is created in a directory owned by clamav so race
* conditions are not possible |
442d8407 |
*/ |
f91f55e0 |
tempname = cli_gentemp("."); |
442d8407 |
|
e2505fdf |
mprintf("*Retrieving http://%s/%s\n", hostname, remotename); |
1f5be3bf |
if((ret = get_database(remotename, hostfd, tempname, hostname, proxy, user, pass))) { |
b3748b64 |
mprintf("@Can't download %s from %s (IP: %s)\n", remotename, hostname, ipaddr); |
442d8407 |
unlink(tempname);
free(tempname);
close(hostfd); |
1f5be3bf |
return ret; |
b151ef55 |
}
|
442d8407 |
close(hostfd); |
b151ef55 |
|
442d8407 |
if((ret = cl_cvdverify(tempname))) {
mprintf("@Verification: %s\n", cl_strerror(ret));
unlink(tempname);
free(tempname);
return 54; |
b151ef55 |
}
|
df6c9b74 |
if((current = cl_cvdhead(tempname)) == NULL) { |
881069d7 |
mprintf("@Can't read CVD header of new %s database.\n", localname); |
df6c9b74 |
unlink(tempname);
free(tempname);
return 54;
}
if(current->version < dbver) {
mprintf("@Mirrors are not fully synchronized. Please try again later.\n");
cl_cvdfree(current);
unlink(tempname);
free(tempname); |
1f5be3bf |
return 59; |
df6c9b74 |
}
|
442d8407 |
if(!nodb && unlink(localname)) {
mprintf("@Can't unlink %s. Please fix it and try again.\n", localname); |
df6c9b74 |
cl_cvdfree(current); |
b151ef55 |
unlink(tempname);
free(tempname); |
442d8407 |
return 53; |
6aa7e047 |
} else {
if(rename(tempname, localname) == -1) {
mprintf("@Can't rename %s to %s: %s\n", tempname, localname, strerror(errno));
if(errno == EEXIST) {
unlink(localname);
if(rename(tempname, localname) == -1)
mprintf("@All attempts to rename the temporary file failed: %s\n", strerror(errno));
}
}
} |
b151ef55 |
|
442d8407 |
mprintf("%s updated (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder);
logg("%s updated (version: %d, sigs: %d, f-level: %d, builder: %s)\n", localname, current->version, current->sigs, current->fl, current->builder); |
b151ef55 |
|
ead674a2 |
if(flevel < current->fl) { |
d5e37688 |
mprintf("WARNING: Your ClamAV installation is OUTDATED!\n");
mprintf("WARNING: Current functionality level = %d, recommended = %d\n", flevel, current->fl);
mprintf("DON'T PANIC! Read http://www.clamav.net/faq.html\n");
logg("WARNING: Your ClamAV installation is OUTDATED!\n");
logg("WARNING: Current functionality level = %d, recommended = %d\n", flevel, current->fl);
logg("DON'T PANIC! Read http://www.clamav.net/faq.html\n"); |
ead674a2 |
}
|
881069d7 |
*signo += current->sigs; |
442d8407 |
cl_cvdfree(current);
free(tempname); |
b151ef55 |
return 0;
}
/* this function returns socket descriptor */
/* proxy support finshed by njh@bandsman.co.uk */ |
0539b2a8 |
int wwwconnect(const char *server, const char *proxy, int pport, char *ip, char *localip) |
b151ef55 |
{ |
f58ad3be |
int socketfd = -1, port, i; |
b151ef55 |
struct sockaddr_in name;
struct hostent *host; |
8b1cee14 |
char ipaddr[16]; |
4be012e9 |
unsigned char *ia; |
b151ef55 |
const char *hostpt; |
0539b2a8 |
struct hostent *he = NULL; |
b151ef55 |
|
4be012e9 |
if(ip)
strcpy(ip, "???");
|
b151ef55 |
name.sin_family = AF_INET;
|
0539b2a8 |
if (localip) {
if ((he = gethostbyname(localip)) == NULL) {
char *herr;
switch(h_errno) {
case HOST_NOT_FOUND:
herr = "Host not found";
break;
case NO_DATA:
herr = "No IP address";
break;
case NO_RECOVERY:
herr = "Unrecoverable DNS error";
break;
case TRY_AGAIN:
herr = "Temporary DNS error";
break;
default:
herr = "Unknown error";
break;
}
mprintf("!Could not resolve local ip address '%s': %s\n", localip, herr);
mprintf("^Using standard local ip address and port for fetching.\n");
} else {
struct sockaddr_in client;
memset ((char *) &client, 0, sizeof(struct sockaddr_in));
client.sin_family = AF_INET;
client.sin_addr = *(struct in_addr *) he->h_addr_list[0];
if (bind(socketfd, (struct sockaddr *) &client, sizeof(struct sockaddr_in)) != 0) {
mprintf("!Could not bind to local ip address '%s': %s\n", localip, strerror(errno));
mprintf("^Using default client ip.\n");
} else {
ia = (unsigned char *) he->h_addr_list[0];
sprintf(ipaddr, "%u.%u.%u.%u", ia[0], ia[1], ia[2], ia[3]);
mprintf("*Using ip '%s' for fetching.\n", ipaddr);
}
}
} |
b151ef55 |
if(proxy) { |
0d98d74c |
hostpt = proxy;
if(!(port = pport)) { |
b151ef55 |
#ifndef C_CYGWIN
const struct servent *webcache = getservbyname("webcache", "TCP");
if(webcache)
port = ntohs(webcache->s_port);
else
port = 8080;
endservent();
#else
port = 8080;
#endif
} |
0d98d74c |
|
b151ef55 |
} else {
hostpt = server;
port = 80;
}
if((host = gethostbyname(hostpt)) == NULL) { |
0539b2a8 |
char *herr;
switch(h_errno) {
case HOST_NOT_FOUND:
herr = "Host not found";
break;
case NO_DATA:
herr = "No IP address";
break;
case NO_RECOVERY:
herr = "Unrecoverable DNS error";
break;
case TRY_AGAIN:
herr = "Temporary DNS error";
break;
default:
herr = "Unknown error";
break;
}
mprintf("@Can't get information about %s: %s\n", hostpt, herr); |
b151ef55 |
return -1;
}
|
ad8e29f9 |
for(i = 0; host->h_addr_list[i] != 0; i++) {
/* this dirty hack comes from pink - Nosuid TCP/IP ping 1.6 */
ia = (unsigned char *) host->h_addr_list[i];
sprintf(ipaddr, "%u.%u.%u.%u", ia[0], ia[1], ia[2], ia[3]); |
4be012e9 |
|
ad8e29f9 |
if(ip)
strcpy(ip, ipaddr);
if(i > 0)
mprintf("Trying host %s (%s)...\n", hostpt, ipaddr); |
4be012e9 |
|
ad8e29f9 |
name.sin_addr = *((struct in_addr *) host->h_addr_list[i]);
name.sin_port = htons(port);
|
555d5ec3 |
#ifdef PF_INET
socketfd = socket(PF_INET, SOCK_STREAM, 0);
#else
socketfd = socket(AF_INET, SOCK_STREAM, 0);
#endif
|
ad8e29f9 |
if(connect(socketfd, (struct sockaddr *) &name, sizeof(struct sockaddr_in)) == -1) { |
b3748b64 |
mprintf("Can't connect to port %d of host %s (IP: %s)\n", port, hostpt, ipaddr); |
555d5ec3 |
close(socketfd); |
ad8e29f9 |
continue;
} |
b151ef55 |
|
ad8e29f9 |
return socketfd; |
b151ef55 |
}
|
ad8e29f9 |
return -2; |
b151ef55 |
}
|
66c64a38 |
int Rfc2822DateTime(char *buf, time_t mtime)
{
struct tm *time;
time = gmtime(&mtime);
return strftime(buf, 36, "%a, %d %b %Y %X GMT", time);
}
struct cl_cvd *remote_cvdhead(const char *file, int socketfd, const char *hostname, const char *proxy, const char *user, const char *pass, int *ims) |
b151ef55 |
{ |
8b1cee14 |
char cmd[512], head[513], buffer[FILEBUFF], *ch, *tmp; |
b151ef55 |
int i, j, bread, cnt;
char *remotename = NULL, *authorization = NULL; |
442d8407 |
struct cl_cvd *cvd; |
66c64a38 |
char last_modified[36];
struct stat sb;
|
b151ef55 |
if(proxy) {
remotename = mmalloc(strlen(hostname) + 8);
sprintf(remotename, "http://%s", hostname);
if(user) {
int len; |
c8c761d1 |
char *buf = mmalloc((strlen(pass) + strlen(user)) * 2 + 4); |
0d98d74c |
char *userpass = mmalloc(strlen(user) + strlen(pass) + 2);
sprintf(userpass, "%s:%s", user, pass);
len=fmt_base64(buf,userpass,strlen(userpass));
free(userpass); |
b151ef55 |
buf[len]='\0';
authorization = mmalloc(strlen(buf) + 30);
sprintf(authorization, "Proxy-Authorization: Basic %s\r\n", buf);
free(buf); |
0d98d74c |
} |
b151ef55 |
}
|
b9194b42 |
if(stat(file, &sb) != -1 && sb.st_mtime < time(NULL)) { |
66c64a38 |
Rfc2822DateTime(last_modified, sb.st_mtime);
} else {
time_t mtime = 1104119530;
Rfc2822DateTime(last_modified, mtime); |
b9194b42 |
mprintf("*Assuming modification time in the past\n"); |
66c64a38 |
} |
b151ef55 |
|
66c64a38 |
mprintf("*If-Modified-Since: %s\n", last_modified);
mprintf("Reading CVD header (%s): ", file); |
b151ef55 |
#ifdef NO_SNPRINTF |
442d8407 |
sprintf(cmd, "GET %s/%s HTTP/1.1\r\n" |
b151ef55 |
"Host: %s\r\n%s"
"User-Agent: "PACKAGE"/"VERSION"\r\n"
"Connection: close\r\n" |
35ea9b81 |
"Range: bytes=0-511\r\n" |
66c64a38 |
"If-Modified-Since: %s\r\n"
"\r\n", (remotename != NULL)?remotename:"", file, hostname, (authorization != NULL)?authorization:"", last_modified); |
b151ef55 |
#else |
442d8407 |
snprintf(cmd, sizeof(cmd), "GET %s/%s HTTP/1.1\r\n" |
35ea9b81 |
"Host: %s\r\n%s"
"User-Agent: "PACKAGE"/"VERSION"\r\n"
"Connection: close\r\n"
"Range: bytes=0-511\r\n" |
66c64a38 |
"If-Modified-Since: %s\r\n"
"\r\n", (remotename != NULL)?remotename:"", file, hostname, (authorization != NULL)?authorization:"", last_modified); |
b151ef55 |
#endif
write(socketfd, cmd, strlen(cmd));
free(remotename);
free(authorization);
tmp = buffer; |
7b7b3ca5 |
cnt = FILEBUFF; |
b151ef55 |
while ((bread = recv(socketfd, tmp, cnt, 0)) > 0) {
tmp+=bread;
cnt-=bread;
if (cnt <= 0) break;
}
if(bread == -1) { |
442d8407 |
mprintf("@Error while reading CVD header of database from %s\n", hostname); |
b151ef55 |
return NULL;
}
|
20535d8a |
if((strstr(buffer, "HTTP/1.1 404")) != NULL || (strstr(buffer, "HTTP/1.0 404")) != NULL) { |
66c64a38 |
mprintf("@CVD file not found on remote server\n");
return NULL;
}
/* check whether the resource is up-to-date */ |
20535d8a |
if((strstr(buffer, "HTTP/1.1 304")) != NULL || (strstr(buffer, "HTTP/1.0 304")) != NULL) { |
66c64a38 |
*ims = 0;
mprintf("OK (IMS)\n");
return NULL;
} else {
*ims = 1; |
b151ef55 |
}
ch = buffer;
i = 0;
while (1) {
if (*ch == '\n' && *(ch - 1) == '\r' && *(ch - 2) == '\n' && *(ch - 3) == '\r') {
ch++;
i++;
break;
}
ch++;
i++;
}
|
442d8407 |
memset(head, 0, sizeof(head));
for (j=0; j<512; j++) {
if (!ch || (ch && !*ch) || (ch && !isprint(ch[j]))) {
mprintf("@Malformed CVD header detected.\n"); |
b151ef55 |
return NULL;
} |
442d8407 |
head[j] = ch[j]; |
b151ef55 |
}
|
442d8407 |
if((cvd = cl_cvdparse(head)) == NULL)
mprintf("@Broken CVD header.\n");
else
mprintf("OK\n");
return cvd; |
b151ef55 |
}
|
0d98d74c |
int get_database(const char *dbfile, int socketfd, const char *file, const char *hostname, const char *proxy, const char *user, const char *pass) |
b151ef55 |
{ |
0d98d74c |
char cmd[512], buffer[FILEBUFF], *ch; |
442d8407 |
int bread, fd, i, rot = 0; |
b151ef55 |
char *remotename = NULL, *authorization = NULL; |
442d8407 |
const char *rotation = "|/-\\"; |
b151ef55 |
if(proxy) {
remotename = mmalloc(strlen(hostname) + 8);
sprintf(remotename, "http://%s", hostname);
if(user) {
int len; |
c8c761d1 |
char *buf = mmalloc((strlen(pass) + strlen(user)) * 2 + 4); |
0d98d74c |
char *userpass = mmalloc(strlen(user) + strlen(pass) + 2);
sprintf(userpass, "%s:%s", user, pass);
len=fmt_base64(buf,userpass,strlen(userpass));
free(userpass); |
b151ef55 |
buf[len]='\0';
authorization = mmalloc(strlen(buf) + 30);
sprintf(authorization, "Proxy-Authorization: Basic %s\r\n", buf);
free(buf);
}
}
|
193c72c5 |
#if defined(C_CYGWIN) || defined(C_OS2) |
bf539ea1 |
if((fd = open(file, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644)) == -1) {
#else |
b151ef55 |
if((fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0644)) == -1) { |
bf539ea1 |
#endif |
18fc7513 |
char currdir[512];
getcwd(currdir, sizeof(currdir));
mprintf("@Can't create new file %s in %s\n", file, currdir);
mprintf("@The database directory must be writable for UID %d or GID %d\n", getuid(), getgid()); |
1f5be3bf |
return 57; |
b151ef55 |
}
#ifdef NO_SNPRINTF |
442d8407 |
sprintf(cmd, "GET %s/%s HTTP/1.1\r\n" |
b151ef55 |
"Host: %s\r\n%s"
"User-Agent: "PACKAGE"/"VERSION"\r\n"
"Cache-Control: no-cache\r\n"
"Connection: close\r\n"
"\r\n", (remotename != NULL)?remotename:"", dbfile, hostname, (authorization != NULL)?authorization:"");
#else |
442d8407 |
snprintf(cmd, sizeof(cmd), "GET %s/%s HTTP/1.1\r\n" |
b151ef55 |
"Host: %s\r\n%s"
"User-Agent: "PACKAGE"/"VERSION"\r\n"
"Cache-Control: no-cache\r\n"
"Connection: close\r\n"
"\r\n", (remotename != NULL)?remotename:"", dbfile, hostname, (authorization != NULL)?authorization:"");
#endif
write(socketfd, cmd, strlen(cmd));
free(remotename);
free(authorization);
|
c5e5f90a |
/* read all the http headers */ |
b151ef55 |
ch = buffer;
i = 0;
while (1) { |
c5e5f90a |
/* recv one byte at a time, until we reach \r\n\r\n */
if(recv(socketfd, buffer + i, 1, 0) == -1) {
mprintf("@Error while reading database from %s\n", hostname);
close(fd);
unlink(file);
return 52;
}
if (i>2 && *ch == '\n' && *(ch - 1) == '\r' && *(ch - 2) == '\n' && *(ch - 3) == '\r') { |
b151ef55 |
i++;
break;
}
ch++;
i++; |
1f5be3bf |
} |
b151ef55 |
|
1b2046e3 |
buffer[i] = 0;
|
c5e5f90a |
/* check whether the resource actually existed or not */
if ((strstr(buffer, "HTTP/1.1 404")) != NULL) {
mprintf("@%s not found on remote server\n", dbfile);
close(fd);
unlink(file);
return 58;
}
/* receive body and write it to disk */
|
7b7b3ca5 |
while((bread = read(socketfd, buffer, FILEBUFF))) { |
b151ef55 |
write(fd, buffer, bread); |
442d8407 |
mprintf("Downloading %s [%c]\r", dbfile, rotation[rot]);
fflush(stdout); |
8b1cee14 |
rot++; |
8b242bb9 |
rot %= 4; |
b151ef55 |
}
|
442d8407 |
mprintf("Downloading %s [*]\n", dbfile); |
b151ef55 |
close(fd);
return 0;
}
const char base64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
unsigned int fmt_base64(char* dest,const char* src,unsigned int len) {
register const unsigned char* s=(const unsigned char*) src;
unsigned short bits=0,temp=0;
unsigned long written=0,i;
for (i=0; i< len; ++i) {
temp<<=8; temp+=s[i]; bits+=8;
while (bits>6) {
if (dest) dest[written]=base64[((temp>>(bits-6))&63)];
++written; bits-=6;
}
}
if (bits) {
temp<<=(6-bits);
if (dest) dest[written]=base64[temp&63];
++written;
}
while (written&3) { if (dest) dest[written]='='; ++written; }
return written;
} |