Update copyright year for 2009.
[inoclam.git] / src / inotify.cxx
blobce9c7dcd2516881748486129ec265adf040fa71b
1 /*
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
19 #include <errno.h>
20 #include <idn-int.h>
21 #include <inotifytools/inotifytools.h>
22 #include <inotifytools/inotify.h>
23 #include <libdaemon/dlog.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include <string>
30 #include "config.hxx"
31 #include "clam.hxx"
32 #include "inotify.hxx"
33 #include "monitor.hxx"
34 #include "signal.hxx"
35 #include "smtp.hxx"
37 /**
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;
45 int length;
47 args = (inotify_main_args_t *) v;
49 event = NULL;
50 length = 0;
52 if (args == NULL || args->conf == NULL || args->clamav == NULL || args->conf->getDirectory() == NULL) {
53 daemon_log(LOG_ERR, "Invalid Arguments!");
54 exit_now = 1;
55 if (args) {
56 free(args);
57 args = NULL;
59 monitor_dec();
60 return NULL;
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());
66 } else {
67 daemon_log(LOG_ERR, "Failed init inotify: %s", strerror(inotifytools_error()));
69 exit_now = 1;
72 daemon_log(LOG_INFO, "inotify watching '%s'", args->conf->getDirectory());
74 while (!exit_now) {
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) {
93 std::string * msg;
94 msg = new std::string();
96 msg->append("File: ");
97 msg->append(filename->c_str());
98 msg->append("\n");
100 msg->append("Date: ");
101 std::string * tstamp;
102 tstamp = smtp_get_timestamp();
103 if (!tstamp) {
104 exit_now = 1;
105 break;
107 msg->append(tstamp->c_str());
108 delete tstamp;
110 msg->append("\n\n");
111 std::string * banner;
112 banner = smtp_get_banner();
113 msg->append(banner->c_str());
114 delete banner;
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());
131 } else {
132 daemon_log(LOG_ERR, "Failed init inotify: %s", strerror(inotifytools_error()));
134 exit_now = 1;
141 inotifytools_cleanup();
142 if (args) {
143 free(args);
144 args = NULL;
147 monitor_dec();
148 return NULL;