forked from Cisco-Talos/clamav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clamconf.c
251 lines (214 loc) · 5.76 KB
/
clamconf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright (C) 2006 Sensory Networks, Inc.
* (C) 2007 Tomasz Kojm <[email protected]>
* Written by Tomasz Kojm
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "cfgparser.h"
#define _GNU_SOURCE
#include "getopt.h"
static void printopt(const struct cfgoption *opt, const struct cfgstruct *cpt, int nondef)
{
if(!cpt->enabled && opt->numarg == -1) {
if(!nondef || (opt->numarg != cpt->numarg))
printf("%s not set\n", opt->name);
return;
}
switch(opt->argtype) {
case OPT_STR:
case OPT_FULLSTR:
case OPT_QUOTESTR:
if(!nondef || !opt->strarg || strcmp(opt->strarg, cpt->strarg))
printf("%s = \"%s\"\n", opt->name, cpt->strarg);
break;
case OPT_NUM:
case OPT_COMPSIZE:
if(!nondef || (opt->numarg != cpt->numarg))
printf("%s = %u\n", opt->name, cpt->numarg);
break;
case OPT_BOOL:
if(!nondef || (opt->numarg != cpt->numarg))
printf("%s = %s\n", opt->name, cpt->enabled ? "yes" : "no");
break;
default:
printf("%s: UNKNOWN ARGUMENT TYPE\n", opt->name);
}
}
static void printcfg(const char *cfgfile, int nondef)
{
const struct cfgoption *opt;
const struct cfgstruct *cpt;
struct cfgstruct *cfg;
int i;
unsigned short cfgowner = 0;
if(!(cfg = getcfg(cfgfile, 1))) {
printf("Can't parse %s\n", cfgfile);
return;
}
/* pre loop to detect merged config */
for(i = 0; ; i++) {
opt = &cfg_options[i];
if(!opt->name)
break;
cpt = cfgopt(cfg, opt->name);
if((cpt->numarg != opt->numarg) || (cpt->strarg && opt->strarg && strcmp(cpt->strarg, opt->strarg))) {
if((opt->owner & OPT_CLAMD) && !(opt->owner & OPT_FRESHCLAM))
cfgowner |= OPT_CLAMD;
else if((opt->owner & OPT_FRESHCLAM) && !(opt->owner & OPT_CLAMD))
cfgowner |= OPT_FRESHCLAM;
}
}
if((cfgowner & OPT_CLAMD) && (cfgowner & OPT_FRESHCLAM)) { /* merged cfg */
printf("%s: clamd and freshclam directives\n", cfgfile);
printf("-----------------\n");
printf("\n[common]\n");
for(i = 0; ; i++) {
opt = &cfg_options[i];
if(!opt->name)
break;
if((opt->owner & OPT_CLAMD) && (opt->owner & OPT_FRESHCLAM)) {
cpt = cfgopt(cfg, opt->name);
printopt(opt, cpt, nondef);
}
}
printf("\n[clamd]\n");
for(i = 0; ; i++) {
opt = &cfg_options[i];
if(!opt->name)
break;
if((opt->owner & OPT_CLAMD) && !(opt->owner & OPT_FRESHCLAM)) {
cpt = cfgopt(cfg, opt->name);
printopt(opt, cpt, nondef);
}
}
printf("\n[freshclam]\n");
for(i = 0; ; i++) {
opt = &cfg_options[i];
if(!opt->name)
break;
if((opt->owner & OPT_FRESHCLAM) && !(opt->owner & OPT_CLAMD)) {
cpt = cfgopt(cfg, opt->name);
printopt(opt, cpt, nondef);
}
}
} else { /* separate cfg */
if(cfgowner & OPT_CLAMD) {
printf("%s: clamd directives\n", cfgfile);
printf("-----------------\n");
for(i = 0; ; i++) {
opt = &cfg_options[i];
if(!opt->name)
break;
if(opt->owner & OPT_CLAMD) {
cpt = cfgopt(cfg, opt->name);
printopt(opt, cpt, nondef);
}
}
} else {
printf("%s: freshclam directives\n", cfgfile);
printf("-----------------\n");
for(i = 0; ; i++) {
opt = &cfg_options[i];
if(!opt->name)
break;
if(opt->owner & OPT_FRESHCLAM) {
cpt = cfgopt(cfg, opt->name);
printopt(opt, cpt, nondef);
}
}
}
}
freecfg(cfg);
}
static void help(void)
{
printf("\n");
printf(" Clam AntiVirus: Configuration Tool "VERSION"\n");
printf(" (C) 2006 - 2007 ClamAV Team - http://www.clamav.net/team\n\n");
printf(" --help -h show help\n");
printf(" --config-dir DIR -c DIR search for config files in DIR\n");
printf(" --non-default -n only print non-default settings\n");
printf("\n");
}
int main(int argc, char **argv)
{
char path[1024];
struct stat sb;
int ret, opt_index, nondef = 0;
const char *getopt_parameters = "hc:n";
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"config-dir", 1, 0, 'c'},
{"non-default", 0, 0, 'n'},
{0, 0, 0, 0}
};
char *confdir = strdup(CONFDIR);
int found = 0;
while(1) {
opt_index = 0;
ret = getopt_long(argc, argv, getopt_parameters, long_options, &opt_index);
if (ret == -1)
break;
switch (ret) {
case 0:
break;
case 'c':
free(confdir);
confdir = strdup(optarg);
break;
case 'h':
help();
free(confdir);
exit(0);
case 'n':
nondef = 1;
break;
default:
printf("ERROR: Unknown option passed\n");
free(confdir);
exit(1);
}
}
snprintf(path, sizeof(path), "%s/clamd.conf", confdir);
if(stat(path, &sb) != -1) {
printcfg(path, nondef);
found = 1;
printf("\n");
}
snprintf(path, sizeof(path), "%s/freshclam.conf", confdir);
if(stat(path, &sb) != -1) {
printcfg(path, nondef);
found = 1;
}
if(!found) {
printf("No config files found in %s\n", confdir);
free(confdir);
return 1;
}
free(confdir);
return 0;
}