/* * Copyright (C) 2006 Tony Sin(x) '76 * All rights reserved. * */ /* * GNU GENERAL PUBLIC LICENSE * Version 2, June 1991 * * Copyright (C) 1989, 1991 Free Software Foundation, Inc. * 675 Mass Ave, Cambridge, MA 02139, USA * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ifdef HAVE_CONFIG_H #ifndef __main_config_h__ #define __main_config_h__ #include "../config.h" #endif #include #include #include #include #include #include #include #include #include #include #include #include #endif #include "clam.h" #include "exception.h" struct cl_engine *cClamAv::root; int cClamAv::counter = 0; int cClamAv::scanTaskCounter = 0; pthread_mutex_t cClamAv::counterMutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t cClamAv::dbMutex = PTHREAD_MUTEX_INITIALIZER; pthread_t cClamAv::thid = 0; unsigned long int cClamAv::sizelimit = 1048576; cClamAv::cClamAv() { unsigned int signo = 0; int err = 0; pthread_attr_t thattr; FILE *fp; try { if ((err = putenv(CLAM_TMP_VAR))) throw cException(CLAMAV_CLASS_NAME,CLAMAV_CLASS_NAME,"putenv",err); if (!(counter++)) { if ((err = cl_init(CL_INIT_DEFAULT))) throw cException(CLAMAV_CLASS_NAME,CLAMAV_CLASS_NAME,"cl_init",err); root = cl_engine_new(); if ((err = cl_load(cl_retdbdir(), root, &signo, CL_DB_STDOPT))) throw cException(CLAMAV_CLASS_NAME,CLAMAV_CLASS_NAME,"cl_load",err); if ((err = cl_engine_compile(root))) throw cException(CLAMAV_CLASS_NAME,CLAMAV_CLASS_NAME,"cl_engine_compile",err); if ((err = pthread_attr_init(&thattr))) throw cException(CLAMAV_CLASS_NAME,CLAMAV_CLASS_NAME,"pthread_attr_init",err,1); if ((err = pthread_attr_setdetachstate(&thattr,PTHREAD_CREATE_DETACHED))) throw cException(CLAMAV_CLASS_NAME,CLAMAV_CLASS_NAME,"pthread_attr_setdetachstate",err,1); if ((err = pthread_create(&thid,&thattr,taskProc,(void *) this))) throw cException(CLAMAV_CLASS_NAME,CLAMAV_CLASS_NAME,"pthread_create",err,1); if ((fp = fopen(SIZE_LIMIT_FILE,"rt")) != NULL) { fscanf(fp,"%lu",&sizelimit); fclose(fp); } syslog(LOG_INFO,"ClamAv database loaded"); } } catch (cException c_e) { if (c_e.err_depth != 1) { root = NULL; syslog(LOG_ERR,"%s",c_e.str); } } } cClamAv::~cClamAv() { if (!(--counter)) { pthread_kill(thid,SIGUSR2); cl_engine_free(root); root = NULL; } } bool cClamAv::scanFile(const char *filename, const char **virus_name) { int result = CL_CLEAN; struct stat msg_size; try { pthread_mutex_lock(&counterMutex); if ((++scanTaskCounter) == 1) pthread_mutex_lock(&dbMutex); pthread_mutex_unlock(&counterMutex); if (stat(filename,&msg_size) == -1) throw cException(CLAMAV_CLASS_NAME,"scanFile","stat",errno,0); if ((!sizelimit) || (((unsigned long int) msg_size.st_size) < sizelimit)) result = ((root != NULL)? cl_scanfile(filename, virus_name, NULL, root, CL_SCAN_ARCHIVE|CL_SCAN_MAIL|CL_SCAN_OLE2|CL_SCAN_PE| CL_SCAN_HTML|CL_SCAN_BLOCKBROKEN) : CL_CLEAN); else syslog(LOG_INFO,"message too big, scanning skipped"); pthread_mutex_lock(&counterMutex); if (!(--scanTaskCounter)) pthread_mutex_unlock(&dbMutex); pthread_mutex_unlock(&counterMutex); if ((result != CL_CLEAN) && (result != CL_VIRUS)) throw cException(CLAMAV_CLASS_NAME,"scanFile","cl_scanfile",result); } catch(cException c_e) { if (c_e.err_depth == 1) { pthread_mutex_lock(&counterMutex); if (!(--scanTaskCounter)) pthread_mutex_unlock(&dbMutex); pthread_mutex_unlock(&counterMutex); } syslog(LOG_ERR,"%s",c_e.str); syslog(LOG_ERR,"error occurred, message will not be blocked"); result = CL_CLEAN; } return (result == CL_VIRUS); } void *cClamAv::taskProc(void *arg) { cClamAv *ptr = (cClamAv *) arg; struct cl_stat dbstat; int err, sig; unsigned int signo; sigset_t mask; syslog(LOG_INFO,"cClamDBTask started"); try { if ((err = putenv(CLAM_TMP_VAR))) throw cException(CLAMAV_TASK_NAME,"taskProc","putenv",err); if (sigemptyset(&mask) == -1) throw cException(CLAMAV_TASK_NAME,"taskProc","sigemptyset, update won't be available",errno); if (sigaddset(&mask,SIGUSR1) == -1) throw cException(CLAMAV_TASK_NAME,"taskProc","sigaddset(USR1), update won't be available",errno); if (sigaddset(&mask,SIGUSR2) == -1) throw cException(CLAMAV_TASK_NAME,"taskProc","sigaddset(USR2), update won't be available",errno); memset(&dbstat,0,sizeof(struct cl_stat)); cl_statinidir(cl_retdbdir(),&dbstat); do { sig = 0; sigwait(&mask,&sig); if (sig == SIGUSR1) { syslog(LOG_INFO,"cClamDBTask -> database update requested"); if (cl_statchkdir(&dbstat) == 1) { if ((err = pthread_mutex_lock(&(ptr->dbMutex)))) throw cException(CLAMAV_TASK_NAME,"taskProc","pthread_mutex_lock(dbMutex)",err); cl_engine_free(ptr->root); ptr->root = NULL; cl_load(cl_retdbdir(), ptr->root, &signo, CL_DB_STDOPT); cl_engine_compile(ptr->root); if ((err = pthread_mutex_unlock(&(ptr->dbMutex)))) throw cException(CLAMAV_TASK_NAME,"taskProc","pthread_mutex_unlock(dbMutex)",err,1); syslog(LOG_INFO,"cClamDBTask -> database updated"); cl_statfree(&dbstat); memset(&dbstat,0,sizeof(struct cl_stat)); cl_statinidir(cl_retdbdir(),&dbstat); } } } while (sig != SIGUSR2); } catch(cException c_e) { syslog(LOG_ERR,"%s",c_e.str); if (c_e.err_depth != -1) pthread_mutex_unlock(&(ptr->dbMutex)); } syslog(LOG_INFO,"cClamDBTask terminated"); return NULL; }