c -> c++
[inoclam.git] / clam.cxx
blobc450ea82693a00483ef3697f5f3a888d6fd818fd
1 /*
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
19 * Contributor(s):
20 * Tom Cort <tom.cort@state.vt.us>
23 #include <unistd.h>
24 #include <clamav.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <libdaemon/dlog.h>
29 #include <pthread.h>
31 #include "clam.hxx"
32 #include "monitor.hxx"
33 #include "signal.hxx"
35 /**
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.
39 * @see clam_refresh()
40 * @see contains_virus()
42 pthread_mutex_t engine_lock;
44 /**
45 * Thread attributes used by the clam_refresh() thread. This is
46 * a global so that main() can free them when its cleaning up.
47 * @see main()
48 * @see clam_refresh()
50 pthread_attr_t ta;
52 /**
53 * Multiple threads are using and altering "engine".
54 * Use the engine_lock to prevent concurrency issues.
56 struct cl_engine *engine = NULL;
58 /**
59 * Load the virus definition files and prepare the engine.
61 void clam_init()
63 unsigned int sigs = 0;
64 int ret;
66 pthread_t tt;
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));
78 engine = NULL;
79 return;
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));
89 cl_free(engine);
90 engine = NULL;
91 return;
94 daemon_log(LOG_INFO, "(%s:%u) Virus detection engine ready.", __FILE__, __LINE__);
95 pthread_mutex_unlock(&engine_lock);
97 monitor_inc();
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);
101 if (ret) {
102 monitor_dec();
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
110 void clam_refresh()
112 unsigned int sigs;
113 int ret;
114 struct cl_stat dbstat;
116 memset(&dbstat, 0, sizeof(struct cl_stat));
117 cl_statinidir(cl_retdbdir(), &dbstat);
119 do {
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));
130 tmp_engine = NULL;
131 continue;
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));
140 cl_free(tmp_engine);
141 tmp_engine = NULL;
142 continue;
145 /* Swap tmp_engine and engine, free resources from old engine */
146 pthread_mutex_lock(&engine_lock);
147 old_engine = engine;
148 engine = tmp_engine;
149 tmp_engine = NULL;
150 daemon_log(LOG_INFO, "(%s:%u) Virus detection engine ready.", __FILE__, __LINE__);
151 pthread_mutex_unlock(&engine_lock);
153 cl_free(old_engine);
154 old_engine = NULL;
156 cl_statfree(&dbstat);
157 memset(&dbstat, 0, sizeof(struct cl_stat));
158 cl_statinidir(cl_retdbdir(), &dbstat);
161 sleep(10);
162 } while (!exit_now);
164 cl_statfree(&dbstat);
166 monitor_dec();
167 /* pthread_exit(NULL);
168 */ return;
172 * Scans a file for virus.
173 * @return -1 Error || 0 No Virus || +1 Virus Found
175 int contains_virus(char *filename)
177 int ret;
178 struct cl_limits limits;
179 const char *virname;
181 pthread_mutex_lock(&engine_lock);
183 memset(&limits, 0, sizeof(struct cl_limits));
184 limits.maxfiles = 1;
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);
194 return 1;
195 } else if (CL_CLEAN == ret) {
196 pthread_mutex_unlock(&engine_lock);
197 daemon_log(LOG_INFO, "(%s:%u) %s: OK", __FILE__, __LINE__, filename);
198 return 0;
199 } else {
200 pthread_mutex_unlock(&engine_lock);
201 daemon_log(LOG_ERR, "(%s:%u) Scan Error: %s (%s)", __FILE__, __LINE__, cl_strerror(ret), filename);
202 return -1;
207 * Free resources used by the engine.
209 void clam_exit()
211 pthread_mutex_lock(&engine_lock);
213 if (engine) {
214 cl_free(engine);
215 engine = NULL;
218 pthread_mutex_unlock(&engine_lock);