~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 *  Copyright (C) 2009 Sourcefire, Inc.
 *  Author: Tomasz Kojm <tkojm@clamav.net>
 *
 *  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 <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <time.h>

#include "shared/optparser.h"
#include "shared/misc.h"

#include "libclamav/str.h"
#include "libclamav/clamav.h"
#include "libclamav/others.h"
#include "libclamav/bytecode.h"

#ifndef _WIN32
extern const struct clam_option *clam_options;
#else
__declspec(dllimport) extern const struct clam_option *clam_options;
#endif

static struct _cfgfile {
    const char *name;
    int tool;
} cfgfile[] = {
    { "clamd.conf",	    OPT_CLAMD	    },
    { "freshclam.conf",	    OPT_FRESHCLAM   },
    { "clamav-milter.conf", OPT_MILTER	    },
    { NULL,		    0		    }
};

static const char *dbnames[] = { "main.cvd", "main.cld", "daily.cvd",
				 "daily.cld", "safebrowsing.cvd",
				 "safebrowsing.cld", NULL };

static void printopts(struct optstruct *opts, int nondef)
{
	const struct optstruct *opt;

    while(opts) {
	if(!opts->name) {
	    opts = opts->next;
	    continue;
	}
	if(clam_options[opts->idx].owner & OPT_DEPRECATED) {
	    if(opts->active)
		printf("*** %s is DEPRECATED ***\n", opts->name);
	    opts = opts->next;
	    continue;
	}
	if(nondef && (opts->numarg == clam_options[opts->idx].numarg) && ((opts->strarg == clam_options[opts->idx].strarg) || (opts->strarg && clam_options[opts->idx].strarg && !strcmp(opts->strarg, clam_options[opts->idx].strarg)))) {
	    opts = opts->next;
	    continue;
	}
	if(!opts->enabled) 
	    printf("%s disabled\n", opts->name);
	else switch(clam_options[opts->idx].argtype) {
	    case TYPE_STRING:
		printf("%s = \"%s\"", opts->name, opts->strarg);
		opt = opts;
		while((opt = opt->nextarg))
		    printf(", \"%s\"", opt->strarg);
		printf("\n");
		break;

	    case TYPE_NUMBER:
	    case TYPE_SIZE:
		printf("%s = \"%lld\"", opts->name, opts->numarg);
		opt = opts;
		while((opt = opt->nextarg))
		    printf(", \"%lld\"", opt->numarg);
		printf("\n");
		break;

	    case TYPE_BOOL:
		printf("%s = \"yes\"\n", opts->name);
		break;

	    default:
		printf("!!! %s: UNKNOWN INTERNAL TYPE !!!\n", opts->name);
	}
	opts = opts->next;
    }
}

static int printconf(const char *name)
{
	int i, j, tool = 0, tokens_count;
	char buffer[1025];
	const char *tokens[128];
	const struct clam_option *cpt;

    for(i = 0; cfgfile[i].name; i++) {
	if(!strcmp(name, cfgfile[i].name)) {
	    tool = cfgfile[i].tool;
	    break;
	}
    }
    if(!tool) {
	printf("ERROR: Unknown config file\nAvailable options:");
	for(i = 0; cfgfile[i].name; i++)
	    printf(" %s", cfgfile[i].name);
	printf("\n");
	return 1;
    }

    printf("##\n## %s - automatically generated by clamconf "VERSION"\n##\n", name);
    printf("\n# Comment out or remove the line below.\nExample\n");

    for(i = 0; clam_options[i].owner; i++) {
	cpt = &clam_options[i];
	if(cpt->name && (cpt->owner & tool) && !(cpt->owner & OPT_DEPRECATED) && !(cpt->flags & 4)) {
	    strncpy(buffer, cpt->description, 1024);
	    buffer[1024] = 0;
	    tokens_count = cli_strtokenize(buffer, '\n', 128, tokens);
	    printf("\n");
	    for(j = 0; j < tokens_count; j++)
		printf("# %s\n", tokens[j]);

	    switch(cpt->argtype) {
		case TYPE_STRING:
		    if(cpt->strarg)
			printf("# Default: %s\n", cpt->strarg);
		    else
			printf("# Default: disabled\n");
		    break;

		case TYPE_NUMBER:
		    if(cpt->numarg != -1)
			printf("# Default: %lld\n", cpt->numarg);
		    else
			printf("# Default: disabled\n");
		    break;

		case TYPE_SIZE:
		    printf("# You may use 'M' or 'm' for megabytes (1M = 1m = 1048576 bytes)\n# and 'K' or 'k' for kilobytes (1K = 1k = 1024 bytes). To specify the size\n# in bytes just don't use modifiers.\n");
		    if(cpt->numarg != -1)
			printf("# Default: %lld\n", cpt->numarg);
		    else
			printf("# Default: disabled\n");
		    break;

		case TYPE_BOOL:
		    if(cpt->numarg != -1)
			printf("# Default: %s\n", cpt->numarg ? "yes" : "no");
		    else
			printf("# Default: disabled\n");
		    break;

		default:
		    printf("!!! %s: UNKNOWN INTERNAL TYPE !!!\n", cpt->name);
	    }

	    if(cpt->suggested && strchr(cpt->suggested, '\n')) {
		strncpy(buffer, cpt->suggested, 1024);
		buffer[1024] = 0;
		tokens_count = cli_strtokenize(buffer, '\n', 128, tokens);
		for(j = 0; j < tokens_count; j++)
		    printf("#%s %s\n", cpt->name, tokens[j]);
	    } else {
		printf("#%s %s\n", cpt->name, cpt->suggested ? cpt->suggested : "ARG");
	    }
	}
    }

    return 0;
}

static void help(void)
{
    printf("\n");
    printf("           Clam AntiVirus: Configuration Tool %s\n", get_version());
    printf("           By The ClamAV Team: http://www.clamav.net/team\n");
    printf("           (C) 2009 Sourcefire, Inc.\n\n");

    printf("    --help                 -h         Show help\n");
    printf("    --version              -V         Show version\n");
    printf("    --config-dir=DIR       -c DIR     Read configuration files from DIR\n");
    printf("    --non-default          -n         Only display non-default settings\n");
    printf("    --generate-config=NAME -g NAME    Generate example config file\n");
    printf("\n");
    return;
}

int main(int argc, char **argv)
{
	const char *dir;
	char path[512], dbdir[512], *pt;
	struct optstruct *opts, *toolopts;
	const struct optstruct *opt;
	unsigned int i, j;
	struct cl_cvd *cvd;
	unsigned int flevel;


    opts = optparse(NULL, argc, argv, 1, OPT_CLAMCONF, 0, NULL);
    if(!opts) {
	printf("ERROR: Can't parse command line options\n");
	return 1;
    }

    if(optget(opts, "help")->enabled) {
	help();
	optfree(opts);
	return 0;
    }

    if(optget(opts, "version")->enabled) {
	printf("Clam AntiVirus Configuration Tool %s\n", get_version());
	optfree(opts);
	return 0;
    }

    if((opt = optget(opts, "generate-config"))->enabled) {
	printconf(opt->strarg);
	optfree(opts);
	return 0;
    }

    dbdir[0] = 0;
    dir = optget(opts, "config-dir")->strarg;
    printf("Checking configuration files in %s\n", dir);
    for(i = 0; cfgfile[i].name; i++) {
	snprintf(path, sizeof(path), "%s"PATHSEP"%s", dir, cfgfile[i].name);
	path[511] = 0;
	if(access(path, R_OK)) {
	    printf("\n%s not found\n", cfgfile[i].name);
	    continue;
	}
	printf("\nConfig file: %s\n", cfgfile[i].name);
	for(j = 0; j < strlen(cfgfile[i].name) + 13; j++)
	    printf("-");
	printf("\n");
	toolopts = optparse(path, 0, NULL, 1, cfgfile[i].tool | OPT_DEPRECATED, 0, NULL);
	if(!toolopts)
	    continue;
	printopts(toolopts, optget(opts, "non-default")->enabled);
	if(cfgfile[i].tool == OPT_FRESHCLAM) {
	    opt = optget(toolopts, "DatabaseDirectory");
	    strncpy(dbdir, opt->strarg, sizeof(dbdir));
	    dbdir[sizeof(dbdir) - 1] = 0;
	}
	optfree(toolopts);
    }
    optfree(opts);

    printf("\nSoftware settings\n-----------------\n");
    printf("Version: %s\n", cl_retver());
    if(strcmp(cl_retver(), get_version()))
	printf("WARNING: Version mismatch: libclamav=%s, clamconf=%s\n", cl_retver(), get_version());
    cl_init(CL_INIT_DEFAULT);
    printf("Optional features supported: ");
#ifdef USE_MPOOL
	printf("MEMPOOL ");
#endif
#ifdef SUPPORT_IPv6
	printf("IPv6 ");
#endif
#ifdef CLAMUKO
	printf("CLAMUKO ");
#endif
#ifdef C_BIGSTACK
	printf("BIGSTACK ");
#endif
#ifdef FRESHCLAM_DNS_FIX
	printf("FRESHCLAM_DNS_FIX ");
#endif
#ifdef FPU_WORDS_BIGENDIAN
	printf("AUTOIT_EA06 ");
#endif
#ifdef HAVE_BZLIB_H
	printf("BZIP2 ");
#endif
    if(have_rar)
	printf("RAR ");
    if (have_clamjit)
	printf("JIT");
    printf("\n");

    if(!strlen(dbdir)) {
	pt = freshdbdir();
	if(pt) {
	    strncpy(dbdir, pt, sizeof(dbdir));
	    free(pt);
	} else {
	    strncpy(dbdir, DATADIR, sizeof(dbdir));
	}
	dbdir[sizeof(dbdir) - 1] = 0;
    }
    printf("Database directory: %s\n", dbdir);
    flevel = cl_retflevel();
    for(i = 0; dbnames[i]; i++) {
	snprintf(path, sizeof(path), "%s"PATHSEP"%s", dbdir, dbnames[i]);
	path[511] = 0;
	if(!access(path, R_OK)) {
	    cvd = cl_cvdhead(path);
	    if(!cvd) {
		printf("%s: Can't get information about the database\n", dbnames[i]);
	    } else {
		const time_t t = cvd->stime;
		printf("%s: version %u, sigs: %u, built on %s", dbnames[i], cvd->version, cvd->sigs, ctime(&t));
		if(cvd->fl > flevel)
		    printf("%s: WARNING: This database requires f-level %u (current f-level: %u)\n", dbnames[i], cvd->fl, flevel);
		cl_cvdfree(cvd);
	    }
	}
    }
    return 0;
}