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 version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Tom Cort <tom.cort@state.vt.us>
20 * Matt Gagne <matt.gagne@state.vt.us>
25 #include <inotifytools/inotifytools.h>
26 #include <inotifytools/inotify.h>
27 #include <libdaemon/dlog.h>
36 #include "inotify.hxx"
37 #include "monitor.hxx"
42 * Watch the specified directory for changes and call contains_virus()
43 * @param conf configuration
45 void *inotify_main(void *v
)
47 inotify_main_args_t
*args
;
48 struct inotify_event
*event
;
51 args
= (inotify_main_args_t
*) v
;
56 if (args
== NULL
|| args
->conf
== NULL
|| args
->clamav
== NULL
|| args
->conf
->getDirectory() == NULL
) {
57 daemon_log(LOG_ERR
, "Invalid Arguments!");
67 if (!inotifytools_initialize() || !inotifytools_watch_recursively(args
->conf
->getDirectory(), IN_ALL_EVENTS
)) {
68 if (inotifytools_error() == ENOENT
) {
69 daemon_log(LOG_ERR
, "directory '%s' does not exist. Check /etc/inoclam.conf", args
->conf
->getDirectory());
71 daemon_log(LOG_ERR
, "Failed init inotify: %s", strerror(inotifytools_error()));
76 daemon_log(LOG_INFO
, "inotify watching '%s'", args
->conf
->getDirectory());
79 event
= inotifytools_next_event(3);
80 if (event
&& event
->name
&& event
->wd
) {
81 if ((event
->mask
& (IN_CLOSE_WRITE
| IN_MOVED_TO
)) && !(event
->mask
& IN_ISDIR
)) {
83 * Scan writtable files that are being closed.
84 * Scan files that have been moved to a watched directory.
85 * TODO: run the scan in a new thread.
88 std::string
* filename
;
89 filename
= new std::string();
90 filename
->append(inotifytools_filename_from_wd(event
->wd
), strlen(inotifytools_filename_from_wd(event
->wd
)));
91 filename
->append(event
->name
, strlen(event
->name
));
93 /* Perform the Virus Checking */
94 args
->clamav
->clam_scan(filename
, args
->conf
);
96 if (args
->conf
->getFileEMailEnabled() == cfg_true
) {
98 msg
= new std::string();
100 msg
->append("File: ");
101 msg
->append(filename
->c_str());
104 msg
->append("Date: ");
105 std::string
* tstamp
;
106 tstamp
= smtp_get_timestamp();
111 msg
->append(tstamp
->c_str());
115 std::string
* banner
;
116 banner
= smtp_get_banner();
117 msg
->append(banner
->c_str());
120 smtp_send(args
->conf
->getFileEMailSubject(), msg
, args
->conf
);
121 delete msg
; /* Clean up email msg */
124 delete filename
; /* Clean up filename */
125 } else if ((event
->mask
& (IN_CREATE
| IN_MOVED_TO
| IN_DELETE
)) && (event
->mask
& IN_ISDIR
)) {
127 * If the directory structure has changed, reload the watch list.
128 * Benchmark this to determine if this is too much of a performance hit.
130 inotifytools_cleanup();
132 if (!inotifytools_initialize() || !inotifytools_watch_recursively(args
->conf
->getDirectory(), IN_ALL_EVENTS
)) {
133 if (inotifytools_error() == ENOENT
) {
134 daemon_log(LOG_ERR
, "directory '%s' does not exist. Check /etc/inoclam.conf", args
->conf
->getDirectory());
136 daemon_log(LOG_ERR
, "Failed init inotify: %s", strerror(inotifytools_error()));
145 inotifytools_cleanup();