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>
26 #include <inotifytools/inotifytools.h>
27 #include <inotifytools/inotify.h>
28 #include <libdaemon/dlog.h>
37 #include "inotify.hxx"
42 * Initializes inotify and tells it to 'watch' watch_dir.
43 * @param watch_dir the directory to watch.
44 * @return 1 for OK, -1 for failure.
46 static int __inotify_init(char *watch_dir
)
48 if (!inotifytools_initialize() || !inotifytools_watch_recursively(watch_dir
, IN_ALL_EVENTS
)) {
49 if (inotifytools_error() == ENOENT
) {
50 daemon_log(LOG_ERR
, "watch_dir '%s' does not exist. Check /etc/inoclam.conf", watch_dir
);
52 daemon_log(LOG_ERR
, "Failed init inotify: %s", strerror(inotifytools_error()));
56 /* daemon_log(LOG_DEBUG, "init inotify"); */
62 * Free resources used by inotify
64 static void __inotify_exit()
66 inotifytools_cleanup();
70 * Watch the specified directory for changes and call contains_virus()
71 * @param conf configuration
73 void inotify_main(config
* conf
)
75 struct inotify_event
*event
;
81 if (conf
->getWatchDir() == NULL
|| __inotify_init(conf
->getWatchDir()) == -1) {
86 daemon_log(LOG_INFO
, "inotify watching '%s'", conf
->getWatchDir());
88 while (!exit_now
&& (event
= inotifytools_next_event(-1))) {
89 if (event
&& event
->name
&& event
->wd
) {
90 if ((event
->mask
& (IN_CLOSE_WRITE
| IN_MOVED_TO
)) && !(event
->mask
& IN_ISDIR
)) {
92 * Scan writtable files that are being closed.
93 * Scan files that have been moved to a watched directory.
94 * TODO: run the scan in a new thread.
97 std::string
* filename
;
98 filename
= new std::string();
99 filename
->append(inotifytools_filename_from_wd(event
->wd
), strlen(inotifytools_filename_from_wd(event
->wd
)));
100 filename
->append(event
->name
, strlen(event
->name
));
102 /* Perform the Virus Checking */
103 clam_scan(filename
, conf
);
105 if (conf
->getSMTPEnabled() == cfg_true
) {
107 msg
= new std::string("Testing file notification");
108 smtp_send(conf
->getFileSubject(), msg
, conf
);
109 delete msg
; /* Clean up email msg */
112 delete filename
; /* Clean up filename */
113 } else if ((event
->mask
& (IN_CREATE
| IN_MOVED_TO
| IN_DELETE
)) && (event
->mask
& IN_ISDIR
)) {
115 * If the directory structure has changed, reload the watch list.
116 * Benchmark this to determine if this is too much of a performance hit.
119 __inotify_init(conf
->getWatchDir());