~ubuntu-branches/ubuntu/wily/clamav/wily-proposed

« back to all changes in this revision

Viewing changes to .pc/0008-Add-upstream-systemd-support-for-clamav-daemon-and-c.patch/freshclam/freshclam.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman, Sebastian Andrzej Siewior, Andreas Cadhalpun, Scott Kitterman, Javier Fernández-Sanguino
  • Date: 2015-01-28 00:25:13 UTC
  • mfrom: (0.48.14 sid)
  • Revision ID: package-import@ubuntu.com-20150128002513-lil2oi74cooy4lzr
Tags: 0.98.6+dfsg-1
[ Sebastian Andrzej Siewior ]
* update "fix-ssize_t-size_t-off_t-printf-modifier", include of misc.h was
  missing but was pulled in via the systemd patch.
* Don't leak return codes from libmspack to clamav API. (Closes: #774686).

[ Andreas Cadhalpun ]
* Add patch to avoid emitting incremental progress messages when not
  outputting to a terminal. (Closes: #767350)
* Update lintian-overrides for unused-file-paragraph-in-dep5-copyright.
* clamav-base.postinst: always chown /var/log/clamav and /var/lib/clamav
  to clamav:clamav, not only on fresh installations. (Closes: #775400)
* Adapt the clamav-daemon and clamav-freshclam logrotate scripts,
  so that they correctly work under systemd.
* Move the PidFile variable from the clamd/freshclam configuration files
  to the init scripts. This makes the init scripts more robust against
  misconfiguration and avoids error messages with systemd. (Closes: #767353)
* debian/copyright: drop files from Files-Excluded only present in github
  tarballs
* Drop Workaround-a-bug-in-libc-on-Hurd.patch, because hurd got fixed.
  (see #752237)
* debian/rules: Remove useless --with-system-tommath --without-included-ltdl
  configure options.

[ Scott Kitterman ]
* Stop stripping llvm when repacking the tarball as the system llvm on some
  releases is too old to use
* New upstream bugfix release
  - Library shared object revisions.
  - Includes a patch from Sebastian Andrzej Siewior making ClamAV pid files
    compatible with systemd.
  - Fix a heap out of bounds condition with crafted Yoda's crypter files.
    This issue was discovered by Felix Groebert of the Google Security Team.
  - Fix a heap out of bounds condition with crafted mew packer files. This
    issue was discovered by Felix Groebert of the Google Security Team.
  - Fix a heap out of bounds condition with crafted upx packer files. This
    issue was discovered by Kevin Szkudlapski of Quarkslab.
  - Fix a heap out of bounds condition with crafted upack packer files. This
    issue was discovered by Sebastian Andrzej Siewior. CVE-2014-9328.
  - Compensate a crash due to incorrect compiler optimization when handling
    crafted petite packer files. This issue was discovered by Sebastian
    Andrzej Siewior.
* Update lintian override for embedded zlib to match new so version

[ Javier Fernández-Sanguino ]
* Updated Spanish Debconf template translation (Closes: #773563)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (C) 2002 - 2006 Tomasz Kojm <tkojm@clamav.net>
3
 
 *
4
 
 *  This program is free software; you can redistribute it and/or modify
5
 
 *  it under the terms of the GNU General Public License version 2 as
6
 
 *  published by the Free Software Foundation.
7
 
 *
8
 
 *  This program is distributed in the hope that it will be useful,
9
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 *  GNU General Public License for more details.
12
 
 *
13
 
 *  You should have received a copy of the GNU General Public License
14
 
 *  along with this program; if not, write to the Free Software
15
 
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16
 
 *  MA 02110-1301, USA.
17
 
 */
18
 
 
19
 
#if HAVE_CONFIG_H
20
 
#include "clamav-config.h"
21
 
#endif
22
 
 
23
 
#include <stdio.h>
24
 
#include <stdlib.h>
25
 
#ifdef  HAVE_UNISTD_H
26
 
#include <unistd.h>
27
 
#endif
28
 
#include <string.h>
29
 
#include <errno.h>
30
 
#include <signal.h>
31
 
#include <time.h>
32
 
#include <sys/types.h>
33
 
#ifndef _WIN32
34
 
#include <sys/wait.h>
35
 
#endif
36
 
#include <sys/stat.h>
37
 
#include <fcntl.h>
38
 
#ifdef  HAVE_PWD_H
39
 
#include <pwd.h>
40
 
#endif
41
 
#ifdef HAVE_GRP_H
42
 
#include <grp.h>
43
 
#endif
44
 
 
45
 
#if defined(USE_SYSLOG) && !defined(C_AIX)
46
 
#include <syslog.h>
47
 
#endif
48
 
 
49
 
#include "target.h"
50
 
#include "clamav.h"
51
 
#include "freshclamcodes.h"
52
 
 
53
 
#include "libclamav/others.h"
54
 
#include "libclamav/str.h"
55
 
 
56
 
#include "shared/optparser.h"
57
 
#include "shared/output.h"
58
 
#include "shared/misc.h"
59
 
 
60
 
#include "execute.h"
61
 
#include "manager.h"
62
 
#include "mirman.h"
63
 
 
64
 
static short terminate = 0;
65
 
extern int active_children;
66
 
 
67
 
static short foreground = 1;
68
 
char updtmpdir[512], dbdir[512];
69
 
int sigchld_wait = 1;
70
 
const char *pidfile = NULL;
71
 
char hostid[37];
72
 
 
73
 
void submit_host_info(struct optstruct *opts);
74
 
char *get_hostid(void *cbdata);
75
 
int is_valid_hostid(void);
76
 
 
77
 
static void
78
 
sighandler (int sig)
79
 
{
80
 
 
81
 
    switch (sig)
82
 
    {
83
 
#ifdef  SIGCHLD
84
 
    case SIGCHLD:
85
 
        if (sigchld_wait)
86
 
            waitpid (-1, NULL, WNOHANG);
87
 
        active_children--;
88
 
        break;
89
 
#endif
90
 
 
91
 
#ifdef SIGPIPE
92
 
    case SIGPIPE:
93
 
        /* no action, app will get EPIPE */
94
 
        break;
95
 
#endif
96
 
 
97
 
#ifdef  SIGALRM
98
 
    case SIGALRM:
99
 
        terminate = -1;
100
 
        break;
101
 
#endif
102
 
#ifdef  SIGUSR1
103
 
    case SIGUSR1:
104
 
        terminate = -1;
105
 
        break;
106
 
#endif
107
 
 
108
 
#ifdef  SIGHUP
109
 
    case SIGHUP:
110
 
        terminate = -2;
111
 
        break;
112
 
#endif
113
 
 
114
 
    default:
115
 
        if (*updtmpdir)
116
 
            cli_rmdirs (updtmpdir);
117
 
        if (pidfile)
118
 
            unlink (pidfile);
119
 
        logg ("Update process terminated\n");
120
 
        exit (2);
121
 
    }
122
 
 
123
 
    return;
124
 
}
125
 
 
126
 
static void
127
 
writepid (const char *pidfile)
128
 
{
129
 
    FILE *fd;
130
 
    int old_umask;
131
 
    old_umask = umask (0006);
132
 
    if ((fd = fopen (pidfile, "w")) == NULL)
133
 
    {
134
 
        logg ("!Can't save PID to file %s: %s\n", pidfile, strerror (errno));
135
 
    }
136
 
    else
137
 
    {
138
 
        fprintf (fd, "%d\n", (int) getpid ());
139
 
        fclose (fd);
140
 
    }
141
 
    umask (old_umask);
142
 
}
143
 
 
144
 
static void
145
 
help (void)
146
 
{
147
 
    mprintf_stdout = 1;
148
 
 
149
 
    mprintf ("\n");
150
 
    mprintf ("                   Clam AntiVirus: freshclam  %s\n",
151
 
             get_version ());
152
 
    printf ("           By The ClamAV Team: http://www.clamav.net/about.html#credits\n");
153
 
    printf ("           (C) 2007-2009 Sourcefire, Inc. et al.\n\n");
154
 
 
155
 
    mprintf ("    --help               -h              show help\n");
156
 
    mprintf
157
 
        ("    --version            -V              print version number and exit\n");
158
 
    mprintf ("    --verbose            -v              be verbose\n");
159
 
    mprintf
160
 
        ("    --debug                              enable debug messages\n");
161
 
    mprintf
162
 
        ("    --quiet                              only output error messages\n");
163
 
    mprintf
164
 
        ("    --no-warnings                        don't print and log warnings\n");
165
 
    mprintf
166
 
        ("    --stdout                             write to stdout instead of stderr\n");
167
 
    mprintf ("\n");
168
 
    mprintf
169
 
        ("    --config-file=FILE                   read configuration from FILE.\n");
170
 
    mprintf ("    --log=FILE           -l FILE         log into FILE\n");
171
 
#ifndef _WIN32
172
 
    mprintf ("    --daemon             -d              run in daemon mode\n");
173
 
    mprintf
174
 
        ("    --pid=FILE           -p FILE         save daemon's pid in FILE\n");
175
 
    mprintf ("    --user=USER          -u USER         run as USER\n");
176
 
#endif
177
 
    mprintf
178
 
        ("    --no-dns                             force old non-DNS verification method\n");
179
 
    mprintf
180
 
        ("    --checks=#n          -c #n           number of checks per day, 1 <= n <= 50\n");
181
 
    mprintf
182
 
        ("    --datadir=DIRECTORY                  download new databases into DIRECTORY\n");
183
 
#ifdef BUILD_CLAMD
184
 
    mprintf
185
 
        ("    --daemon-notify[=/path/clamd.conf]   send RELOAD command to clamd\n");
186
 
#endif
187
 
    mprintf
188
 
        ("    --local-address=IP   -a IP           bind to IP for HTTP downloads\n");
189
 
    mprintf
190
 
        ("    --on-update-execute=COMMAND          execute COMMAND after successful update\n");
191
 
    mprintf
192
 
        ("    --on-error-execute=COMMAND           execute COMMAND if errors occured\n");
193
 
    mprintf
194
 
        ("    --on-outdated-execute=COMMAND        execute COMMAND when software is outdated\n");
195
 
    mprintf
196
 
        ("    --list-mirrors                       print mirrors from mirrors.dat\n");
197
 
    mprintf
198
 
        ("    --enable-stats                       enable statistical information reporting\n");
199
 
    mprintf
200
 
        ("    --stats-host-id=UUID                 HostID in the form of an UUID to use when submitting statistical information\n");
201
 
    mprintf
202
 
        ("    --update-db=DBNAME                   only update database DBNAME\n");
203
 
 
204
 
    mprintf ("\n");
205
 
}
206
 
 
207
 
static int
208
 
download (const struct optstruct *opts, const char *cfgfile)
209
 
{
210
 
    int ret = 0, try = 1, maxattempts = 0;
211
 
    const struct optstruct *opt;
212
 
 
213
 
 
214
 
    maxattempts = optget (opts, "MaxAttempts")->numarg;
215
 
    logg ("*Max retries == %d\n", maxattempts);
216
 
 
217
 
    if (!(opt = optget (opts, "DatabaseMirror"))->enabled)
218
 
    {
219
 
        logg ("^You must specify at least one database mirror in %s\n",
220
 
              cfgfile);
221
 
        return FCE_CONFIG;
222
 
    }
223
 
    else
224
 
    {
225
 
        while (opt)
226
 
        {
227
 
            ret = downloadmanager (opts, opt->strarg, try);
228
 
#ifndef _WIN32
229
 
            alarm (0);
230
 
#endif
231
 
            if (ret == FCE_CONNECTION || ret == FCE_BADCVD
232
 
                || ret == FCE_FAILEDGET || ret == FCE_MIRRORNOTSYNC)
233
 
            {
234
 
                if (try < maxattempts)
235
 
                {
236
 
                    logg ("Trying again in 5 secs...\n");
237
 
                    try++;
238
 
                    sleep (5);
239
 
                    continue;
240
 
                }
241
 
                else
242
 
                {
243
 
                    logg ("Giving up on %s...\n", opt->strarg);
244
 
                    opt = (struct optstruct *) opt->nextarg;
245
 
                    if (!opt)
246
 
                    {
247
 
                        logg ("Update failed. Your network may be down or none of the mirrors listed in %s is working. Check http://www.clamav.net/documentation.html for possible reasons.\n", cfgfile);
248
 
                    }
249
 
                }
250
 
 
251
 
            }
252
 
            else
253
 
            {
254
 
                return ret;
255
 
            }
256
 
        }
257
 
    }
258
 
 
259
 
    return ret;
260
 
}
261
 
 
262
 
static void
263
 
msg_callback (enum cl_msg severity, const char *fullmsg, const char *msg,
264
 
              void *ctx)
265
 
{
266
 
    UNUSEDPARAM(fullmsg);
267
 
    UNUSEDPARAM(ctx);
268
 
 
269
 
    switch (severity)
270
 
    {
271
 
    case CL_MSG_ERROR:
272
 
        logg ("^[LibClamAV] %s", msg);
273
 
        break;
274
 
    case CL_MSG_WARN:
275
 
        logg ("~[LibClamAV] %s", msg);
276
 
        break;
277
 
    default:
278
 
        logg ("*[LibClamAV] %s", msg);
279
 
        break;
280
 
    }
281
 
}
282
 
 
283
 
int
284
 
main (int argc, char **argv)
285
 
{
286
 
    int ret = FCE_CONNECTION, retcl;
287
 
    const char *cfgfile, *arg = NULL;
288
 
    char *pt;
289
 
    struct optstruct *opts;
290
 
    const struct optstruct *opt;
291
 
#ifndef _WIN32
292
 
    struct sigaction sigact;
293
 
    struct sigaction oldact;
294
 
#endif
295
 
#ifdef HAVE_PWD_H
296
 
    const char *dbowner;
297
 
    struct passwd *user;
298
 
#endif
299
 
    STATBUF statbuf;
300
 
    struct mirdat mdat;
301
 
 
302
 
    if (check_flevel ())
303
 
        exit (FCE_INIT);
304
 
 
305
 
    if ((retcl = cl_init (CL_INIT_DEFAULT)))
306
 
    {
307
 
        mprintf ("!Can't initialize libclamav: %s\n", cl_strerror (retcl));
308
 
        return FCE_INIT;
309
 
    }
310
 
 
311
 
    if ((opts =
312
 
         optparse (NULL, argc, argv, 1, OPT_FRESHCLAM, 0, NULL)) == NULL)
313
 
    {
314
 
        mprintf ("!Can't parse command line options\n");
315
 
        return FCE_INIT;
316
 
    }
317
 
 
318
 
    if (optget (opts, "help")->enabled)
319
 
    {
320
 
        help ();
321
 
        optfree (opts);
322
 
        return 0;
323
 
    }
324
 
 
325
 
    /* parse the config file */
326
 
    cfgfile = optget (opts, "config-file")->strarg;
327
 
    pt = strdup (cfgfile);
328
 
    if ((opts =
329
 
         optparse (cfgfile, 0, NULL, 1, OPT_FRESHCLAM, 0, opts)) == NULL)
330
 
    {
331
 
        fprintf (stderr, "ERROR: Can't open/parse the config file %s\n", pt);
332
 
        free (pt);
333
 
        return FCE_INIT;
334
 
    }
335
 
    free (pt);
336
 
 
337
 
    if (optget (opts, "version")->enabled)
338
 
    {
339
 
        print_version (optget (opts, "DatabaseDirectory")->strarg);
340
 
        optfree (opts);
341
 
        return 0;
342
 
    }
343
 
 
344
 
    /* Stats/intelligence gathering  */
345
 
    if (optget(opts, "stats-host-id")->enabled) {
346
 
        char *p = optget(opts, "stats-host-id")->strarg;
347
 
 
348
 
        if (strcmp(p, "default")) {
349
 
            if (!strcmp(p, "anonymous")) {
350
 
                strcpy(hostid, STATS_ANON_UUID);
351
 
            } else {
352
 
                if (strlen(p) > 36) {
353
 
                    logg("!Invalid HostID\n");
354
 
                    optfree(opts);
355
 
                    return FCE_INIT;
356
 
                }
357
 
 
358
 
                strcpy(hostid, p);
359
 
            }
360
 
        } else {
361
 
            strcpy(hostid, "default");
362
 
        }
363
 
    }
364
 
    submit_host_info(opts);
365
 
 
366
 
    if (optget (opts, "HTTPProxyPassword")->enabled)
367
 
    {
368
 
        if (CLAMSTAT (cfgfile, &statbuf) == -1)
369
 
        {
370
 
            logg ("^Can't stat %s (critical error)\n", cfgfile);
371
 
            optfree (opts);
372
 
            return FCE_CONFIG;
373
 
        }
374
 
 
375
 
#ifndef _WIN32
376
 
        if (statbuf.
377
 
            st_mode & (S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH |
378
 
                       S_IXOTH))
379
 
        {
380
 
            logg ("^Insecure permissions (for HTTPProxyPassword): %s must have no more than 0700 permissions.\n", cfgfile);
381
 
            optfree (opts);
382
 
            return FCE_CONFIG;
383
 
        }
384
 
#endif
385
 
    }
386
 
 
387
 
#ifdef HAVE_PWD_H
388
 
    /* freshclam shouldn't work with root privileges */
389
 
    dbowner = optget (opts, "DatabaseOwner")->strarg;
390
 
 
391
 
    if (!geteuid ())
392
 
    {
393
 
        if ((user = getpwnam (dbowner)) == NULL)
394
 
        {
395
 
            logg ("^Can't get information about user %s.\n", dbowner);
396
 
            optfree (opts);
397
 
            return FCE_USERINFO;
398
 
        }
399
 
 
400
 
        if (optget (opts, "AllowSupplementaryGroups")->enabled)
401
 
        {
402
 
#ifdef HAVE_INITGROUPS
403
 
            if (initgroups (dbowner, user->pw_gid))
404
 
            {
405
 
                logg ("^initgroups() failed.\n");
406
 
                optfree (opts);
407
 
                return FCE_USERORGROUP;
408
 
            }
409
 
#endif
410
 
        }
411
 
        else
412
 
        {
413
 
#ifdef HAVE_SETGROUPS
414
 
            if (setgroups (1, &user->pw_gid))
415
 
            {
416
 
                logg ("^setgroups() failed.\n");
417
 
                optfree (opts);
418
 
                return FCE_USERORGROUP;
419
 
            }
420
 
#endif
421
 
        }
422
 
 
423
 
        if (setgid (user->pw_gid))
424
 
        {
425
 
            logg ("^setgid(%d) failed.\n", (int) user->pw_gid);
426
 
            optfree (opts);
427
 
            return FCE_USERORGROUP;
428
 
        }
429
 
 
430
 
        if (setuid (user->pw_uid))
431
 
        {
432
 
            logg ("^setuid(%d) failed.\n", (int) user->pw_uid);
433
 
            optfree (opts);
434
 
            return FCE_USERORGROUP;
435
 
        }
436
 
    }
437
 
#endif /* HAVE_PWD_H */
438
 
 
439
 
    /* initialize some important variables */
440
 
 
441
 
    if (optget (opts, "Debug")->enabled || optget (opts, "debug")->enabled)
442
 
        cl_debug ();
443
 
 
444
 
    if (optget (opts, "verbose")->enabled)
445
 
        mprintf_verbose = 1;
446
 
 
447
 
    if (optget (opts, "quiet")->enabled)
448
 
        mprintf_quiet = 1;
449
 
 
450
 
    if (optget (opts, "no-warnings")->enabled)
451
 
    {
452
 
        mprintf_nowarn = 1;
453
 
        logg_nowarn = 1;
454
 
    }
455
 
 
456
 
    if (optget (opts, "stdout")->enabled)
457
 
        mprintf_stdout = 1;
458
 
 
459
 
    /* initialize logger */
460
 
    logg_verbose = mprintf_verbose ? 1 : optget (opts, "LogVerbose")->enabled;
461
 
    logg_time = optget (opts, "LogTime")->enabled;
462
 
    logg_size = optget (opts, "LogFileMaxSize")->numarg;
463
 
    if (logg_size)
464
 
        logg_rotate = optget(opts, "LogRotate")->enabled;
465
 
 
466
 
    if ((opt = optget (opts, "UpdateLogFile"))->enabled)
467
 
    {
468
 
        logg_file = opt->strarg;
469
 
        if (logg ("#--------------------------------------\n"))
470
 
        {
471
 
            mprintf ("!Problem with internal logger (UpdateLogFile = %s).\n",
472
 
                     logg_file);
473
 
            optfree (opts);
474
 
            return FCE_LOGGING;
475
 
        }
476
 
    }
477
 
    else
478
 
        logg_file = NULL;
479
 
 
480
 
#if defined(USE_SYSLOG) && !defined(C_AIX)
481
 
    if (optget (opts, "LogSyslog")->enabled)
482
 
    {
483
 
        int fac = LOG_LOCAL6;
484
 
 
485
 
        if ((opt = optget (opts, "LogFacility"))->enabled)
486
 
        {
487
 
            if ((fac = logg_facility (opt->strarg)) == -1)
488
 
            {
489
 
                mprintf ("!LogFacility: %s: No such facility.\n",
490
 
                         opt->strarg);
491
 
                optfree (opts);
492
 
                return FCE_LOGGING;
493
 
            }
494
 
        }
495
 
 
496
 
        openlog ("freshclam", LOG_PID, fac);
497
 
        logg_syslog = 1;
498
 
    }
499
 
#endif
500
 
 
501
 
    cl_set_clcb_msg (msg_callback);
502
 
    /* change the current working directory */
503
 
    if (chdir (optget (opts, "DatabaseDirectory")->strarg))
504
 
    {
505
 
        logg ("!Can't change dir to %s\n",
506
 
              optget (opts, "DatabaseDirectory")->strarg);
507
 
        optfree (opts);
508
 
        return FCE_DIRECTORY;
509
 
    }
510
 
    else
511
 
    {
512
 
        if (!getcwd (dbdir, sizeof (dbdir)))
513
 
        {
514
 
            logg ("!getcwd() failed\n");
515
 
            optfree (opts);
516
 
            return FCE_DIRECTORY;
517
 
        }
518
 
        logg ("*Current working dir is %s\n", dbdir);
519
 
    }
520
 
 
521
 
 
522
 
    if (optget (opts, "list-mirrors")->enabled)
523
 
    {
524
 
        if (mirman_read ("mirrors.dat", &mdat, 1) == -1)
525
 
        {
526
 
            printf ("Can't read mirrors.dat\n");
527
 
            optfree (opts);
528
 
            return FCE_FILE;
529
 
        }
530
 
        mirman_list (&mdat);
531
 
        mirman_free (&mdat);
532
 
        optfree (opts);
533
 
        return 0;
534
 
    }
535
 
 
536
 
    if ((opt = optget (opts, "PrivateMirror"))->enabled)
537
 
    {
538
 
        struct optstruct *dbm, *opth;
539
 
 
540
 
        dbm = (struct optstruct *) optget (opts, "DatabaseMirror");
541
 
        dbm->active = dbm->enabled = 1;
542
 
        do
543
 
        {
544
 
            if (cli_strbcasestr (opt->strarg, ".clamav.net"))
545
 
            {
546
 
                logg ("!PrivateMirror: *.clamav.net is not allowed in this mode\n");
547
 
                optfree (opts);
548
 
                return FCE_PRIVATEMIRROR;
549
 
            }
550
 
 
551
 
            if (dbm->strarg)
552
 
                free (dbm->strarg);
553
 
            dbm->strarg = strdup (opt->strarg);
554
 
            if (!dbm->strarg)
555
 
            {
556
 
                logg ("!strdup() failed\n");
557
 
                optfree (opts);
558
 
                return FCE_MEM;
559
 
            }
560
 
            if (!dbm->nextarg)
561
 
            {
562
 
                dbm->nextarg =
563
 
                    (struct optstruct *) calloc (1,
564
 
                                                 sizeof (struct optstruct));
565
 
                if (!dbm->nextarg)
566
 
                {
567
 
                    logg ("!calloc() failed\n");
568
 
                    optfree (opts);
569
 
                    return FCE_MEM;
570
 
                }
571
 
            }
572
 
            opth = dbm;
573
 
            dbm = dbm->nextarg;
574
 
        }
575
 
        while ((opt = opt->nextarg));
576
 
 
577
 
        opth->nextarg = NULL;
578
 
        while (dbm)
579
 
        {
580
 
            free (dbm->name);
581
 
            free (dbm->cmd);
582
 
            free (dbm->strarg);
583
 
            opth = dbm;
584
 
            dbm = dbm->nextarg;
585
 
            free (opth);
586
 
        }
587
 
 
588
 
        /* disable DNS db checks */
589
 
        opth = (struct optstruct *) optget (opts, "no-dns");
590
 
        opth->active = opth->enabled = 1;
591
 
 
592
 
        /* disable scripted updates */
593
 
        opth = (struct optstruct *) optget (opts, "ScriptedUpdates");
594
 
        opth->active = opth->enabled = 0;
595
 
    }
596
 
 
597
 
    *updtmpdir = 0;
598
 
 
599
 
#ifdef _WIN32
600
 
    signal (SIGINT, sighandler);
601
 
#else
602
 
    memset (&sigact, 0, sizeof (struct sigaction));
603
 
    sigact.sa_handler = sighandler;
604
 
    sigaction (SIGINT, &sigact, NULL);
605
 
    sigaction (SIGPIPE, &sigact, NULL);
606
 
#endif
607
 
    if (optget (opts, "daemon")->enabled)
608
 
    {
609
 
        int bigsleep, checks;
610
 
#ifndef _WIN32
611
 
        time_t now, wakeup;
612
 
 
613
 
        sigaction (SIGTERM, &sigact, NULL);
614
 
        sigaction (SIGHUP, &sigact, NULL);
615
 
        sigaction (SIGCHLD, &sigact, NULL);
616
 
#endif
617
 
 
618
 
        checks = optget (opts, "Checks")->numarg;
619
 
 
620
 
        if (checks <= 0)
621
 
        {
622
 
            logg ("^Number of checks must be a positive integer.\n");
623
 
            optfree (opts);
624
 
            return FCE_CHECKS;
625
 
        }
626
 
 
627
 
        if (!optget (opts, "DNSDatabaseInfo")->enabled
628
 
            || optget (opts, "no-dns")->enabled)
629
 
        {
630
 
            if (checks > 50)
631
 
            {
632
 
                logg ("^Number of checks must be between 1 and 50.\n");
633
 
                optfree (opts);
634
 
                return FCE_CHECKS;
635
 
            }
636
 
        }
637
 
 
638
 
        bigsleep = 24 * 3600 / checks;
639
 
 
640
 
#ifndef _WIN32
641
 
        if (!optget (opts, "Foreground")->enabled)
642
 
        {
643
 
            if (daemonize () == -1)
644
 
            {
645
 
                logg ("!daemonize() failed\n");
646
 
                optfree (opts);
647
 
                return FCE_FAILEDUPDATE;
648
 
            }
649
 
            foreground = 0;
650
 
            mprintf_disabled = 1;
651
 
        }
652
 
#endif
653
 
 
654
 
        if ((opt = optget (opts, "PidFile"))->enabled)
655
 
        {
656
 
            pidfile = opt->strarg;
657
 
            writepid (pidfile);
658
 
        }
659
 
 
660
 
        active_children = 0;
661
 
 
662
 
        logg ("#freshclam daemon %s (OS: " TARGET_OS_TYPE ", ARCH: "
663
 
              TARGET_ARCH_TYPE ", CPU: " TARGET_CPU_TYPE ")\n",
664
 
              get_version ());
665
 
 
666
 
        while (!terminate)
667
 
        {
668
 
            ret = download (opts, cfgfile);
669
 
 
670
 
            if (ret > 1)
671
 
            {
672
 
                if ((opt = optget (opts, "OnErrorExecute"))->enabled)
673
 
                    arg = opt->strarg;
674
 
 
675
 
                if (arg)
676
 
                    execute ("OnErrorExecute", arg, opts);
677
 
 
678
 
                arg = NULL;
679
 
            }
680
 
 
681
 
            logg ("#--------------------------------------\n");
682
 
#ifdef  SIGALRM
683
 
            sigaction (SIGALRM, &sigact, &oldact);
684
 
#endif
685
 
#ifdef  SIGUSR1
686
 
            sigaction (SIGUSR1, &sigact, &oldact);
687
 
#endif
688
 
 
689
 
#ifdef  _WIN32
690
 
            sleep (bigsleep);
691
 
#else
692
 
            time (&wakeup);
693
 
            wakeup += bigsleep;
694
 
            alarm (bigsleep);
695
 
            do
696
 
            {
697
 
                pause ();
698
 
                time (&now);
699
 
            }
700
 
            while (!terminate && now < wakeup);
701
 
 
702
 
            if (terminate == -1)
703
 
            {
704
 
                logg ("Received signal: wake up\n");
705
 
                terminate = 0;
706
 
            }
707
 
            else if (terminate == -2)
708
 
            {
709
 
                logg ("Received signal: re-opening log file\n");
710
 
                terminate = 0;
711
 
                logg_close ();
712
 
            }
713
 
#endif
714
 
 
715
 
#ifdef  SIGALRM
716
 
            sigaction (SIGALRM, &oldact, NULL);
717
 
#endif
718
 
#ifdef  SIGUSR1
719
 
            sigaction (SIGUSR1, &oldact, NULL);
720
 
#endif
721
 
        }
722
 
 
723
 
    }
724
 
    else
725
 
    {
726
 
        ret = download (opts, cfgfile);
727
 
    }
728
 
 
729
 
    if (ret > 1)
730
 
    {
731
 
        if ((opt = optget (opts, "OnErrorExecute"))->enabled)
732
 
            execute ("OnErrorExecute", opt->strarg, opts);
733
 
    }
734
 
 
735
 
    if (pidfile)
736
 
    {
737
 
        unlink (pidfile);
738
 
    }
739
 
 
740
 
    optfree (opts);
741
 
 
742
 
    cl_cleanup_crypto();
743
 
 
744
 
    return (ret);
745
 
}
746
 
 
747
 
void submit_host_info(struct optstruct *opts)
748
 
{
749
 
    struct cl_engine *engine;
750
 
    cli_intel_t *intel;
751
 
 
752
 
    if (!optget(opts, "enable-stats")->enabled)
753
 
        return;
754
 
 
755
 
    engine = cl_engine_new();
756
 
    if (!(engine))
757
 
        return;
758
 
 
759
 
    if (optget (opts, "Debug")->enabled || optget (opts, "debug")->enabled)
760
 
        cl_debug ();
761
 
 
762
 
    if (optget (opts, "verbose")->enabled)
763
 
        mprintf_verbose = 1;
764
 
 
765
 
    cl_engine_stats_enable(engine);
766
 
 
767
 
    intel = engine->stats_data;
768
 
    if (!(intel)) {
769
 
        engine->cb_stats_submit = NULL;
770
 
        cl_engine_free(engine);
771
 
        return;
772
 
    }
773
 
 
774
 
    intel->host_info = calloc(1, strlen(TARGET_OS_TYPE) + strlen(TARGET_ARCH_TYPE) + 2);
775
 
    if (!(intel->host_info)) {
776
 
        engine->cb_stats_submit = NULL;
777
 
        cl_engine_free(engine);
778
 
        return;
779
 
    }
780
 
 
781
 
    sprintf(intel->host_info, "%s %s", TARGET_OS_TYPE, TARGET_ARCH_TYPE);
782
 
 
783
 
    if (!strcmp(hostid, "none"))
784
 
        cl_engine_set_clcb_stats_get_hostid(engine, NULL);
785
 
    else if (strcmp(hostid, "default"))
786
 
        cl_engine_set_clcb_stats_get_hostid(engine, get_hostid);
787
 
 
788
 
    if (optget(opts, "stats-timeout")->enabled) {
789
 
        cl_engine_set_num(engine, CL_ENGINE_STATS_TIMEOUT, optget(opts, "StatsTimeout")->numarg);
790
 
    }
791
 
 
792
 
    cl_engine_free(engine);
793
 
}
794
 
 
795
 
int is_valid_hostid(void)
796
 
{
797
 
    int count, i;
798
 
 
799
 
    if (strlen(hostid) != 36)
800
 
        return 0;
801
 
 
802
 
    count=0;
803
 
    for (i=0; i < 36; i++)
804
 
        if (hostid[i] == '-')
805
 
            count++;
806
 
 
807
 
    if (count != 4)
808
 
        return 0;
809
 
 
810
 
    if (hostid[8] != '-' || hostid[13] != '-' || hostid[18] != '-' || hostid[23] != '-')
811
 
        return 0;
812
 
 
813
 
    return 1;
814
 
}
815
 
 
816
 
char *get_hostid(void *cbdata)
817
 
{
818
 
    UNUSEDPARAM(cbdata);
819
 
 
820
 
    if (!strcmp(hostid, "none"))
821
 
        return NULL;
822
 
 
823
 
    if (!is_valid_hostid())
824
 
        return strdup(STATS_ANON_UUID);
825
 
 
826
 
    logg("HostID is valid: %s\n", hostid);
827
 
 
828
 
    return strdup(hostid);
829
 
}