Version bump.
[inoclam.git] / src / inotify.cxx
blob68fff1d800f358c20261589308c8b82afa06966a
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 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
18 * Contributor(s):
19 * Tom Cort <tom.cort@state.vt.us>
20 * Matt Gagne <matt.gagne@state.vt.us>
23 #include <errno.h>
24 #include <idn-int.h>
25 #include <inotifytools/inotifytools.h>
26 #include <inotifytools/inotify.h>
27 #include <libdaemon/dlog.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include <string>
34 #include "config.hxx"
35 #include "clam.hxx"
36 #include "inotify.hxx"
37 #include "monitor.hxx"
38 #include "signal.hxx"
39 #include "smtp.hxx"
41 /**
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;
49 int length;
51 args = (inotify_main_args_t *) v;
53 event = NULL;
54 length = 0;
56 if (args == NULL || args->conf == NULL || args->clamav == NULL || args->conf->getDirectory() == NULL) {
57 daemon_log(LOG_ERR, "Invalid Arguments!");
58 exit_now = 1;
59 if (args) {
60 free(args);
61 args = NULL;
63 monitor_dec();
64 return NULL;
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());
70 } else {
71 daemon_log(LOG_ERR, "Failed init inotify: %s", strerror(inotifytools_error()));
73 exit_now = 1;
76 daemon_log(LOG_INFO, "inotify watching '%s'", args->conf->getDirectory());
78 while (!exit_now) {
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) {
97 std::string * msg;
98 msg = new std::string();
100 msg->append("File: ");
101 msg->append(filename->c_str());
102 msg->append("\n");
104 msg->append("Date: ");
105 std::string * tstamp;
106 tstamp = smtp_get_timestamp();
107 if (!tstamp) {
108 exit_now = 1;
109 break;
111 msg->append(tstamp->c_str());
112 delete tstamp;
114 msg->append("\n\n");
115 std::string * banner;
116 banner = smtp_get_banner();
117 msg->append(banner->c_str());
118 delete banner;
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());
135 } else {
136 daemon_log(LOG_ERR, "Failed init inotify: %s", strerror(inotifytools_error()));
138 exit_now = 1;
145 inotifytools_cleanup();
146 if (args) {
147 free(args);
148 args = NULL;
151 monitor_dec();
152 return NULL;