~ubuntu-branches/ubuntu/precise/clamav/precise-updates

« back to all changes in this revision

Viewing changes to .pc/0002-Add-an-additional-n-after-the-number-in-the-pidfile.patch/clamav-milter/clamav-milter.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2015-01-30 09:01:52 UTC
  • mfrom: (118.1.5 precise-security)
  • Revision ID: package-import@ubuntu.com-20150130090152-es6oz6eg0wsa4r40
Tags: 0.98.6+dfsg-0ubuntu0.12.04.1
* Updated to 0.98.6 to fix security issues, including CVE-2014-9328.
* Removed upstreamed patches:
  - d/p/0002-Add-an-additional-n-after-the-number-in-the-pidfile.patch
  - d/p/0017-Bump-.so-version-number.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (C)2008 Sourcefire, Inc.
3
 
 *
4
 
 *  Author: aCaB <acab@clamav.net>
5
 
 *
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.
9
 
 *
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.
14
 
 *
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,
18
 
 *  MA 02110-1301, USA.
19
 
 */
20
 
 
21
 
#if HAVE_CONFIG_H
22
 
#include "clamav-config.h"
23
 
#endif
24
 
 
25
 
#include <stdio.h>
26
 
#include <sys/types.h>
27
 
#include <unistd.h>
28
 
#include <pwd.h>
29
 
#include <grp.h>
30
 
#include <string.h>
31
 
#ifdef USE_SYSLOG
32
 
#include <syslog.h>
33
 
#endif
34
 
#include <time.h>
35
 
#include <libmilter/mfapi.h>
36
 
 
37
 
#include "clamav.h"
38
 
 
39
 
#include "shared/output.h"
40
 
#include "shared/optparser.h"
41
 
#include "shared/misc.h"
42
 
#include "libclamav/default.h"
43
 
 
44
 
#include "connpool.h"
45
 
#include "netcode.h"
46
 
#include "clamfi.h"
47
 
#include "whitelist.h"
48
 
 
49
 
struct smfiDesc descr;
50
 
 
51
 
int main(int argc, char **argv) {
52
 
    char *my_socket, *pt;
53
 
    const struct optstruct *opt;
54
 
    struct optstruct *opts;
55
 
    time_t currtime;
56
 
    mode_t umsk;
57
 
    int ret;
58
 
 
59
 
    cl_initialize_crypto();
60
 
 
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 */
72
 
 
73
 
    opts = optparse(NULL, argc, argv, 1, OPT_MILTER, 0, NULL);
74
 
    if (!opts) {
75
 
        mprintf("!Can't parse command line options\n");
76
 
        return 1;
77
 
    }
78
 
 
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");
84
 
        optfree(opts);
85
 
        return 0;
86
 
    }
87
 
 
88
 
    if(opts->filename) {
89
 
        int x;
90
 
        for(x = 0; opts->filename[x]; x++)
91
 
            mprintf("^Ignoring option %s\n", opts->filename[x]);
92
 
    }
93
 
 
94
 
    if(optget(opts, "version")->enabled) {
95
 
        printf("clamav-milter %s\n", get_version());
96
 
        optfree(opts);
97
 
        return 0;
98
 
    }
99
 
 
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);
103
 
        free(pt);
104
 
        return 1;
105
 
    }
106
 
    free(pt);
107
 
 
108
 
    if((opt = optget(opts, "Chroot"))->enabled) {
109
 
        if(chdir(opt->strarg) != 0) {
110
 
            logg("!Cannot change directory to %s\n", opt->strarg);
111
 
            return 1;
112
 
        }
113
 
        if(chroot(opt->strarg) != 0) {
114
 
            logg("!chroot to %s failed. Are you root?\n", opt->strarg);
115
 
            return 1;
116
 
        }
117
 
    }
118
 
 
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);
123
 
            optfree(opts);
124
 
            return 1;
125
 
        }
126
 
 
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");
131
 
                optfree(opts);
132
 
                return 1;
133
 
            }
134
 
#else
135
 
            mprintf("!AllowSupplementaryGroups: initgroups() is not available, please disable AllowSupplementaryGroups\n");
136
 
            optfree(opts);
137
 
            return 1;
138
 
#endif
139
 
        } else {
140
 
#ifdef HAVE_SETGROUPS
141
 
            if(setgroups(1, &user->pw_gid)) {
142
 
                fprintf(stderr, "ERROR: setgroups() failed.\n");
143
 
                optfree(opts);
144
 
                return 1;
145
 
            }
146
 
#endif
147
 
        }
148
 
 
149
 
        if(setgid(user->pw_gid)) {
150
 
            fprintf(stderr, "ERROR: setgid(%d) failed.\n", (int) user->pw_gid);
151
 
            optfree(opts);
152
 
            return 1;
153
 
        }
154
 
 
155
 
        if(setuid(user->pw_uid)) {
156
 
            fprintf(stderr, "ERROR: setuid(%d) failed.\n", (int) user->pw_uid);
157
 
            optfree(opts);
158
 
            return 1;
159
 
        }
160
 
    }
161
 
 
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;
166
 
    if (logg_size)
167
 
        logg_rotate = optget(opts, "LogRotate")->enabled;
168
 
 
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");
173
 
            logg_close();
174
 
            optfree(opts);
175
 
            return 1;
176
 
        }
177
 
    } else
178
 
        logg_file = NULL;
179
 
 
180
 
#if defined(USE_SYSLOG) && !defined(C_AIX)
181
 
    if(optget(opts, "LogSyslog")->enabled) {
182
 
        int fac;
183
 
 
184
 
        opt = optget(opts, "LogFacility");
185
 
        if((fac = logg_facility(opt->strarg)) == -1) {
186
 
            logg("!LogFacility: %s: No such facility.\n", opt->strarg);
187
 
            logg_close();
188
 
            optfree(opts);
189
 
            return 1;
190
 
        }
191
 
 
192
 
        openlog("clamav-milter", LOG_PID, fac);
193
 
        logg_syslog = 1;
194
 
    }
195
 
#endif
196
 
 
197
 
    time(&currtime);
198
 
    if(logg("#+++ Started at %s", ctime(&currtime))) {
199
 
        fprintf(stderr, "ERROR: Can't initialize the internal logger\n");
200
 
        logg_close();
201
 
        optfree(opts);
202
 
        return 1;
203
 
    }
204
 
    if((opt = optget(opts, "TemporaryDirectory"))->enabled)
205
 
        tempdir = opt->strarg;
206
 
 
207
 
    if(localnets_init(opts) || init_actions(opts)) {
208
 
        logg_close();
209
 
        optfree(opts);
210
 
        return 1;
211
 
    }
212
 
 
213
 
    if((opt = optget(opts, "Whitelist"))->enabled && whitelist_init(opt->strarg)) {
214
 
        localnets_free();
215
 
        logg_close();
216
 
        optfree(opts);
217
 
        return 1;
218
 
    }
219
 
 
220
 
    if((opt = optget(opts, "SkipAuthenticated"))->enabled && smtpauth_init(opt->strarg)) {
221
 
        localnets_free();
222
 
        whitelist_free();
223
 
        logg_close();
224
 
        optfree(opts);
225
 
        return 1;
226
 
    }
227
 
 
228
 
    pt = optget(opts, "AddHeader")->strarg;
229
 
    if(strcasecmp(pt, "No")) {
230
 
        char myname[255];
231
 
 
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);
235
 
        } else
236
 
            snprintf(xvirushdr, sizeof(xvirushdr), "clamav-milter %s", get_version());
237
 
        xvirushdr[sizeof(xvirushdr)-1] = '\0';
238
 
 
239
 
        descr.xxfi_flags |= SMFIF_ADDHDRS;
240
 
 
241
 
        if(strcasecmp(pt, "Add")) { /* Replace or Yes */
242
 
            descr.xxfi_flags |= SMFIF_CHGHDRS;
243
 
            addxvirus = 1;
244
 
        } else { /* Add */
245
 
            addxvirus = 2;
246
 
        }
247
 
    }
248
 
 
249
 
    multircpt = optget(opts, "SupportMultipleRecipients")->enabled;
250
 
    
251
 
    if(!(my_socket = optget(opts, "MilterSocket")->strarg)) {
252
 
        logg("!Please configure the MilterSocket directive\n");
253
 
        localnets_free();
254
 
        whitelist_free();
255
 
        logg_close();
256
 
        optfree(opts);
257
 
        return 1;
258
 
    }
259
 
 
260
 
    if(!optget(opts, "Foreground")->enabled) {
261
 
        if(daemonize() == -1) {
262
 
            logg("!daemonize() failed\n");
263
 
            localnets_free();
264
 
            whitelist_free();
265
 
            cpool_free();
266
 
            logg_close();
267
 
            optfree(opts);
268
 
            return 1;
269
 
        }
270
 
        if(chdir("/") == -1)
271
 
            logg("^Can't change current working directory to root\n");
272
 
    }
273
 
 
274
 
    if(smfi_setconn(my_socket) == MI_FAILURE) {
275
 
        logg("!smfi_setconn failed\n");
276
 
        localnets_free();
277
 
        whitelist_free();
278
 
        logg_close();
279
 
        optfree(opts);
280
 
        return 1;
281
 
    }
282
 
    if(smfi_register(descr) == MI_FAILURE) {
283
 
        logg("!smfi_register failed\n");
284
 
        localnets_free();
285
 
        whitelist_free();
286
 
        logg_close();
287
 
        optfree(opts);
288
 
        return 1;
289
 
    }
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);
294
 
        localnets_free();
295
 
        whitelist_free();
296
 
        logg_close();
297
 
        optfree(opts);
298
 
        return 1;
299
 
    }
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;
304
 
        mode_t sock_mode;
305
 
        if(!strncmp(my_socket, "unix:", 5))
306
 
            sock_name += 5;
307
 
        if(!strncmp(my_socket, "local:", 6))
308
 
            sock_name += 6;
309
 
        if(*my_socket == ':')
310
 
            sock_name ++;
311
 
 
312
 
        if(optget(opts, "MilterSocketGroup")->enabled) {
313
 
            char *gname = optget(opts, "MilterSocketGroup")->strarg, *end;
314
 
            gid_t sock_gid = strtol(gname, &end, 10);
315
 
            if(*end) {
316
 
                struct group *pgrp = getgrnam(gname);
317
 
                if(!pgrp) {
318
 
                    logg("!Unknown group %s\n", gname);
319
 
                    localnets_free();
320
 
                    whitelist_free();
321
 
                    logg_close();
322
 
                    optfree(opts);
323
 
                    return 1;
324
 
                }
325
 
                sock_gid = pgrp->gr_gid;
326
 
            }
327
 
            if(chown(sock_name, -1, sock_gid)) {
328
 
                logg("!Failed to change socket ownership to group %s\n", gname);
329
 
                localnets_free();
330
 
                whitelist_free();
331
 
                logg_close();
332
 
                optfree(opts);
333
 
                return 1;
334
 
            }
335
 
        }
336
 
        if(optget(opts, "MilterSocketMode")->enabled) {
337
 
            char *end;
338
 
            sock_mode = strtol(optget(opts, "MilterSocketMode")->strarg, &end, 8);
339
 
            if(*end) {
340
 
                logg("!Invalid MilterSocketMode %s\n", optget(opts, "MilterSocketMode")->strarg);
341
 
                localnets_free();
342
 
                whitelist_free();
343
 
                logg_close();
344
 
                optfree(opts);
345
 
                return 1;
346
 
            }
347
 
        } else
348
 
            sock_mode = 0777 & ~umsk;
349
 
 
350
 
        if(chmod(sock_name, sock_mode & 0666)) {
351
 
            logg("!Cannot set milter socket permission to %s\n", optget(opts, "MilterSocketMode")->strarg);
352
 
            localnets_free();
353
 
            whitelist_free();
354
 
            logg_close();
355
 
            optfree(opts);
356
 
            return 1;
357
 
        }
358
 
    }
359
 
 
360
 
    maxfilesize = optget(opts, "MaxFileSize")->numarg;
361
 
    if(!maxfilesize) {
362
 
        logg("^Invalid MaxFileSize, using default (%d)\n", CLI_DEFAULT_MAXFILESIZE);
363
 
        maxfilesize = CLI_DEFAULT_MAXFILESIZE;
364
 
    }
365
 
    readtimeout = optget(opts, "ReadTimeout")->numarg;
366
 
 
367
 
    cpool_init(opts);
368
 
    if (!cp) {
369
 
        logg("!Failed to init the socket pool\n");
370
 
        localnets_free();
371
 
        whitelist_free();
372
 
        logg_close();
373
 
        optfree(opts);
374
 
        return 1;
375
 
    }   
376
 
 
377
 
    if((opt = optget(opts, "PidFile"))->enabled) {
378
 
        FILE *fd;
379
 
        mode_t old_umask = umask(0002);
380
 
 
381
 
        if((fd = fopen(opt->strarg, "w")) == NULL) {
382
 
            logg("!Can't save PID in file %s\n", opt->strarg);
383
 
        } else {
384
 
            if (fprintf(fd, "%u", (unsigned int)getpid())<0) {
385
 
                logg("!Can't save PID in file %s\n", opt->strarg);
386
 
            }
387
 
            fclose(fd);
388
 
        }
389
 
        umask(old_umask);
390
 
    }
391
 
 
392
 
    ret = smfi_main();
393
 
 
394
 
    optfree(opts);
395
 
 
396
 
    logg_close();
397
 
    cpool_free();
398
 
    localnets_free();
399
 
    whitelist_free();
400
 
 
401
 
    return ret;
402
 
}
403
 
 
404
 
/*
405
 
 * Local Variables:
406
 
 * mode: c
407
 
 * c-basic-offset: 4
408
 
 * tab-width: 8
409
 
 * End: 
410
 
 * vim: set cindent smartindent autoindent softtabstop=4 shiftwidth=4 tabstop=8: 
411
 
 */