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>
28 #include <libdaemon/dlog.h>
32 #include "monitor.hxx"
36 * A lock used to serialize access to the engine. Serialized access is
37 * needed to keep clam_refresh() from changing the engine while
38 * contains_virus() is using it.
40 * @see contains_virus()
42 pthread_mutex_t engine_lock
;
45 * Thread attributes used by the clam_refresh() thread. This is
46 * a global so that main() can free them when its cleaning up.
53 * Multiple threads are using and altering "engine".
54 * Use the engine_lock to prevent concurrency issues.
56 struct cl_engine
*engine
= NULL
;
59 * Load the virus definition files and prepare the engine.
63 unsigned int sigs
= 0;
68 memset(&engine_lock
, '\0', sizeof(pthread_mutex_t
));
69 pthread_mutex_init(&engine_lock
, 0);
71 pthread_mutex_lock(&engine_lock
);
73 /* Load virus definition files */
74 ret
= cl_load(cl_retdbdir(), &engine
, &sigs
, CL_DB_STDOPT
);
75 if (CL_SUCCESS
!= ret
) {
76 pthread_mutex_unlock(&engine_lock
);
77 daemon_log(LOG_ERR
, "(%s:%u) cl_load() error: %s", __FILE__
, __LINE__
, cl_strerror(ret
));
82 daemon_log(LOG_INFO
, "(%s:%u) Virus definitions loaded (%d signatures).", __FILE__
, __LINE__
, sigs
);
84 /* prepare the detection engine */
85 ret
= cl_build(engine
);
86 if (CL_SUCCESS
!= ret
) {
87 pthread_mutex_unlock(&engine_lock
);
88 daemon_log(LOG_ERR
, "(%s:%u) cl_build() error: %s", __FILE__
, __LINE__
, cl_strerror(ret
));
94 daemon_log(LOG_INFO
, "(%s:%u) Virus detection engine ready.", __FILE__
, __LINE__
);
95 pthread_mutex_unlock(&engine_lock
);
98 pthread_attr_init(&ta
);
99 pthread_attr_setdetachstate(&ta
, PTHREAD_CREATE_DETACHED
);
100 ret
= pthread_create(&tt
, &ta
, (void *(*)(void *)) clam_refresh
, (void *) NULL
);
103 daemon_log(LOG_ERR
, "(%s:%u) Can't create clam_refresh thread: %s", __FILE__
, __LINE__
, strerror(errno
));
108 * Thread that reloads virus definitions as needed
114 struct cl_stat dbstat
;
116 memset(&dbstat
, 0, sizeof(struct cl_stat
));
117 cl_statinidir(cl_retdbdir(), &dbstat
);
120 if (cl_statchkdir(&dbstat
) == 1) {
121 struct cl_engine
*tmp_engine
= NULL
;
122 struct cl_engine
*old_engine
= NULL
;
124 daemon_log(LOG_INFO
, "(%s:%u) Reloading new virus definitions", __FILE__
, __LINE__
);
126 /* Load virus definition files */
127 ret
= cl_load(cl_retdbdir(), &tmp_engine
, &sigs
, CL_DB_STDOPT
);
128 if (CL_SUCCESS
!= ret
) {
129 daemon_log(LOG_ERR
, "(%s:%u) cl_load() error: %s", __FILE__
, __LINE__
, cl_strerror(ret
));
134 daemon_log(LOG_INFO
, "(%s:%u) Virus definitions loaded (%d signatures).", __FILE__
, __LINE__
, sigs
);
136 /* prepare the detection engine */
137 ret
= cl_build(tmp_engine
);
138 if (CL_SUCCESS
!= ret
) {
139 daemon_log(LOG_ERR
, "(%s:%u) cl_build() error: %s", __FILE__
, __LINE__
, cl_strerror(ret
));
145 /* Swap tmp_engine and engine, free resources from old engine */
146 pthread_mutex_lock(&engine_lock
);
150 daemon_log(LOG_INFO
, "(%s:%u) Virus detection engine ready.", __FILE__
, __LINE__
);
151 pthread_mutex_unlock(&engine_lock
);
156 cl_statfree(&dbstat
);
157 memset(&dbstat
, 0, sizeof(struct cl_stat
));
158 cl_statinidir(cl_retdbdir(), &dbstat
);
164 cl_statfree(&dbstat
);
167 /* pthread_exit(NULL);
172 * Scans a file for virus.
173 * @return -1 Error || 0 No Virus || +1 Virus Found
175 int contains_virus(char *filename
)
178 struct cl_limits limits
;
181 pthread_mutex_lock(&engine_lock
);
183 memset(&limits
, 0, sizeof(struct cl_limits
));
185 limits
.maxfilesize
= 10 * 1048576;
186 limits
.maxreclevel
= 1;
187 limits
.maxmailrec
= 1;
188 limits
.maxratio
= 200;
190 ret
= cl_scanfile(filename
, &virname
, NULL
, engine
, &limits
, CL_SCAN_STDOPT
);
191 if (CL_VIRUS
== ret
) {
192 pthread_mutex_unlock(&engine_lock
);
193 daemon_log(LOG_INFO
, "(%s:%u) %s: %s FOUND", __FILE__
, __LINE__
, filename
, virname
);
195 } else if (CL_CLEAN
== ret
) {
196 pthread_mutex_unlock(&engine_lock
);
197 daemon_log(LOG_INFO
, "(%s:%u) %s: OK", __FILE__
, __LINE__
, filename
);
200 pthread_mutex_unlock(&engine_lock
);
201 daemon_log(LOG_ERR
, "(%s:%u) Scan Error: %s (%s)", __FILE__
, __LINE__
, cl_strerror(ret
), filename
);
207 * Free resources used by the engine.
211 pthread_mutex_lock(&engine_lock
);
218 pthread_mutex_unlock(&engine_lock
);