~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/others.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2007-2008 Sourcefire, Inc.
 
2
 *  Copyright (C) 2007-2010 Sourcefire, Inc.
3
3
 *
4
4
 *  Authors: Tomasz Kojm, Trog
5
5
 *
33
33
#endif
34
34
#include <sys/types.h>
35
35
#include <sys/stat.h>
36
 
#ifndef C_WINDOWS
 
36
#include <dirent.h>
 
37
#ifndef _WIN32
37
38
#include <sys/wait.h>
38
39
#include <sys/time.h>
39
 
#include <dirent.h>
40
40
#endif
41
41
#include <time.h>
42
42
#include <fcntl.h>
43
 
#ifndef C_WINDOWS
 
43
#ifdef HAVE_PWD_H
44
44
#include <pwd.h>
45
45
#endif
46
46
#include <errno.h>
47
47
#include "target.h"
48
 
#ifndef C_WINDOWS
49
 
#include <sys/time.h>
50
 
#endif
51
48
#ifdef  HAVE_SYS_PARAM_H
52
49
#include <sys/param.h>
53
50
#endif
54
51
#ifdef  HAVE_MALLOC_H
55
52
#include <malloc.h>
56
53
#endif
57
 
#if     defined(_MSC_VER) && defined(_DEBUG)
58
 
#include <crtdbg.h>
59
 
#endif
60
54
 
61
55
#if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
62
56
#include <limits.h>
71
65
#include "ltdl.h"
72
66
#include "matcher-ac.h"
73
67
#include "default.h"
74
 
 
75
 
#ifndef O_BINARY
76
 
#define O_BINARY        0
77
 
#endif
78
 
 
79
 
#ifdef        C_WINDOWS
80
 
#undef        P_tmpdir
81
 
#define       P_tmpdir        "C:\\WINDOWS\\TEMP"
82
 
#endif
 
68
#include "scanners.h"
 
69
#include "bytecode.h"
83
70
 
84
71
int (*cli_unrar_open)(int fd, const char *dirname, unrar_state_t *state);
85
72
int (*cli_unrar_extract_next_prepare)(unrar_state_t *state, const char *dirname);
134
121
    const lt_dlinfo *info;
135
122
    char modulename[128];
136
123
    lt_dlhandle rhandle;
137
 
    int canretry=1;
138
124
    unsigned i;
139
125
 
140
126
    if (lt_dladdsearchdir(SEARCH_LIBDIR)) {
260
246
            return "CL_EMAXFILES";
261
247
        case CL_EFORMAT:
262
248
            return "CL_EFORMAT: Bad format or broken data";
 
249
        case CL_EBYTECODE:
 
250
            return "CL_EBYTECODE: error during bytecode execution";
263
251
        default:
264
252
            return "Unknown error code";
265
253
    }
267
255
 
268
256
int cl_init(unsigned int initoptions)
269
257
{
 
258
        int rc;
270
259
        struct timeval tv;
271
260
        unsigned int pid = (unsigned int) getpid();
272
261
 
 
262
    {
 
263
        unrar_main_header_t x;
 
264
        if (((char*)&x.flags - (char*)&x) != 3) {
 
265
            cli_errmsg("Structure packing not working, got %u offset, expected %u\n",
 
266
                       (unsigned)((char*)&x.flags - (char*)&x), 3);
 
267
            return CL_EARG;
 
268
        }
 
269
    }
273
270
    /* put dlopen() stuff here, etc. */
274
271
    if (lt_init() == 0) {
275
272
        cli_rarload();
276
273
    }
277
274
    gettimeofday(&tv, (struct timezone *) 0);
278
275
    srand(pid + tv.tv_usec*(pid+1) + clock());
 
276
    rc = bytecode_init();
 
277
    if (rc)
 
278
        return rc;
279
279
    return CL_SUCCESS;
280
280
}
281
281
 
298
298
    new->min_cc_count = CLI_DEFAULT_MIN_CC_COUNT;
299
299
    new->min_ssn_count = CLI_DEFAULT_MIN_SSN_COUNT;
300
300
 
 
301
    new->bytecode_security = CL_BYTECODE_TRUST_SIGNED;
301
302
    new->refcount = 1;
302
303
    new->ac_only = 0;
303
304
    new->ac_mindepth = CLI_DEFAULT_AC_MINDEPTH;
380
381
        case CL_ENGINE_KEEPTMP:
381
382
            engine->keeptmp = num;
382
383
            break;
 
384
        case CL_ENGINE_BYTECODE_SECURITY:
 
385
#ifndef CL_DEBUG
 
386
            if (num == CL_BYTECODE_TRUST_ALL) {
 
387
                cli_errmsg("cl_engine_set_num: CL_BYTECODE_TRUST_ALL is only supported when ClamAV is built in debug mode\n");
 
388
                return CL_EARG;
 
389
            }
 
390
#endif
 
391
            engine->bytecode_security = num;
 
392
            break;
383
393
        default:
384
394
            cli_errmsg("cl_engine_set_num: Incorrect field number\n");
385
395
            return CL_EARG;
787
797
#else
788
798
            while((dent = readdir(dd))) {
789
799
#endif
790
 
#if     (!defined(C_INTERIX)) && (!defined(C_WINDOWS))
791
800
                if(dent->d_ino)
792
 
#endif
793
801
                {
794
802
                    if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
795
803
                        path = cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
798
806
                            return -1;
799
807
                        }
800
808
 
801
 
                        sprintf(path, "%s/%s", dirname, dent->d_name);
 
809
                        sprintf(path, "%s"PATHSEP"%s", dirname, dent->d_name);
802
810
 
803
811
                        /* stat the file */
804
812
                        if(lstat(path, &statbuf) != -1) {
841
849
}
842
850
#endif
843
851
 
 
852
int cli_dumpscan(int fd, off_t offset, size_t size, cli_ctx *ctx)
 
853
{
 
854
        int newfd, bread, sum = 0, ret;
 
855
        char buff[FILEBUFF];
 
856
        char *name;
 
857
 
 
858
    if(offset) {
 
859
        if(lseek(fd, offset, SEEK_SET) == -1) {
 
860
            cli_dbgmsg("cli_dumpscan: Can't lseek to %u\n", (unsigned int) offset);
 
861
            return CL_EFORMAT; /* most likely because of corrupted file */
 
862
        }
 
863
    }
 
864
 
 
865
    if(!(name = cli_gentemp(ctx->engine->tmpdir)))
 
866
        return CL_EMEM;
 
867
 
 
868
    if((newfd = open(name, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IRWXU)) < 0) {
 
869
        cli_errmsg("cli_dumpscan: Can't create file %s\n", name);
 
870
        free(name);
 
871
        return CL_ECREAT;
 
872
    }
 
873
 
 
874
    while((bread = cli_readn(fd, buff, FILEBUFF)) > 0) {
 
875
        if((uint32_t) (sum + bread) >= size) {
 
876
            if(write(newfd, buff, size - sum) == -1) {
 
877
                cli_errmsg("cli_dumpscan: Can't write to %s\n", name);
 
878
                cli_unlink(name);
 
879
                free(name);
 
880
                close(newfd);
 
881
                return CL_EWRITE;
 
882
            }
 
883
            break;
 
884
        } else {
 
885
            if(write(newfd, buff, bread) == -1) {
 
886
                cli_errmsg("cli_dumpscan: Can't write to %s\n", name);
 
887
                cli_unlink(name);
 
888
                free(name);
 
889
                close(newfd);
 
890
                return CL_EWRITE;
 
891
            }
 
892
        }
 
893
        sum += bread;
 
894
    }
 
895
    cli_dbgmsg("DUMP&SCAN: File extracted to %s\n", name);
 
896
    lseek(newfd, 0, SEEK_SET);
 
897
    if((ret = cli_magic_scandesc(newfd, ctx)) == CL_VIRUS)
 
898
        cli_dbgmsg("cli_dumpscan: Infected with %s\n", *ctx->virname);
 
899
 
 
900
    close(newfd);
 
901
    if(!ctx->engine->keeptmp) {
 
902
        if(cli_unlink(name)) {
 
903
            free(name);
 
904
            return CL_EUNLINK;
 
905
        }
 
906
    }
 
907
    free(name);
 
908
    return ret;
 
909
}
 
910
 
844
911
/* Implement a generic bitset, trog@clamav.net */
845
912
 
846
913
#define BITS_PER_CHAR (8)
847
914
#define BITSET_DEFAULT_SIZE (1024)
848
 
#define FALSE (0)
849
 
#define TRUE (1)
850
915
 
851
916
static unsigned long nearest_power(unsigned long num)
852
917
{
930
995
        }
931
996
        return (bs->bitset[char_offset] & ((unsigned char)1 << bit_offset));
932
997
}
933