Document the tagging and tarball creation process.
[inoclam.git] / config.cxx
blobdc91b59afdf59f1d329ea8373bd0df48077be3be
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 <confuse.h>
25 #include <libdaemon/dlog.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <stdio.h>
36 #include "config.hxx"
37 #include "clam.hxx"
38 #include "inotify.hxx"
39 #include "smtp.hxx"
41 /**
42 * Default Constructor
44 config::config()
46 virus_removal_enabled = cfg_false;
47 smtp_enabled_virus = cfg_true;
48 smtp_enabled_file = cfg_false;
49 watch_dir = NULL;
50 smtp_host = NULL;
51 smtp_port = 0;
52 smtp_to = NULL;
53 smtp_from = NULL;
54 smtp_subject_virus = NULL;
55 smtp_subject_file = NULL;
58 /**
59 * Checks if the configuration file exists as a regular file.
60 * @return Returns 1 if the file exists and is regular, else 0.
62 int config::config_file_exists()
64 struct stat s;
66 /* No config file defined - NULL Pointer Check */
67 if (!DEFAULT_CONFIGFILE) {
68 return 0;
71 /* File exists? */
72 if (stat(DEFAULT_CONFIGFILE, &s) < 0) {
73 return 0;
76 /* File is a regular file? */
77 if (!S_ISREG(s.st_mode)) {
78 return 0;
81 return 1;
84 /**
85 * Release any resources that hold configuration information.
86 * This function effectively resets all configurable values.
87 * It should be called at the end of the program.
89 void config::config_free()
91 if (watch_dir != NULL) {
92 free(watch_dir);
93 watch_dir = NULL;
96 virus_removal_enabled = cfg_true;
98 smtp_enabled_virus = cfg_true;
100 smtp_enabled_file = cfg_true;
102 if (smtp_host != NULL) {
103 free(smtp_host);
104 smtp_host = NULL;
107 smtp_port = 0;
109 if (smtp_to != NULL) {
110 free(smtp_to);
111 smtp_to = NULL;
114 if (smtp_from != NULL) {
115 free(smtp_from);
116 smtp_from = NULL;
119 if (smtp_subject_virus != NULL) {
120 free(smtp_subject_virus);
121 smtp_subject_virus = NULL;
124 if (smtp_subject_file != NULL) {
125 free(smtp_subject_file);
126 smtp_subject_file = NULL;
132 * Examines each value of the current configuration.
133 * If a value was not set, it is set to the default.
135 void config::config_with_defaults()
137 if (watch_dir == NULL) {
138 watch_dir = strdup(WATCH_DIR);
141 if (smtp_host == NULL) {
142 smtp_host = strdup(SMTP_HOST);
145 if (smtp_port == 0) {
146 smtp_port = SMTP_PORT;
149 if (smtp_to == NULL) {
150 smtp_to = strdup(SMTP_TO);
153 if (smtp_from == NULL) {
154 smtp_from = strdup(SMTP_FROM);
157 if (smtp_subject_virus == NULL) {
158 smtp_subject_virus = strdup(SMTP_SUBJECT_VIRUS);
161 if (smtp_subject_file == NULL) {
162 smtp_subject_file = strdup(SMTP_SUBJECT_FILE);
168 * Parses an inoclam.conf configuration file.
170 void config::parse()
172 int rc;
174 config_free();
176 cfg_t *cfg;
177 cfg_opt_t opts[] = {
178 CFG_SIMPLE_STR("watch_dir", &watch_dir),
179 CFG_SIMPLE_BOOL("virus_removal_enabled", &virus_removal_enabled),
180 CFG_SIMPLE_BOOL("smtp_enabled_file", &smtp_enabled_file),
181 CFG_SIMPLE_BOOL("smtp_enabled_virus", &smtp_enabled_virus),
182 CFG_SIMPLE_STR("smtp_host", &smtp_host),
183 CFG_SIMPLE_INT("smtp_port", &smtp_port),
184 CFG_SIMPLE_STR("smtp_to", &smtp_to),
185 CFG_SIMPLE_STR("smtp_from", &smtp_from),
186 CFG_SIMPLE_STR("smtp_subject_virus", &smtp_subject_virus),
187 CFG_SIMPLE_STR("smtp_subject_file", &smtp_subject_file),
188 CFG_END()
191 cfg = NULL;
193 if (!config_file_exists()) {
194 daemon_log(LOG_INFO, "Configuration file '%s' not found.", DEFAULT_CONFIGFILE);
195 config_with_defaults();
196 return;
199 cfg = cfg_init(opts, 0);
200 if (!cfg) {
201 daemon_log(LOG_ERR, "cfg_init failed!");
202 config_with_defaults();
203 return;
206 rc = cfg_parse(cfg, DEFAULT_CONFIGFILE);
207 if (rc == CFG_PARSE_ERROR) {
208 daemon_log(LOG_ERR, "parser error '%s'", DEFAULT_CONFIGFILE);
211 if (cfg) {
212 cfg_free(cfg);
213 cfg = NULL;
216 config_with_defaults();
219 cfg_bool_t config::isSMTPEnabledFile()
221 return smtp_enabled_file;
224 cfg_bool_t config::isSMTPEnabledVirus()
226 return smtp_enabled_virus;
229 cfg_bool_t config::getVirusRemovalEnabled()
231 return virus_removal_enabled;
234 char *config::getHost()
236 return smtp_host;
239 int config::getPort()
241 return smtp_port;
244 char *config::getTo()
246 return smtp_to;
249 char *config::getFrom()
251 return smtp_from;
254 char *config::getVirusSubject()
256 return smtp_subject_virus;
259 char *config::getFileSubject()
261 return smtp_subject_file;
264 char *config::getWatchDir()
266 return watch_dir;
270 * Deconstructor
272 config::~config()
274 config_free();