2 * inoclam - Inotify+ClamAV virus scanner
3 * Copyright (C) 2007 Vermont Department of Taxes
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Tom Cort <tom.cort@state.vt.us>
21 * Matt Gagne <matt.gagne@state.vt.us>
30 #include <libdaemon/dlog.h>
33 #include "inoclam.hxx"
34 #include "monitor.hxx"
42 * Thread that reloads virus definitions as needed
44 void *clam_refresh(void *v
)
48 struct cl_stat dbstat
;
53 memset(&dbstat
, 0, sizeof(struct cl_stat
));
54 cl_statinidir(cl_retdbdir(), &dbstat
);
57 if (cl_statchkdir(&dbstat
) == 1) {
58 struct cl_engine
*tmp_engine
= NULL
;
59 struct cl_engine
*old_engine
= NULL
;
61 daemon_log(LOG_INFO
, "Reloading new virus definitions");
63 /* TODO: make options configurable. */
64 /* For example: enable/disable CL_DB_NCORE, CL_DB_PHISHING_URLS, etc. */
66 /* Load virus definition files */
67 ret
= cl_load(cl_retdbdir(), &tmp_engine
, &sigs
, CL_DB_STDOPT
);
68 if (CL_SUCCESS
!= ret
) {
69 daemon_log(LOG_ERR
, "cl_load() error: %s", cl_strerror(ret
));
74 daemon_log(LOG_INFO
, "Virus definitions loaded (%d signatures).", sigs
);
76 /* prepare the detection engine */
77 ret
= cl_build(tmp_engine
);
78 if (CL_SUCCESS
!= ret
) {
79 daemon_log(LOG_ERR
, "cl_build() error: %s", cl_strerror(ret
));
85 /* Swap tmp_engine and engine, free resources from old engine */
86 pthread_mutex_lock(&(clamav
->engine_lock
));
87 old_engine
= clamav
->engine
;
88 clamav
->engine
= tmp_engine
;
90 daemon_log(LOG_INFO
, "Virus detection engine ready.");
91 pthread_mutex_unlock(&(clamav
->engine_lock
));
97 memset(&dbstat
, 0, sizeof(struct cl_stat
));
98 cl_statinidir(cl_retdbdir(), &dbstat
);
104 cl_statfree(&dbstat
);
107 clamav
->refresh_thread_alive
= false;
112 * Load the virus definition files and prepare the engine.
116 unsigned int sigs
= 0;
122 refresh_thread_alive
= false;
124 memset(&engine_lock
, '\0', sizeof(pthread_mutex_t
));
125 pthread_mutex_init(&engine_lock
, 0);
126 pthread_mutex_lock(&engine_lock
);
128 /* Load virus definition files */
129 ret
= cl_load(cl_retdbdir(), &engine
, &sigs
, CL_DB_STDOPT
);
130 if (CL_SUCCESS
!= ret
) {
131 pthread_mutex_unlock(&engine_lock
);
132 daemon_log(LOG_ERR
, "cl_load() error: %s", cl_strerror(ret
));
137 daemon_log(LOG_INFO
, "Virus definitions loaded (%d signatures).", sigs
);
139 /* prepare the detection engine */
140 ret
= cl_build(engine
);
141 if (CL_SUCCESS
!= ret
) {
142 pthread_mutex_unlock(&engine_lock
);
143 daemon_log(LOG_ERR
, "cl_build() error: %s", cl_strerror(ret
));
149 daemon_log(LOG_INFO
, "Virus detection engine ready.");
150 pthread_mutex_unlock(&engine_lock
);
152 refresh_thread_alive
= true;
154 pthread_attr_init(&ta
);
155 pthread_attr_setdetachstate(&ta
, PTHREAD_CREATE_DETACHED
);
156 ret
= pthread_create(&tt
, &ta
, clam_refresh
, (void *) this);
158 refresh_thread_alive
= false;
160 daemon_log(LOG_ERR
, "Can't create clam_refresh thread: %s", strerror(errno
));
165 * Scans a file for virus.
166 * @return -1 Error || 0 No Virus || +1 Virus Found
168 int clam::clam_scan(std::string
* filename
, config
* conf
)
171 struct cl_limits limits
;
174 pthread_mutex_lock(&engine_lock
);
176 memset(&limits
, 0, sizeof(struct cl_limits
));
178 limits
.maxfilesize
= 10 * 1048576;
179 limits
.maxreclevel
= 1;
180 limits
.maxmailrec
= 1;
181 limits
.maxratio
= 200;
183 /* TODO: make options configurable. */
184 /* For example: enable/disable CL_SCAN_BLOCKENCRYPTED, CL_SCAN_BLOCKMAX, CL_SCAN_OLE2, etc. */
186 ret
= cl_scanfile(filename
->c_str(), &virname
, NULL
, engine
, &limits
, CL_SCAN_STDOPT
);
187 if (CL_VIRUS
== ret
) {
188 int file_removed
= 0;
190 pthread_mutex_unlock(&engine_lock
);
191 daemon_log(LOG_INFO
, "%s: %s FOUND", filename
->c_str(), virname
);
193 if (conf
->getVirusRemovalEnabled() == cfg_true
) {
195 rc
= unlink(filename
->c_str());
199 daemon_log(LOG_ERR
, "unlink failed for '%s': %s", filename
->c_str(), strerror(errno
));
203 if (conf
->getSMTPEnabled() == cfg_true
) {
204 std::string
* smtp_body
;
212 /* get the timestamp */
214 tm
= localtime(&now
);
216 timestamp
= (char *) malloc(sizeof(char) * 41);
218 daemon_log(LOG_ERR
, "malloc() failed!");
221 memset(timestamp
, '\0', 41); /* RFC 2822 Date */
222 strftime(timestamp
, 40, "%a, %d %b %Y %H:%M:%S +0000", tm
);
225 * "File: <filename>\n"
226 * "Virus: <virname>\n"
227 * "Date: Thu, 28 Jun 2001 14:17:15 +0000\n"
228 * "Deleted: <Yes|No>\n"
230 * " (o_ Powered by: inoclam v1.0 (Bender)\n"
231 * " //\ Homepage: http://www.inoclam.org/\n"
232 * " V_/_ Author: Vermont Department of Taxes\n"
235 smtp_body
= new std::string();
236 smtp_body
->append("File: ");
237 smtp_body
->append(filename
->c_str());
238 smtp_body
->append("\n");
241 vname
= new std::string(virname
);
242 smtp_body
->append("Virus: ");
243 smtp_body
->append(vname
->c_str());
244 smtp_body
->append("\n");
247 std::string
* tstamp
;
248 tstamp
= new std::string(timestamp
);
249 smtp_body
->append("Date: ");
250 smtp_body
->append(tstamp
->c_str());
251 smtp_body
->append("\n");
254 smtp_body
->append("Deleted: ");
256 std::string
* rmstatus
;
258 if (file_removed
== 1) {
259 rmstatus
= new std::string("Yes");
261 rmstatus
= new std::string("No");
264 smtp_body
->append(rmstatus
->c_str());
267 smtp_body
->append("\n\n");
269 std::string
* version
;
270 version
= new std::string(VERSION
);
271 smtp_body
->append(" (o_ Powered by inoclam ");
272 smtp_body
->append(version
->c_str());
273 smtp_body
->append("\n");
276 smtp_body
->append(" //\\ Developed by the Vermont Department of Taxes\n");
277 smtp_body
->append(" V_/_ Project Homepage: http://www.inoclam.org/\n");
279 smtp_send(conf
->getVirusSubject(), smtp_body
, conf
);
281 delete smtp_body
; /* Clean up email string */
290 } else if (CL_CLEAN
== ret
) {
291 pthread_mutex_unlock(&engine_lock
);
292 daemon_log(LOG_INFO
, "%s: OK", filename
->c_str());
295 pthread_mutex_unlock(&engine_lock
);
296 daemon_log(LOG_ERR
, "Scan Error: %s (%s)", cl_strerror(ret
), filename
->c_str());
302 * Free resources used by the engine.
306 pthread_mutex_lock(&engine_lock
);
313 while (refresh_thread_alive
) {
318 pthread_mutex_unlock(&engine_lock
);
319 pthread_mutex_destroy(&engine_lock
);
320 pthread_attr_destroy(&ta
);