Add pthread checks to CMakeLists.txt
[inoclam.git] / config.cxx
blobf2ce3b40a9349d2fc8a0e83cdbc39341d86a60bd
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 "inotify.hxx"
37 #include "smtp.hxx"
39 /**
40 * Checks if the configuration file exists as a regular file.
41 * @return Returns 1 if the file exists and is regular, else 0.
43 static int config_file_exists()
45 struct stat s;
47 /* No config file defined - NULL Pointer Check */
48 if (!DEFAULT_CONFIGFILE) {
49 return 0;
52 /* File exists? */
53 if (stat(DEFAULT_CONFIGFILE, &s) < 0) {
54 return 0;
57 /* File is a regular file? */
58 if (!S_ISREG(s.st_mode)) {
59 return 0;
62 return 1;
65 /**
66 * Release any resources that hold configuration information.
67 * This function effectively resets all configurable values.
68 * It should be called at the end of the program.
70 void config_free()
72 if (watch_dir != NULL) {
73 free(watch_dir);
74 watch_dir = NULL;
77 virus_removal_enabled = cfg_true;
79 smtp_enabled = cfg_true;
81 if (smtp_host != NULL) {
82 free(smtp_host);
83 smtp_host = NULL;
86 smtp_port = 0;
88 if (smtp_to != NULL) {
89 free(smtp_to);
90 smtp_to = NULL;
93 if (smtp_from != NULL) {
94 free(smtp_from);
95 smtp_from = NULL;
98 if (smtp_subject != NULL) {
99 free(smtp_subject);
100 smtp_subject = NULL;
105 * Examines each value of the current configuration.
106 * If a value was not set, it is set to the default.
108 static void config_with_defaults()
110 if (watch_dir == NULL) {
111 watch_dir = strdup(WATCH_DIR);
114 if (smtp_host == NULL) {
115 smtp_host = strdup(SMTP_HOST);
118 if (smtp_port == 0) {
119 smtp_port = SMTP_PORT;
122 if (smtp_to == NULL) {
123 smtp_to = strdup(SMTP_TO);
126 if (smtp_from == NULL) {
127 smtp_from = strdup(SMTP_FROM);
130 if (smtp_subject == NULL) {
131 smtp_subject = strdup(SMTP_SUBJECT);
136 * Parses an inoclam.conf configuration file.
138 void config_parse()
140 int rc;
142 config_free();
144 cfg_t *cfg;
145 cfg_opt_t opts[] = {
146 CFG_SIMPLE_STR("watch_dir", &watch_dir),
147 CFG_SIMPLE_BOOL("virus_removal_enabled", &virus_removal_enabled),
148 CFG_SIMPLE_BOOL("smtp_enabled", &smtp_enabled),
149 CFG_SIMPLE_STR("smtp_host", &smtp_host),
150 CFG_SIMPLE_INT("smtp_port", &smtp_port),
151 CFG_SIMPLE_STR("smtp_to", &smtp_to),
152 CFG_SIMPLE_STR("smtp_from", &smtp_from),
153 CFG_SIMPLE_STR("smtp_subject", &smtp_subject),
154 CFG_END()
157 cfg = NULL;
159 if (!config_file_exists()) {
160 daemon_log(LOG_INFO, "(%s:%u) config file '%s' does not exist", __FILE__, __LINE__, DEFAULT_CONFIGFILE);
161 config_with_defaults();
162 return;
165 cfg = cfg_init(opts, 0);
166 if (!cfg) {
167 daemon_log(LOG_ERR, "(%s:%u) cfg_init failed!", __FILE__, __LINE__);
168 config_with_defaults();
169 return;
172 rc = cfg_parse(cfg, DEFAULT_CONFIGFILE);
173 if (rc == CFG_PARSE_ERROR) {
174 daemon_log(LOG_ERR, "(%s:%u) parser error '%s'", __FILE__, __LINE__, DEFAULT_CONFIGFILE);
177 if (cfg) {
178 cfg_free(cfg);
179 cfg = NULL;
182 config_with_defaults();