Add a cheat sheet for inoclam developers.
[inoclam.git] / inotify.cxx
blobd1b7581bc92df70b5b77f08f8e6e151ab73cbafc
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 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
19 * Contributor(s):
20 * Tom Cort <tom.cort@state.vt.us>
21 * Matt Gagne <matt.gagne@state.vt.us>
24 #include <errno.h>
25 #include <idn-int.h>
26 #include <inotifytools/inotifytools.h>
27 #include <inotifytools/inotify.h>
28 #include <libdaemon/dlog.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include <string>
35 #include "config.hxx"
36 #include "clam.hxx"
37 #include "inotify.hxx"
38 #include "signal.hxx"
39 #include "smtp.hxx"
41 /**
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);
51 } else {
52 daemon_log(LOG_ERR, "Failed init inotify: %s", strerror(inotifytools_error()));
54 return -1;
55 } else {
56 /* daemon_log(LOG_DEBUG, "init inotify"); */
57 return 1;
61 /**
62 * Free resources used by inotify
64 static void __inotify_exit()
66 inotifytools_cleanup();
69 /**
70 * Watch the specified directory for changes and call contains_virus()
71 * @param conf configuration
73 void inotify_main(config * conf, clam * clamav)
75 struct inotify_event *event;
76 int length;
78 event = NULL;
79 length = 0;
81 if (conf->getWatchDir() == NULL || __inotify_init(conf->getWatchDir()) == -1) {
82 exit_now = 1;
83 return;
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 clamav->clam_scan(filename, conf);
105 if (conf->isSMTPEnabledFile() == cfg_true) {
106 std::string * msg;
107 msg = new std::string();
109 msg->append("File: ");
110 msg->append(filename->c_str());
111 msg->append("\n");
113 msg->append("Date: ");
114 std::string * tstamp;
115 tstamp = smtp_get_timestamp();
116 if (!tstamp) {
117 exit_now = 1;
118 break;
120 msg->append(tstamp->c_str());
121 delete tstamp;
123 msg->append("\n\n");
124 std::string * banner;
125 banner = smtp_get_banner();
126 msg->append(banner->c_str());
127 delete banner;
129 smtp_send(conf->getFileSubject(), msg, conf);
130 delete msg; /* Clean up email msg */
133 delete filename; /* Clean up filename */
134 } else if ((event->mask & (IN_CREATE | IN_MOVED_TO | IN_DELETE)) && (event->mask & IN_ISDIR)) {
136 * If the directory structure has changed, reload the watch list.
137 * Benchmark this to determine if this is too much of a performance hit.
139 __inotify_exit();
140 __inotify_init(conf->getWatchDir());
146 __inotify_exit();