Enhance config file format.
[inoclam.git] / src / config.cxx
blob4fa9025b32fd3496ff182b86afb2b13962633cca
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 <list>
25 #include <confuse.h>
26 #include <libdaemon/dlog.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
37 #include "config.hxx"
38 #include "clam.hxx"
39 #include "inotify.hxx"
40 #include "smtp.hxx"
42 config::config() {
43 directory = NULL;
44 file_email_subject = NULL;
45 virus_email_subject = NULL;
46 host = NULL;
47 from = NULL;
48 to = NULL;
51 void config::setDirectory(char *new_dir) {
52 directory = new_dir;
55 char *config::getDirectory() {
56 return directory;
59 void config::setFileEMailSubject(char *subject) {
60 file_email_subject = subject;
63 char *config::getFileEMailSubject() {
64 return file_email_subject;
67 void config::setVirusEMailSubject(char *subject) {
68 virus_email_subject = subject;
71 char *config::getVirusEMailSubject() {
72 return virus_email_subject;
75 void config::setFileEMailEnabled(cfg_bool_t enabled) {
76 file_email_enabled = enabled;
79 cfg_bool_t config::getFileEMailEnabled() {
80 return file_email_enabled;
83 void config::setVirusEMailEnabled(cfg_bool_t enabled) {
84 virus_email_enabled = enabled;
88 cfg_bool_t config::getVirusEMailEnabled() {
89 return virus_email_enabled;
92 void config::setVirusRemovalEnabled(cfg_bool_t enabled) {
93 virus_removal_enabled = enabled;
96 cfg_bool_t config::getVirusRemovalEnabled() {
97 return virus_removal_enabled;
100 char *config::getHost() {
101 return host;
104 void config::setHost(char *h) {
105 host = h;
108 int config::getPort() {
109 return port;
112 void config::setPort(int p) {
113 port = p;
116 char *config::getFrom() {
117 return from;
120 void config::setFrom(char *f) {
121 from = f;
124 std::list<char*> *config::getTo() {
125 return to;
128 void config::setTo(std::list<char*> *t) {
129 to = t;
132 config::~config() {
133 if (to) {
134 for (std::list<char*>::iterator itr = to->begin(); itr != to->end(); ++itr) {
135 free(*itr);
137 delete to;
138 to = NULL;
140 if (directory) {
141 free(directory);
142 directory = NULL;
145 if (virus_email_subject) {
146 free(virus_email_subject);
147 virus_email_subject = NULL;
150 if (file_email_subject) {
151 free(file_email_subject);
152 file_email_subject = NULL;
155 if (host) {
156 free(host);
157 host = NULL;
160 if (from) {
161 free(from);
162 from = NULL;
167 * Parses an inoclam.conf configuration file.
169 std::list<config::config*> *config_parse()
171 std::list<config::config*> *conf_list;
172 conf_list = new std::list<config::config*>;
174 int n, i, j;
175 int rc;
177 cfg_t *cfg;
179 cfg_opt_t file_opts[] = {
180 CFG_BOOL("email_alert", cfg_false, CFGF_NONE),
181 CFG_STR("email_subject", "[inoclam] File Created/Changed", CFGF_NONE),
182 CFG_END()
185 cfg_opt_t virus_opts[] = {
186 CFG_BOOL("auto_remove", cfg_true, CFGF_NONE),
187 CFG_BOOL("email_alert", cfg_true, CFGF_NONE),
188 CFG_STR("email_subject", "[inoclam] Virus Detected", CFGF_NONE),
189 CFG_END()
192 cfg_opt_t smtp_opts[] = {
193 CFG_STR("host", "localhost", CFGF_NONE),
194 CFG_INT("port", 25, CFGF_NONE),
195 CFG_STR("from", "root@localhost", CFGF_NONE),
196 CFG_STR_LIST("to", "{root@localhost}", CFGF_NONE),
197 CFG_END()
200 cfg_opt_t watch_opts[] = {
201 CFG_STR("directory", "/tmp", CFGF_NONE),
202 CFG_SEC("file", file_opts, CFGF_NONE),
203 CFG_SEC("virus", virus_opts, CFGF_NONE),
204 CFG_SEC("smtp", smtp_opts, CFGF_NONE),
205 CFG_END()
208 cfg_opt_t opts[] = {
209 CFG_SEC("watch", watch_opts, CFGF_MULTI),
210 CFG_END()
213 cfg = NULL;
215 cfg = cfg_init(opts, 0);
216 if (!cfg) {
217 daemon_log(LOG_ERR, "cfg_init failed!");
218 return conf_list;
221 rc = cfg_parse(cfg, DEFAULT_CONFIGFILE);
222 if (rc == CFG_PARSE_ERROR) {
223 daemon_log(LOG_ERR, "parser error '%s'", DEFAULT_CONFIGFILE);
226 n = cfg_size(cfg, "watch");
227 for(i = 0; i < n; i++) {
228 config::config *conf;
229 conf = new config::config();
231 cfg_t *smtp_cfg = NULL;
232 cfg_t *file_cfg = NULL;
233 cfg_t *virus_cfg = NULL;
234 cfg_t *watch_cfg = cfg_getnsec(cfg, "watch", i);
236 conf->setDirectory(strdup(cfg_getstr(watch_cfg, "directory")));
238 file_cfg = cfg_getsec(watch_cfg, "file");
239 conf->setFileEMailSubject(strdup(cfg_getstr(file_cfg, "email_subject")));
240 conf->setFileEMailEnabled(cfg_getbool(file_cfg, "email_alert"));
242 virus_cfg = cfg_getsec(watch_cfg, "virus");
243 conf->setVirusEMailSubject(strdup(cfg_getstr(virus_cfg, "email_subject")));
244 conf->setVirusEMailEnabled(cfg_getbool(virus_cfg, "email_alert"));
245 conf->setVirusRemovalEnabled(cfg_getbool(virus_cfg, "auto_remove"));
247 smtp_cfg = cfg_getsec(watch_cfg, "smtp");
248 conf->setHost(strdup(cfg_getstr(smtp_cfg, "host")));
249 conf->setPort(cfg_getint(smtp_cfg, "port"));
250 conf->setFrom(strdup(cfg_getstr(smtp_cfg, "from")));
253 std::list<char*> *to;
254 to = new std::list<char*>;
255 for (j = 0; j < cfg_size(smtp_cfg, "to"); j++) {
256 to->push_front(strdup(cfg_getnstr(smtp_cfg, "to", j)));
258 conf->setTo(to);
260 conf_list->push_front(conf);
263 if (cfg) {
264 cfg_free(cfg);
265 cfg = NULL;
268 return conf_list;