Re-add codename to mail banner.
[inoclam.git] / config.cxx
blobc9ce4c31a478915ade7ad20f91178033119d9e72
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 = cfg_true;
48 watch_dir = NULL;
49 smtp_host = NULL;
50 smtp_port = 0;
51 smtp_to = NULL;
52 smtp_from = NULL;
53 smtp_subject_virus = NULL;
54 smtp_subject_file = NULL;
57 /**
58 * Checks if the configuration file exists as a regular file.
59 * @return Returns 1 if the file exists and is regular, else 0.
61 int config::config_file_exists()
63 struct stat s;
65 /* No config file defined - NULL Pointer Check */
66 if (!DEFAULT_CONFIGFILE) {
67 return 0;
70 /* File exists? */
71 if (stat(DEFAULT_CONFIGFILE, &s) < 0) {
72 return 0;
75 /* File is a regular file? */
76 if (!S_ISREG(s.st_mode)) {
77 return 0;
80 return 1;
83 /**
84 * Release any resources that hold configuration information.
85 * This function effectively resets all configurable values.
86 * It should be called at the end of the program.
88 void config::config_free()
90 if (watch_dir != NULL) {
91 free(watch_dir);
92 watch_dir = NULL;
95 virus_removal_enabled = cfg_true;
97 smtp_enabled = cfg_true;
99 if (smtp_host != NULL) {
100 free(smtp_host);
101 smtp_host = NULL;
104 smtp_port = 0;
106 if (smtp_to != NULL) {
107 free(smtp_to);
108 smtp_to = NULL;
111 if (smtp_from != NULL) {
112 free(smtp_from);
113 smtp_from = NULL;
116 if (smtp_subject_virus != NULL) {
117 free(smtp_subject_virus);
118 smtp_subject_virus = NULL;
121 if (smtp_subject_file != NULL) {
122 free(smtp_subject_file);
123 smtp_subject_file = NULL;
129 * Examines each value of the current configuration.
130 * If a value was not set, it is set to the default.
132 void config::config_with_defaults()
134 if (watch_dir == NULL) {
135 watch_dir = strdup(WATCH_DIR);
138 if (smtp_host == NULL) {
139 smtp_host = strdup(SMTP_HOST);
142 if (smtp_port == 0) {
143 smtp_port = SMTP_PORT;
146 if (smtp_to == NULL) {
147 smtp_to = strdup(SMTP_TO);
150 if (smtp_from == NULL) {
151 smtp_from = strdup(SMTP_FROM);
154 if (smtp_subject_virus == NULL) {
155 smtp_subject_virus = strdup(SMTP_SUBJECT_VIRUS);
158 if (smtp_subject_file == NULL) {
159 smtp_subject_file = strdup(SMTP_SUBJECT_FILE);
165 * Parses an inoclam.conf configuration file.
167 void config::parse()
169 int rc;
171 config_free();
173 cfg_t *cfg;
174 cfg_opt_t opts[] = {
175 CFG_SIMPLE_STR("watch_dir", &watch_dir),
176 CFG_SIMPLE_BOOL("virus_removal_enabled", &virus_removal_enabled),
177 CFG_SIMPLE_BOOL("smtp_enabled", &smtp_enabled),
178 CFG_SIMPLE_STR("smtp_host", &smtp_host),
179 CFG_SIMPLE_INT("smtp_port", &smtp_port),
180 CFG_SIMPLE_STR("smtp_to", &smtp_to),
181 CFG_SIMPLE_STR("smtp_from", &smtp_from),
182 CFG_SIMPLE_STR("smtp_subject_virus", &smtp_subject_virus),
183 CFG_SIMPLE_STR("smtp_subject_file", &smtp_subject_file),
184 CFG_END()
187 cfg = NULL;
189 if (!config_file_exists()) {
190 daemon_log(LOG_INFO, "Configuration file '%s' not found.", DEFAULT_CONFIGFILE);
191 config_with_defaults();
192 return;
195 cfg = cfg_init(opts, 0);
196 if (!cfg) {
197 daemon_log(LOG_ERR, "cfg_init failed!");
198 config_with_defaults();
199 return;
202 rc = cfg_parse(cfg, DEFAULT_CONFIGFILE);
203 if (rc == CFG_PARSE_ERROR) {
204 daemon_log(LOG_ERR, "parser error '%s'", DEFAULT_CONFIGFILE);
207 if (cfg) {
208 cfg_free(cfg);
209 cfg = NULL;
212 config_with_defaults();
215 cfg_bool_t config::getSMTPEnabled()
217 return smtp_enabled;
220 cfg_bool_t config::getVirusRemovalEnabled()
222 return virus_removal_enabled;
225 char *config::getHost()
227 return smtp_host;
230 int config::getPort()
232 return smtp_port;
235 char *config::getTo()
237 return smtp_to;
240 char *config::getFrom()
242 return smtp_from;
245 char *config::getVirusSubject()
247 return smtp_subject_virus;
250 char *config::getFileSubject()
252 return smtp_subject_file;
255 char *config::getWatchDir()
257 return watch_dir;
261 * Deconstructor
263 config::~config()
265 config_free();