Version Bump
[inoclam.git] / config.cxx
blobbd00f3a478b322e9d828a891976d35d2074a4722
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>
23 #include <confuse.h>
24 #include <libdaemon/dlog.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <stdio.h>
35 #include "config.hxx"
36 #include "clam.hxx"
37 #include "inotify.hxx"
38 #include "smtp.hxx"
40 /**
41 * Checks if the configuration file exists as a regular file.
42 * @return Returns 1 if the file exists and is regular, else 0.
44 static int config_file_exists()
46 struct stat s;
48 /* No config file defined - NULL Pointer Check */
49 if (!DEFAULT_CONFIGFILE) {
50 return 0;
53 /* File exists? */
54 if (stat(DEFAULT_CONFIGFILE, &s) < 0) {
55 return 0;
58 /* File is a regular file? */
59 if (!S_ISREG(s.st_mode)) {
60 return 0;
63 return 1;
66 /**
67 * Release any resources that hold configuration information.
68 * This function effectively resets all configurable values.
69 * It should be called at the end of the program.
71 void config_free()
73 if (watch_dir != NULL) {
74 free(watch_dir);
75 watch_dir = NULL;
78 virus_removal_enabled = cfg_true;
80 smtp_enabled = cfg_true;
82 if (smtp_host != NULL) {
83 free(smtp_host);
84 smtp_host = NULL;
87 smtp_port = 0;
89 if (smtp_to != NULL) {
90 free(smtp_to);
91 smtp_to = NULL;
94 if (smtp_from != NULL) {
95 free(smtp_from);
96 smtp_from = NULL;
99 if (smtp_subject != NULL) {
100 free(smtp_subject);
101 smtp_subject = NULL;
106 * Examines each value of the current configuration.
107 * If a value was not set, it is set to the default.
109 static void config_with_defaults()
111 if (watch_dir == NULL) {
112 watch_dir = strdup(WATCH_DIR);
115 if (smtp_host == NULL) {
116 smtp_host = strdup(SMTP_HOST);
119 if (smtp_port == 0) {
120 smtp_port = SMTP_PORT;
123 if (smtp_to == NULL) {
124 smtp_to = strdup(SMTP_TO);
127 if (smtp_from == NULL) {
128 smtp_from = strdup(SMTP_FROM);
131 if (smtp_subject == NULL) {
132 smtp_subject = strdup(SMTP_SUBJECT);
137 * Parses an inoclam.conf configuration file.
139 void config_parse()
141 int rc;
143 config_free();
145 cfg_t *cfg;
146 cfg_opt_t opts[] = {
147 CFG_SIMPLE_STR("watch_dir", &watch_dir),
148 CFG_SIMPLE_BOOL("virus_removal_enabled", &virus_removal_enabled),
149 CFG_SIMPLE_BOOL("smtp_enabled", &smtp_enabled),
150 CFG_SIMPLE_STR("smtp_host", &smtp_host),
151 CFG_SIMPLE_INT("smtp_port", &smtp_port),
152 CFG_SIMPLE_STR("smtp_to", &smtp_to),
153 CFG_SIMPLE_STR("smtp_from", &smtp_from),
154 CFG_SIMPLE_STR("smtp_subject", &smtp_subject),
155 CFG_END()
158 cfg = NULL;
160 if (!config_file_exists()) {
161 daemon_log(LOG_INFO, "Configuration file '%s' not found.", DEFAULT_CONFIGFILE);
162 config_with_defaults();
163 return;
166 cfg = cfg_init(opts, 0);
167 if (!cfg) {
168 daemon_log(LOG_ERR, "cfg_init failed!");
169 config_with_defaults();
170 return;
173 rc = cfg_parse(cfg, DEFAULT_CONFIGFILE);
174 if (rc == CFG_PARSE_ERROR) {
175 daemon_log(LOG_ERR, "parser error '%s'", DEFAULT_CONFIGFILE);
178 if (cfg) {
179 cfg_free(cfg);
180 cfg = NULL;
183 config_with_defaults();