2
* Copyright (C)2008 Sourcefire, Inc.
4
* Author: aCaB <acab@clamav.net>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License version 2 as
8
* published by the Free Software Foundation.
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., 51 Franklin Street, Fifth Floor, Boston,
22
#include "clamav-config.h"
26
#include <sys/types.h>
35
#include <libmilter/mfapi.h>
39
#include "shared/output.h"
40
#include "shared/optparser.h"
41
#include "shared/misc.h"
42
#include "libclamav/default.h"
47
#include "whitelist.h"
49
struct smfiDesc descr;
51
int main(int argc, char **argv) {
53
const struct optstruct *opt;
54
struct optstruct *opts;
59
cl_initialize_crypto();
61
memset(&descr, 0, sizeof(struct smfiDesc));
62
descr.xxfi_name = "ClamAV"; /* filter name */
63
descr.xxfi_version = SMFI_VERSION; /* milter version */
64
descr.xxfi_flags = SMFIF_QUARANTINE; /* flags */
65
descr.xxfi_connect = clamfi_connect; /* connection info filter */
66
descr.xxfi_envfrom = clamfi_envfrom; /* envelope sender filter */
67
descr.xxfi_envrcpt = clamfi_envrcpt; /* envelope recipient filter */
68
descr.xxfi_header = clamfi_header; /* header filter */
69
descr.xxfi_body = clamfi_body; /* body block */
70
descr.xxfi_eom = clamfi_eom; /* end of message */
71
descr.xxfi_abort = clamfi_abort; /* message aborted */
73
opts = optparse(NULL, argc, argv, 1, OPT_MILTER, 0, NULL);
75
mprintf("!Can't parse command line options\n");
79
if(optget(opts, "help")->enabled) {
80
printf("Usage: %s [-c <config-file>]\n\n", argv[0]);
81
printf(" --help -h Show this help\n");
82
printf(" --version -V Show version and exit\n");
83
printf(" --config-file <file> -c Read configuration from file\n\n");
90
for(x = 0; opts->filename[x]; x++)
91
mprintf("^Ignoring option %s\n", opts->filename[x]);
94
if(optget(opts, "version")->enabled) {
95
printf("clamav-milter %s\n", get_version());
100
pt = strdup(optget(opts, "config-file")->strarg);
101
if((opts = optparse(pt, 0, NULL, 1, OPT_MILTER, 0, opts)) == NULL) {
102
printf("%s: cannot parse config file %s\n", argv[0], pt);
108
if((opt = optget(opts, "Chroot"))->enabled) {
109
if(chdir(opt->strarg) != 0) {
110
logg("!Cannot change directory to %s\n", opt->strarg);
113
if(chroot(opt->strarg) != 0) {
114
logg("!chroot to %s failed. Are you root?\n", opt->strarg);
119
if(geteuid() == 0 && (opt = optget(opts, "User"))->enabled) {
120
struct passwd *user = NULL;
121
if((user = getpwnam(opt->strarg)) == NULL) {
122
fprintf(stderr, "ERROR: Can't get information about user %s.\n", opt->strarg);
127
if(optget(opts, "AllowSupplementaryGroups")->enabled) {
128
#ifdef HAVE_INITGROUPS
129
if(initgroups(opt->strarg, user->pw_gid)) {
130
fprintf(stderr, "ERROR: initgroups() failed.\n");
135
mprintf("!AllowSupplementaryGroups: initgroups() is not available, please disable AllowSupplementaryGroups\n");
140
#ifdef HAVE_SETGROUPS
141
if(setgroups(1, &user->pw_gid)) {
142
fprintf(stderr, "ERROR: setgroups() failed.\n");
149
if(setgid(user->pw_gid)) {
150
fprintf(stderr, "ERROR: setgid(%d) failed.\n", (int) user->pw_gid);
155
if(setuid(user->pw_uid)) {
156
fprintf(stderr, "ERROR: setuid(%d) failed.\n", (int) user->pw_uid);
162
logg_lock = !optget(opts, "LogFileUnlock")->enabled;
163
logg_time = optget(opts, "LogTime")->enabled;
164
logg_size = optget(opts, "LogFileMaxSize")->numarg;
165
logg_verbose = mprintf_verbose = optget(opts, "LogVerbose")->enabled;
167
logg_rotate = optget(opts, "LogRotate")->enabled;
169
if((opt = optget(opts, "LogFile"))->enabled) {
170
logg_file = opt->strarg;
171
if(!cli_is_abspath(logg_file)) {
172
fprintf(stderr, "ERROR: LogFile requires full path.\n");
180
#if defined(USE_SYSLOG) && !defined(C_AIX)
181
if(optget(opts, "LogSyslog")->enabled) {
184
opt = optget(opts, "LogFacility");
185
if((fac = logg_facility(opt->strarg)) == -1) {
186
logg("!LogFacility: %s: No such facility.\n", opt->strarg);
192
openlog("clamav-milter", LOG_PID, fac);
198
if(logg("#+++ Started at %s", ctime(&currtime))) {
199
fprintf(stderr, "ERROR: Can't initialize the internal logger\n");
204
if((opt = optget(opts, "TemporaryDirectory"))->enabled)
205
tempdir = opt->strarg;
207
if(localnets_init(opts) || init_actions(opts)) {
213
if((opt = optget(opts, "Whitelist"))->enabled && whitelist_init(opt->strarg)) {
220
if((opt = optget(opts, "SkipAuthenticated"))->enabled && smtpauth_init(opt->strarg)) {
228
pt = optget(opts, "AddHeader")->strarg;
229
if(strcasecmp(pt, "No")) {
232
if(((opt = optget(opts, "ReportHostname"))->enabled && strncpy(myname, opt->strarg, sizeof(myname))) || !gethostname(myname, sizeof(myname))) {
233
myname[sizeof(myname)-1] = '\0';
234
snprintf(xvirushdr, sizeof(xvirushdr), "clamav-milter %s at %s", get_version(), myname);
236
snprintf(xvirushdr, sizeof(xvirushdr), "clamav-milter %s", get_version());
237
xvirushdr[sizeof(xvirushdr)-1] = '\0';
239
descr.xxfi_flags |= SMFIF_ADDHDRS;
241
if(strcasecmp(pt, "Add")) { /* Replace or Yes */
242
descr.xxfi_flags |= SMFIF_CHGHDRS;
249
multircpt = optget(opts, "SupportMultipleRecipients")->enabled;
251
if(!(my_socket = optget(opts, "MilterSocket")->strarg)) {
252
logg("!Please configure the MilterSocket directive\n");
260
if(!optget(opts, "Foreground")->enabled) {
261
if(daemonize() == -1) {
262
logg("!daemonize() failed\n");
271
logg("^Can't change current working directory to root\n");
274
if(smfi_setconn(my_socket) == MI_FAILURE) {
275
logg("!smfi_setconn failed\n");
282
if(smfi_register(descr) == MI_FAILURE) {
283
logg("!smfi_register failed\n");
290
opt = optget(opts, "FixStaleSocket");
291
umsk = umask(0777); /* socket is created with 000 to avoid races */
292
if(smfi_opensocket(opt->enabled) == MI_FAILURE) {
293
logg("!Failed to create socket %s\n", my_socket);
300
umask(umsk); /* restore umask */
301
if(strncmp(my_socket, "inet:", 5) && strncmp(my_socket, "inet6:", 6)) {
302
/* set group ownership and perms on the local socket */
303
char *sock_name = my_socket;
305
if(!strncmp(my_socket, "unix:", 5))
307
if(!strncmp(my_socket, "local:", 6))
309
if(*my_socket == ':')
312
if(optget(opts, "MilterSocketGroup")->enabled) {
313
char *gname = optget(opts, "MilterSocketGroup")->strarg, *end;
314
gid_t sock_gid = strtol(gname, &end, 10);
316
struct group *pgrp = getgrnam(gname);
318
logg("!Unknown group %s\n", gname);
325
sock_gid = pgrp->gr_gid;
327
if(chown(sock_name, -1, sock_gid)) {
328
logg("!Failed to change socket ownership to group %s\n", gname);
336
if(optget(opts, "MilterSocketMode")->enabled) {
338
sock_mode = strtol(optget(opts, "MilterSocketMode")->strarg, &end, 8);
340
logg("!Invalid MilterSocketMode %s\n", optget(opts, "MilterSocketMode")->strarg);
348
sock_mode = 0777 & ~umsk;
350
if(chmod(sock_name, sock_mode & 0666)) {
351
logg("!Cannot set milter socket permission to %s\n", optget(opts, "MilterSocketMode")->strarg);
360
maxfilesize = optget(opts, "MaxFileSize")->numarg;
362
logg("^Invalid MaxFileSize, using default (%d)\n", CLI_DEFAULT_MAXFILESIZE);
363
maxfilesize = CLI_DEFAULT_MAXFILESIZE;
365
readtimeout = optget(opts, "ReadTimeout")->numarg;
369
logg("!Failed to init the socket pool\n");
377
if((opt = optget(opts, "PidFile"))->enabled) {
379
mode_t old_umask = umask(0002);
381
if((fd = fopen(opt->strarg, "w")) == NULL) {
382
logg("!Can't save PID in file %s\n", opt->strarg);
384
if (fprintf(fd, "%u", (unsigned int)getpid())<0) {
385
logg("!Can't save PID in file %s\n", opt->strarg);
410
* vim: set cindent smartindent autoindent softtabstop=4 shiftwidth=4 tabstop=8: