~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/pdf.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:
26
26
#include "clamav-config.h"
27
27
#endif
28
28
 
29
 
#ifdef  HAVE_MMAP
30
29
#include <stdio.h>
31
30
#include <sys/types.h>
32
31
#include <sys/stat.h>
41
40
#ifdef  HAVE_UNISTD_H
42
41
#include <unistd.h>
43
42
#endif
44
 
 
45
 
#ifdef HAVE_SYS_MMAN_H
46
 
#include <sys/mman.h>
47
 
#endif
48
 
 
49
43
#include <zlib.h>
50
44
 
51
 
#ifdef  C_WINDOWS
52
 
#include <io.h>
53
 
#endif
54
 
 
55
45
#include "clamav.h"
56
46
#include "others.h"
57
 
#include "mbox.h"
58
47
#include "pdf.h"
59
48
#include "scanners.h"
 
49
#include "fmap.h"
60
50
#include "str.h"
61
51
 
62
 
#ifndef O_BINARY
63
 
#define O_BINARY        0
64
 
#endif
65
 
 
66
52
#ifdef  CL_DEBUG
67
53
/*#define       SAVE_TMP        
68
54
 *Save the file being worked on in tmp */
74
60
static  const   char    *pdf_nextlinestart(const char *ptr, size_t len);
75
61
static  const   char    *pdf_nextobject(const char *ptr, size_t len);
76
62
 
77
 
/*
78
 
 * TODO: handle embedded URLs if (options&CL_SCAN_MAILURL)
79
 
 */
80
63
int
81
 
cli_pdf(const char *dir, int desc, cli_ctx *ctx, off_t offset)
 
64
cli_pdf(const char *dir, cli_ctx *ctx, off_t offset)
82
65
{
83
66
        off_t size;     /* total number of bytes in the file */
84
67
        off_t bytesleft, trailerlength;
86
69
        const char *p, *q, *trailerstart;
87
70
        const char *xrefstart;  /* cross reference table */
88
71
        /*size_t xreflength;*/
89
 
        table_t *md5table;
90
72
        int printed_predictor_message, printed_embedded_font_message, rc;
91
73
        unsigned int files;
92
 
        struct stat statb;
 
74
        fmap_t *map = *ctx->fmap;
93
75
        int opt_failed = 0;
94
76
 
95
77
        cli_dbgmsg("in cli_pdf(%s)\n", dir);
96
78
 
97
 
        if(fstat(desc, &statb) < 0) {
98
 
                cli_errmsg("cli_pdf: fstat() failed\n");
99
 
                return CL_EOPEN;
100
 
        }
101
 
 
102
 
        size = statb.st_size - offset;
 
79
        size = map->len - offset;
103
80
 
104
81
        if(size <= 7)   /* doesn't even include the file header */
105
82
                return CL_CLEAN;
106
83
 
107
 
        p = buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, desc, offset);
108
 
        if(buf == MAP_FAILED) {
 
84
        p = buf = fmap_need_off_once(map, 0, size); /* FIXME: really port to fmap */
 
85
        if(!buf) {
109
86
                cli_errmsg("cli_pdf: mmap() failed\n");
110
87
                return CL_EMAP;
111
88
        }
125
102
        }
126
103
 
127
104
        if(!bytesleft) {
128
 
            munmap(buf, size);
129
105
            cli_dbgmsg("cli_pdf: file header not found\n");
130
106
            return CL_CLEAN;
131
107
        }
136
112
                        break;
137
113
 
138
114
        if(q <= p) {
139
 
                munmap(buf, size);
140
115
                cli_dbgmsg("cli_pdf: trailer not found\n");
141
116
                return CL_CLEAN;
142
117
        }
155
130
                 * http://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt
156
131
                 * http://www.adobe.com/devnet/pdf/
157
132
                 */
158
 
                munmap(buf, size);
159
133
                cli_dbgmsg("cli_pdf: Encrypted PDF files not yet supported\n");
160
134
                return CL_CLEAN;
161
135
        }
178
152
                                break;
179
153
 
180
154
        if(xrefstart == p) {
181
 
                munmap(buf, size);
182
155
                cli_dbgmsg("cli_pdf: xref not found\n");
183
156
                return CL_CLEAN;
184
157
        }
185
158
 
186
159
        printed_predictor_message = printed_embedded_font_message = 0;
187
160
 
188
 
        md5table = tableCreate();
189
161
        /*
190
162
         * not true, since edits may put data after the trailer
191
163
        xreflength = (size_t)(trailerstart - xrefstart);
204
176
                int is_ascii85decode, is_flatedecode, fout, len, has_cr;
205
177
                /*int object_number, generation_number;*/
206
178
                const char *objstart, *objend, *streamstart, *streamend;
207
 
                unsigned char *md5digest;
208
179
                unsigned long length, objlen, real_streamlen, calculated_streamlen;
209
180
                int is_embedded_font, predictor;
210
181
                char fullname[NAME_MAX + 1];
278
249
                                         */
279
250
                                        if((bytesleft > 11) && strncmp(q, " 0 R", 4) == 0) {
280
251
                                                const char *r, *nq;
281
 
                                                size_t len;
282
252
                                                char b[14];
283
253
 
284
254
                                                q += 4;
399
369
                        has_cr = 1;
400
370
                } else
401
371
                        has_cr = 0;
402
 
                snprintf(fullname, sizeof(fullname), "%s/pdf%02u", dir, files);
 
372
                snprintf(fullname, sizeof(fullname), "%s"PATHSEP"pdf%02u", dir, files);
403
373
                fout = open(fullname, O_RDWR|O_CREAT|O_EXCL|O_TRUNC|O_BINARY, 0600);
404
374
                if(fout < 0) {
405
375
                        char err[128];
522
492
                        files++;
523
493
        
524
494
                        lseek(fout, 0, SEEK_SET);
525
 
                        if((md5digest = cli_md5digest(fout))) {
526
 
                                unsigned int i;
527
 
                                char md5str[33];
528
 
 
529
 
                                for(i = 0; i < 16; i++)
530
 
                                        sprintf(md5str + 2*i, "%02x", md5digest[i]);
531
 
                                md5str[32] = 0;
532
 
                                free(md5digest);
533
 
 
534
 
                                if(tableFind(md5table, md5str) >= 0) {
535
 
                                        cli_dbgmsg("cli_pdf: not scanning duplicate embedded file '%s'\n", fullname);
536
 
                                        ctx->scannedfiles++;
537
 
                                        close(fout);
538
 
                                        if (cli_unlink(fullname)) {
539
 
                                                rc = CL_EUNLINK;
540
 
                                                break;
541
 
                                        }
542
 
                                        if(cli_updatelimits(ctx, real_streamlen) != CL_SUCCESS) {
543
 
                                                rc = CL_CLEAN;
544
 
                                                break;
545
 
                                        }
546
 
                                        continue;
547
 
                                } else
548
 
                                        tableInsert(md5table, md5str, 1);
549
 
                        }
550
 
 
551
 
                        lseek(fout, 0, SEEK_SET);
552
495
                        rc = cli_magic_scandesc(fout, ctx);
553
496
                }
554
497
                close(fout);
557
500
                if(rc != CL_CLEAN) break;
558
501
        }
559
502
 
560
 
        munmap(buf, size);
561
 
 
562
 
        tableDestroy(md5table);
563
503
 
564
504
        cli_dbgmsg("cli_pdf: returning %d\n", rc);
565
505
        return rc;
853
793
        }
854
794
        return NULL;
855
795
}
856
 
 
857
 
#else   /*!HAVE_MMAP*/
858
 
 
859
 
#include "clamav.h"
860
 
#include "others.h"
861
 
#include "pdf.h"
862
 
 
863
 
int
864
 
cli_pdf(const char *dir, int desc, cli_ctx *ctx, off_t offset)
865
 
{
866
 
        cli_dbgmsg("File not decoded - PDF decoding needs mmap() (for now)\n");
867
 
        return CL_CLEAN;
868
 
}
869
 
#endif