~ubuntu-branches/ubuntu/saucy/clamav/saucy-backports

« back to all changes in this revision

Viewing changes to .pc/0010-Call-cl_initialize_crypto-in-cl_init.patch/freshclam/freshclam.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-07-15 01:08:10 UTC
  • mfrom: (0.35.47 sid)
  • Revision ID: package-import@ubuntu.com-20140715010810-ru66ek4fun2iseba
Tags: 0.98.4+dfsg-2~ubuntu13.10.1
No-change backport to saucy (LP: #1341962)

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