2 * inoclam - Inotify+ClamAV virus scanner
3 * Copyright (C) 2007, 2008, 2009 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
21 #include <inotifytools/inotifytools.h>
22 #include <inotifytools/inotify.h>
23 #include <libdaemon/dlog.h>
32 #include "inotify.hxx"
33 #include "monitor.hxx"
38 * Watch the specified directory for changes and call contains_virus()
39 * @param conf configuration
41 void *inotify_main(void *v
)
43 inotify_main_args_t
*args
;
44 struct inotify_event
*event
;
47 args
= (inotify_main_args_t
*) v
;
52 if (args
== NULL
|| args
->conf
== NULL
|| args
->clamav
== NULL
|| args
->conf
->getDirectory() == NULL
) {
53 daemon_log(LOG_ERR
, "Invalid Arguments!");
63 if (!inotifytools_initialize() || !inotifytools_watch_recursively(args
->conf
->getDirectory(), IN_ALL_EVENTS
)) {
64 if (inotifytools_error() == ENOENT
) {
65 daemon_log(LOG_ERR
, "directory '%s' does not exist. Check /etc/inoclam.conf", args
->conf
->getDirectory());
67 daemon_log(LOG_ERR
, "Failed init inotify: %s", strerror(inotifytools_error()));
72 daemon_log(LOG_INFO
, "inotify watching '%s'", args
->conf
->getDirectory());
75 event
= inotifytools_next_event(3);
76 if (event
&& event
->name
&& event
->wd
) {
77 if ((event
->mask
& (IN_CLOSE_WRITE
| IN_MOVED_TO
)) && !(event
->mask
& IN_ISDIR
)) {
79 * Scan writtable files that are being closed.
80 * Scan files that have been moved to a watched directory.
81 * TODO: run the scan in a new thread.
84 std::string
* filename
;
85 filename
= new std::string();
86 filename
->append(inotifytools_filename_from_wd(event
->wd
), strlen(inotifytools_filename_from_wd(event
->wd
)));
87 filename
->append(event
->name
, strlen(event
->name
));
89 /* Perform the Virus Checking */
90 args
->clamav
->clam_scan(filename
, args
->conf
);
92 if (args
->conf
->getFileEMailEnabled() == cfg_true
) {
94 msg
= new std::string();
96 msg
->append("File: ");
97 msg
->append(filename
->c_str());
100 msg
->append("Date: ");
101 std::string
* tstamp
;
102 tstamp
= smtp_get_timestamp();
107 msg
->append(tstamp
->c_str());
111 std::string
* banner
;
112 banner
= smtp_get_banner();
113 msg
->append(banner
->c_str());
116 smtp_send(args
->conf
->getFileEMailSubject(), msg
, args
->conf
);
117 delete msg
; /* Clean up email msg */
120 delete filename
; /* Clean up filename */
121 } else if ((event
->mask
& (IN_CREATE
| IN_MOVED_TO
| IN_DELETE
)) && (event
->mask
& IN_ISDIR
)) {
123 * If the directory structure has changed, reload the watch list.
124 * Benchmark this to determine if this is too much of a performance hit.
126 inotifytools_cleanup();
128 if (!inotifytools_initialize() || !inotifytools_watch_recursively(args
->conf
->getDirectory(), IN_ALL_EVENTS
)) {
129 if (inotifytools_error() == ENOENT
) {
130 daemon_log(LOG_ERR
, "directory '%s' does not exist. Check /etc/inoclam.conf", args
->conf
->getDirectory());
132 daemon_log(LOG_ERR
, "Failed init inotify: %s", strerror(inotifytools_error()));
141 inotifytools_cleanup();