Update copyright year for 2009.
[inoclam.git] / src / config.cxx
blob011bc36f4c577c57ca0ae56dd22c2e8f9d143f12
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 <list>
20 #include <confuse.h>
21 #include <libdaemon/dlog.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdio.h>
32 #include "config.hxx"
33 #include "clam.hxx"
34 #include "inotify.hxx"
35 #include "smtp.hxx"
37 config::config()
39 directory = NULL;
40 file_email_subject = NULL;
41 virus_email_subject = NULL;
42 host = NULL;
43 from = NULL;
44 to = NULL;
47 void config::setDirectory(char *new_dir)
49 directory = new_dir;
52 char *config::getDirectory()
54 return directory;
57 void config::setFileEMailSubject(char *subject)
59 file_email_subject = subject;
62 char *config::getFileEMailSubject()
64 return file_email_subject;
67 void config::setVirusEMailSubject(char *subject)
69 virus_email_subject = subject;
72 char *config::getVirusEMailSubject()
74 return virus_email_subject;
77 void config::setFileEMailEnabled(cfg_bool_t enabled)
79 file_email_enabled = enabled;
82 cfg_bool_t config::getFileEMailEnabled()
84 return file_email_enabled;
87 void config::setVirusEMailEnabled(cfg_bool_t enabled)
89 virus_email_enabled = enabled;
93 cfg_bool_t config::getVirusEMailEnabled()
95 return virus_email_enabled;
98 void config::setVirusRemovalEnabled(cfg_bool_t enabled)
100 virus_removal_enabled = enabled;
103 cfg_bool_t config::getVirusRemovalEnabled()
105 return virus_removal_enabled;
108 char *config::getHost()
110 return host;
113 void config::setHost(char *h)
115 host = h;
118 int config::getPort()
120 return port;
123 void config::setPort(int p)
125 port = p;
128 char *config::getFrom()
130 return from;
133 void config::setFrom(char *f)
135 from = f;
138 std::list < char *>*config::getTo()
140 return to;
143 void config::setTo(std::list < char *>*t)
145 to = t;
148 config::~config()
150 if (to) {
151 for (std::list < char *>::iterator itr = to->begin(); itr != to->end(); ++itr) {
152 free(*itr);
154 delete to;
155 to = NULL;
157 if (directory) {
158 free(directory);
159 directory = NULL;
162 if (virus_email_subject) {
163 free(virus_email_subject);
164 virus_email_subject = NULL;
167 if (file_email_subject) {
168 free(file_email_subject);
169 file_email_subject = NULL;
172 if (host) {
173 free(host);
174 host = NULL;
177 if (from) {
178 free(from);
179 from = NULL;
184 * The configuration file path.
186 char *configfile;
189 * Parses an inoclam.conf configuration file.
191 std::list < config::config * >*config_parse()
193 std::list < config::config * >*conf_list;
194 conf_list = new std::list < config::config * >;
196 int n, i, j;
197 int rc;
199 cfg_t *cfg;
201 cfg_opt_t file_opts[] = {
202 CFG_BOOL((char*)"email_alert", cfg_false, CFGF_NONE),
203 CFG_STR((char*)"email_subject", (char*)"[inoclam] File Created/Changed", CFGF_NONE),
204 CFG_END()
207 cfg_opt_t virus_opts[] = {
208 CFG_BOOL((char*)"auto_remove", cfg_true, CFGF_NONE),
209 CFG_BOOL((char*)"email_alert", cfg_true, CFGF_NONE),
210 CFG_STR((char*)"email_subject", (char*)"[inoclam] Virus Detected", CFGF_NONE),
211 CFG_END()
214 cfg_opt_t smtp_opts[] = {
215 CFG_STR((char*)"host", (char*)"localhost", CFGF_NONE),
216 CFG_INT((char*)"port", 25, CFGF_NONE),
217 CFG_STR((char*)"from", (char*)"root@localhost", CFGF_NONE),
218 CFG_STR_LIST((char*)"to", (char*)"{root@localhost}", CFGF_NONE),
219 CFG_END()
222 cfg_opt_t watch_opts[] = {
223 CFG_STR((char*)"directory", (char*)"/tmp", CFGF_NONE),
224 CFG_SEC((char*)"file", file_opts, CFGF_NONE),
225 CFG_SEC((char*)"virus", virus_opts, CFGF_NONE),
226 CFG_SEC((char*)"smtp", smtp_opts, CFGF_NONE),
227 CFG_END()
230 cfg_opt_t opts[] = {
231 CFG_SEC((char*)"watch", watch_opts, CFGF_MULTI),
232 CFG_END()
235 cfg = NULL;
237 cfg = cfg_init(opts, 0);
238 if (!cfg) {
239 daemon_log(LOG_ERR, "cfg_init failed!");
240 return conf_list;
243 rc = cfg_parse(cfg, configfile);
244 if (rc == CFG_PARSE_ERROR) {
245 daemon_log(LOG_ERR, "parser error '%s'", configfile);
248 n = cfg_size(cfg, "watch");
249 for (i = 0; i < n; i++) {
250 config::config * conf;
251 conf = new config::config();
253 cfg_t *smtp_cfg = NULL;
254 cfg_t *file_cfg = NULL;
255 cfg_t *virus_cfg = NULL;
256 cfg_t *watch_cfg = cfg_getnsec(cfg, "watch", i);
258 conf->setDirectory(strdup(cfg_getstr(watch_cfg, "directory")));
260 file_cfg = cfg_getsec(watch_cfg, "file");
261 conf->setFileEMailSubject(strdup(cfg_getstr(file_cfg, "email_subject")));
262 conf->setFileEMailEnabled(cfg_getbool(file_cfg, "email_alert"));
264 virus_cfg = cfg_getsec(watch_cfg, "virus");
265 conf->setVirusEMailSubject(strdup(cfg_getstr(virus_cfg, "email_subject")));
266 conf->setVirusEMailEnabled(cfg_getbool(virus_cfg, "email_alert"));
267 conf->setVirusRemovalEnabled(cfg_getbool(virus_cfg, "auto_remove"));
269 smtp_cfg = cfg_getsec(watch_cfg, "smtp");
270 conf->setHost(strdup(cfg_getstr(smtp_cfg, "host")));
271 conf->setPort(cfg_getint(smtp_cfg, "port"));
272 conf->setFrom(strdup(cfg_getstr(smtp_cfg, "from")));
275 std::list < char *>*to;
276 to = new std::list < char *>;
277 for (j = 0; j < cfg_size(smtp_cfg, "to"); j++) {
278 to->push_front(strdup(cfg_getnstr(smtp_cfg, "to", j)));
280 conf->setTo(to);
282 conf_list->push_front(conf);
285 if (cfg) {
286 cfg_free(cfg);
287 cfg = NULL;
290 return conf_list;