freshclam/mirman.c
376307a0
 /*
  *  Copyright (C) 2007 Tomasz Kojm <tkojm@clamav.net>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
  *
  *  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., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
  */
 
361cc771
 #ifdef	_MSC_VER
 #include <winsock.h>
 #endif
 
376307a0
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
12702429
 #ifdef	C_WINDOWS
 #define	_USE_32BIT_TIME_T	/* FIXME: mirdat.atime assumes 32bit time_t */
 #endif
 
376307a0
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <time.h>
 
44eb77a1
 #ifndef C_WINDOWS
b54eb319
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
44eb77a1
 #endif
b54eb319
 
376307a0
 #include "mirman.h"
 
 #include "libclamav/cltypes.h"
 #include "libclamav/clamav.h"
 
 #include "shared/output.h"
 
 #ifndef O_BINARY
 #define O_BINARY    0
 #endif
 
3027857c
 #ifndef HAVE_GETADDRINFO
9424a482
 #ifndef AF_INET6
 #define AF_INET6    0xbeef  /* foo */
 #endif
 #endif
 
43dee84b
 #define IGNORE_LONG	3 * 86400
 #define IGNORE_SHORT	1800
376307a0
 
 void mirman_free(struct mirdat *mdat)
 {
     if(mdat && mdat->num) {
 	free(mdat->mirtab);
 	mdat->num = 0;
     }
 }
 
a7db63d1
 int mirman_read(const char *file, struct mirdat *mdat, uint8_t active)
376307a0
 {
 	struct mirdat_ip mip;
 	int fd, bread;
 
 
     memset(mdat, 0, sizeof(struct mirdat));
 
a7db63d1
     if(!(mdat->active = active))
 	return 0;
 
376307a0
     if((fd = open(file, O_RDONLY|O_BINARY)) == -1)
 	return -1;
 
     while((bread = read(fd, &mip, sizeof(mip))) == sizeof(mip)) {
 	mdat->mirtab = (struct mirdat_ip *) realloc(mdat->mirtab, (mdat->num + 1) * sizeof(mip));
 	if(!mdat->mirtab) {
 	    logg("!Can't allocate memory for mdat->mirtab\n");
 	    mirman_free(mdat);
 	    close(fd);
 	    return -1;
 	}
 	memcpy(&mdat->mirtab[mdat->num], &mip, sizeof(mip));
 	mdat->num++;
     }
 
     close(fd);
 
     if(bread) {
40846ea3
 	logg("^Removing broken %s file.\n", file);
376307a0
 	unlink(file);
 	mirman_free(mdat);
 	return -1;
     }
 
     return 0;
 }
 
510da7b7
 int mirman_check(uint32_t *ip, int af, struct mirdat *mdat, struct mirdat_ip **md)
376307a0
 {
 	unsigned int i, flevel = cl_retflevel();
 
 
510da7b7
     if(md)
 	*md = NULL;
 
a7db63d1
     if(!mdat->active)
 	return 0;
 
376307a0
     for(i = 0; i < mdat->num; i++) {
b54eb319
 
95c131db
 	if((af == AF_INET && mdat->mirtab[i].ip4 == *ip) || (af == AF_INET6 && !memcmp(mdat->mirtab[i].ip6, ip, 4 * sizeof(uint32_t)))) {
510da7b7
 
43dee84b
 	    if(!mdat->mirtab[i].atime && !mdat->mirtab[i].ignore) {
510da7b7
 		if(md)
 		    *md = &mdat->mirtab[i];
 		return 0;
 	    }
376307a0
 
 	    if(mdat->dbflevel && (mdat->dbflevel > flevel) && (mdat->dbflevel - flevel > 3))
 		if(time(NULL) - mdat->mirtab[i].atime < 4 * 3600)
 		    return 2;
 
 	    if(mdat->mirtab[i].ignore) {
43dee84b
 		if(!mdat->mirtab[i].atime)
 		    return 1;
 
 		if(time(NULL) - mdat->mirtab[i].atime > IGNORE_LONG) {
376307a0
 		    mdat->mirtab[i].ignore = 0;
510da7b7
 		    if(md)
 			*md = &mdat->mirtab[i];
376307a0
 		    return 0;
 		} else {
43dee84b
 		    if(mdat->mirtab[i].ignore == 2 && (time(NULL) - mdat->mirtab[i].atime > IGNORE_SHORT)) {
 			if(md)
 			    *md = &mdat->mirtab[i];
 			return 0;
 		    }
376307a0
 		    return 1;
 		}
 	    }
510da7b7
 
 	    if(md)
 		*md = &mdat->mirtab[i];
 	    return 0;
376307a0
 	}
     }
 
     return 0;
 }
 
b54eb319
 int mirman_update(uint32_t *ip, int af, struct mirdat *mdat, uint8_t broken)
376307a0
 {
 	unsigned int i, found = 0;
 
 
a7db63d1
     if(!mdat->active)
 	return 0;
 
376307a0
     for(i = 0; i < mdat->num; i++) {
95c131db
 	if((af == AF_INET && mdat->mirtab[i].ip4 == *ip) || (af == AF_INET6 && !memcmp(mdat->mirtab[i].ip6, ip, 4 * sizeof(uint32_t)))) {
376307a0
 	    found = 1;
 	    break;
 	}
     }
 
     if(found) {
04a1171f
 	mdat->mirtab[i].atime = 0; /* will be updated in mirman_write() */
376307a0
 	if(broken)
 	    mdat->mirtab[i].fail++;
 	else
 	    mdat->mirtab[i].succ++;
 
43dee84b
 	if(broken == 2) {
 	    mdat->mirtab[i].ignore = 2;
 	} else {
 	    /*
 	     * If the total number of failures is less than 3 then never
 	     * mark a permanent failure, in other case use the real status.
 	     */
 	    if(mdat->mirtab[i].fail < 3)
 		mdat->mirtab[i].ignore = 0;
 	    else
 		mdat->mirtab[i].ignore = broken;
 	}
376307a0
 
     } else {
 	mdat->mirtab = (struct mirdat_ip *) realloc(mdat->mirtab, (mdat->num + 1) * sizeof(struct mirdat_ip));
 	if(!mdat->mirtab) {
 	    logg("!Can't allocate memory for new element in mdat->mirtab\n");
 	    return -1;
 	}
b54eb319
 	if(af == AF_INET) {
 	    mdat->mirtab[mdat->num].ip4 = *ip;
 	} else {
 	    mdat->mirtab[mdat->num].ip4 = 0;
95c131db
 	    memcpy(mdat->mirtab[mdat->num].ip6, ip, 4 * sizeof(uint32_t));
b54eb319
 	}
04a1171f
 	mdat->mirtab[mdat->num].atime = 0;
376307a0
 	mdat->mirtab[mdat->num].succ = 0;
 	mdat->mirtab[mdat->num].fail = 0;
43dee84b
 	mdat->mirtab[mdat->num].ignore = (broken == 2) ? 2 : 0;
376307a0
 	memset(&mdat->mirtab[mdat->num].res, 0xff, sizeof(mdat->mirtab[mdat->num].res));
 	if(broken)
 	    mdat->mirtab[mdat->num].fail++;
 	else
 	    mdat->mirtab[mdat->num].succ++;
 	mdat->num++;
     }
 
     return 0;
 }
 
 void mirman_list(const struct mirdat *mdat)
 {
 	unsigned int i;
7c647273
 	time_t tm;
b54eb319
 	char ip[46];
7c647273
 
376307a0
 
     for(i = 0; i < mdat->num; i++) {
 	printf("Mirror #%u\n", i + 1);
3027857c
 #ifdef HAVE_GETADDRINFO
b54eb319
 	if(mdat->mirtab[i].ip4)
 	    printf("IP: %s\n", inet_ntop(AF_INET, &mdat->mirtab[i].ip4, ip, sizeof(ip)));
 	else
 	    printf("IP: %s\n", inet_ntop(AF_INET6, mdat->mirtab[i].ip6, ip, sizeof(ip)));
9424a482
 #else
 	if(mdat->mirtab[i].ip4)
 	    printf("IP: %s\n", inet_ntoa(*(struct in_addr*) &mdat->mirtab[i].ip4));
 #endif
376307a0
 	printf("Successes: %u\n", mdat->mirtab[i].succ);
 	printf("Failures: %u\n", mdat->mirtab[i].fail);
7c647273
 	tm = mdat->mirtab[i].atime;
 	printf("Last access: %s", ctime((const time_t *)&tm));
376307a0
 	printf("Ignore: %s\n", mdat->mirtab[i].ignore ? "Yes" : "No");
 	if(i != mdat->num - 1)
 	    printf("-------------------------------------\n");
     }
 }
 
43dee84b
 void mirman_whitelist(struct mirdat *mdat, unsigned int mode)
a68aa31f
 {
 	unsigned int i;
 
43dee84b
     logg("*Whitelisting %s blacklisted mirrors\n", mode == 1 ? "all" : "short-term");
a68aa31f
     for(i = 0; i < mdat->num; i++)
43dee84b
 	if(mode == 1 || (mode == 2 && mdat->mirtab[i].ignore == 2))
 	    mdat->mirtab[i].ignore = 0;
a68aa31f
 }
 
376307a0
 int mirman_write(const char *file, struct mirdat *mdat)
 {
 	int fd;
04a1171f
 	unsigned int i;
376307a0
 
 
     if(!mdat->num)
 	return 0;
 
     if((fd = open(file, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600)) == -1) {
 	logg("!Can't open %s for writing\n", file);
 	mirman_free(mdat);
 	return -1;
     }
 
04a1171f
     for(i = 0; i < mdat->num; i++)
 	if(!mdat->mirtab[i].atime)
 	    mdat->mirtab[i].atime = (uint32_t) time(NULL);
 
376307a0
     if(write(fd, mdat->mirtab, mdat->num * sizeof(struct mirdat_ip)) == -1) {
 	logg("!Can't write to %s\n", file);
 	mirman_free(mdat);
 	close(fd);
 	return -1;
     }
 
     mirman_free(mdat);
     close(fd);
     return 0;
 }