~ubuntu-dev/ubuntu/lucid/dovecot/lucid-201002110912

« back to all changes in this revision

Viewing changes to libsieve/src/lib-sieve/plugins/body/ext-body-common.c

  • Committer: Chuck Short
  • Date: 2010-01-21 20:21:25 UTC
  • mfrom: (4.1.11 squeeze)
  • Revision ID: zulcss@ubuntu.com-20100121202125-pme73o491kfwj5nc
* Merge from debian testing, remaining changes:
  + Add new binary pkg dovecot-postfix that integrates postfix and dovecot
    automatically: (LP: #164837)
  + debian/control:
    - add new binary with short description
    - set Architecture all for dovecot-postfix (LP: #329878)
  + debian/dovecot-postfix.postinst:
    - create initial certificate symlinks to snakeoil.
    - set up postfix with postconf to:
      - use Maildir/ as the default mailbox.
      - use dovecot as the sasl authentication server.
      - use dovecot LDA (deliver).
      - use tls for smtp{d} services.
    - fix certificates paths in postfix' main.cf
    - add reject_unauth_destination to postfix' recipient restrictions
    - add reject_unknown_sender_domain to postfix' sender restriction
    - rename configuration name on remove, delete on purge
    - restart dovecot after linking certificates
    - handle use case when postfix is unconfigurated
  + debian/dovecot-postfix.dirs: create backup directory for postfix's config
    configuration
  + restart postfix and dovecot.
  + debian/dovecot-postfix.postrm:
    - remove all dovecot related configuration from postfix.
    - restart postfix and dovecot.
  + debian/dovecot-common.init:
    - check if /etc/dovecot/dovecot-postfix.conf exists and use it
      as the configuration file if so.
  + debian/patches/warning-ubuntu-postfix.dpatch
    - add warning about dovecot-postfix.conf in dovecot default
      configuration file
  + debian/patches/dovecot-postfix.conf.diff:
    - Ubuntu server custom changes to the default dovecot configuration for
      better interfation with postfix.
    - enable sieve plugin.
    - Ubuntu server custom changes to the default dovecot configuration for
      better integration with postfix:
      - enable imap, pop3, imaps, pop3s and managesieve by default.
      - enable dovecot LDA (deliver).
      - enable SASL auth socket in postfix private directory
   + debian/rules:
     - copy, patch and install dovecot-postfix.conf in /etc/dovecot/.
     - build architecure independent packages too
   + Use Snakeoil SSL certificates by default.
     - debian/control: Depend on ssl-cert.
     - debian/patches/ssl-cert-snakeoil.dpatch: Change default SSL cert
       paths to snakeoil.
     - debian/dovecot-common.postinst: Relax grep for SSL_* a bit.
   + Add autopkgtest to debian/tests/*.
   + Fast TearDown: Update the lsb init header to not stop in level 6.
   + Add ufw integration:
     - Created debian/dovecot-common.ufw.profile.
     - debian/rules: install profile.
     - debian/control: suggest ufw.
   + debian/{control,rules}: enable PIE hardening.
   + dovecot-imapd, dovecot-pop3: Replaces dovecot-common (<< 1:1.1). (LP: #254721)
   + debian/control: Update Vcs-* headers.
   + Add SMTP-AUTH support for Outlook (login auth mechanism)
* New upstream release.
* debian/patches/gold-fix.patch: Removed. Fixed upstream.
* Moved libexec to lib corrections in dovecot-managesieve.patch and
  dovecot-managesieve-dist.patch to dovecot-example.patch
* debian/patches/dovecot-mboxlocking.patch: Regenerated to avoid FTBFS
  when quilt isn't installed.
* debian/patches/quota-mountpoint.patch: Removed. Not needed anymore.
* debian/patches/dovecot-quota.patch: Removed. Quotas aren't properly
  enabled unless mail_plugins = quota imap_quota.
* debian/patches/gold-fix.patch: Fixed configure script to build even
  with binutils-gold or --no-add-needed linker flag (Closes: #554306)
* debian/dovecot-common.init: fixed LSB headers. Thanks to Pascal Volk.
  (Closes: #558040)
* debian/changelog: added CVE references to previous changelog entry.
* debian/rules: checked up the build system. It's not fragile anymore.
  (Closes: 493803)
* debian/dovecot-common.postinst: Now invoking dpkg-reconfigure
  on dovecot-common is enough to generate new certificates
  if the previous ones were removed. (Closes: #545582)
* debian/rules: No longer install convert-tool in /usr/bin.
  It isn't an user utility and it should stay in /usr/lib/dovecot
  like all other similar tool.
* New upstream release. (Closes: #557601)
* [SECURITY] Fixes local information disclosure and denial of service.
  (see: http://www.dovecot.org/list/dovecot-news/2009-November/000143.html
  and CVE-2009-3897)
* Added myself to uploaders.
* Switched to the new source format "3.0 (quilt)":
  - removed dpatch from build-depends
  - removed debian/README.source because now we use only standard
    dpkg features
  - regenerated all patches
* Prepared to switch to multi-origin source:
  - recreated dovecot-libsieve.patch and dovecot-managesieve-dist.patch
    starting from the upstream tarball
  - removed all autotools related build-depends and build-conflict
  - renamed dovecot-libsieve and dovecot-managesieve directories
    to libsieve and managesieve.
* debian/rules: Moved the configuration of libsieve and managesieve from
  the build phase to the configuration phase
* Added dovecot-dbg package  with debugging symbols.  Thanks Stephan Bosch.
  (Closes: #554710)
* Fixed some stray libexec'isms in the default configuration.
* New upstream release.
* debian/dovecot-common.init:
  - use $CONF when starting the daemon. (Closes: #549944)
  - always output start/stop messages. (Closes: #523810)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2002-2009 Dovecot Sieve authors, see the included COPYING file
 
2
 */
 
3
 
 
4
#include "lib.h"
 
5
#include "mempool.h"
 
6
#include "buffer.h"
 
7
#include "array.h"
 
8
#include "str.h"
 
9
#include "istream.h"
 
10
#include "rfc822-parser.h"
 
11
#include "message-date.h"
 
12
#include "message-parser.h"
 
13
#include "message-decoder.h"
 
14
 
 
15
#include "sieve-common.h"
 
16
#include "sieve-message.h"
 
17
#include "sieve-interpreter.h"
 
18
 
 
19
#include "ext-body-common.h"
 
20
 
 
21
/* This implementation is largely borrowed from the original sieve-cmu.c of the 
 
22
 * cmusieve plugin.
 
23
 */
 
24
 
 
25
struct ext_body_part_cached {
 
26
        const char *content_type;
 
27
 
 
28
        const char *raw_body;
 
29
        const char *decoded_body;
 
30
        size_t raw_body_size;
 
31
        size_t decoded_body_size;
 
32
        
 
33
        bool have_body; /* there's the empty end-of-headers line */
 
34
};
 
35
 
 
36
struct ext_body_message_context {
 
37
        pool_t pool;
 
38
        ARRAY_DEFINE(cached_body_parts, struct ext_body_part_cached);
 
39
        ARRAY_DEFINE(return_body_parts, struct ext_body_part);
 
40
        buffer_t *tmp_buffer;
 
41
        buffer_t *raw_body;
 
42
};
 
43
 
 
44
static bool _is_wanted_content_type
 
45
(const char * const *wanted_types, const char *content_type)
 
46
{
 
47
        const char *subtype = strchr(content_type, '/');
 
48
        size_t type_len;
 
49
 
 
50
        type_len = ( subtype == NULL ? strlen(content_type) :
 
51
                (size_t)(subtype - content_type) );
 
52
 
 
53
        i_assert( wanted_types != NULL );
 
54
 
 
55
        for (; *wanted_types != NULL; wanted_types++) {
 
56
                const char *wanted_subtype = strchr(*wanted_types, '/');
 
57
 
 
58
                if (**wanted_types == '\0') {
 
59
                        /* empty string matches everything */
 
60
                        return TRUE;
 
61
                }
 
62
                if (wanted_subtype == NULL) {
 
63
                        /* match only main type */
 
64
                        if (strlen(*wanted_types) == type_len &&
 
65
                          strncasecmp(*wanted_types, content_type, type_len) == 0)
 
66
                                return TRUE;
 
67
                } else {
 
68
                        /* match whole type/subtype */
 
69
                        if (strcasecmp(*wanted_types, content_type) == 0)
 
70
                                return TRUE;
 
71
                }
 
72
        }
 
73
        return FALSE;
 
74
}
 
75
 
 
76
static bool ext_body_get_return_parts
 
77
(struct ext_body_message_context *ctx, const char * const *wanted_types,
 
78
        bool decode_to_plain)
 
79
{
 
80
        const struct ext_body_part_cached *body_parts;
 
81
        unsigned int i, count;
 
82
        struct ext_body_part *return_part;
 
83
 
 
84
        /* Check whether any body parts are cached already */
 
85
        body_parts = array_get(&ctx->cached_body_parts, &count);
 
86
        if ( count == 0 )
 
87
                return FALSE;
 
88
 
 
89
        /* Clear result array */
 
90
        array_clear(&ctx->return_body_parts);
 
91
        
 
92
        /* Fill result array with requested content_types */
 
93
        for (i = 0; i < count; i++) {
 
94
                if (!body_parts[i].have_body) {
 
95
                        /* Part has no body; according to RFC this MUST not match to anything and 
 
96
                         * therefore it is not included in the result.
 
97
                         */
 
98
                        continue;
 
99
                }
 
100
 
 
101
                /* Skip content types that are not requested */
 
102
                if (!_is_wanted_content_type(wanted_types, body_parts[i].content_type))
 
103
                        continue;
 
104
 
 
105
                /* Add new item to the result */
 
106
                return_part = array_append_space(&ctx->return_body_parts);
 
107
                
 
108
                /* Depending on whether a decoded body part is requested, the appropriate
 
109
                 * cache item is read. If it is missing, this function fails and the cache 
 
110
                 * needs to be completed by ext_body_parts_add_missing().
 
111
                 */
 
112
                if (decode_to_plain) {
 
113
                        if (body_parts[i].decoded_body == NULL)
 
114
                                return FALSE;
 
115
                        return_part->content = body_parts[i].decoded_body;
 
116
                        return_part->size = body_parts[i].decoded_body_size;
 
117
                } else {
 
118
                        if (body_parts[i].raw_body == NULL)
 
119
                                return FALSE;
 
120
                        return_part->content = body_parts[i].raw_body;
 
121
                        return_part->size = body_parts[i].raw_body_size;
 
122
                }
 
123
        }
 
124
 
 
125
        return TRUE;
 
126
}
 
127
 
 
128
static void ext_body_part_save
 
129
(struct ext_body_message_context *ctx, struct message_part *part,
 
130
        struct ext_body_part_cached *body_part, bool decoded)
 
131
{
 
132
        buffer_t *buf = ctx->tmp_buffer;
 
133
        char *part_data;
 
134
        size_t part_size;
 
135
 
 
136
        /* Add terminating NUL to the body part buffer */
 
137
        buffer_append_c(buf, '\0');
 
138
 
 
139
        part_data = p_malloc(ctx->pool, buf->used);
 
140
        memcpy(part_data, buf->data, buf->used);
 
141
        part_size = buf->used - 1;
 
142
 
 
143
        /* Depending on whether the part is decoded or not store message body in the
 
144
         * appropriate cache location.
 
145
         */
 
146
        if ( !decoded ) {
 
147
                body_part->raw_body = part_data;
 
148
                body_part->raw_body_size = part_size;
 
149
                i_assert(buf->used - 1 == part->body_size.physical_size);
 
150
        } else {
 
151
                body_part->decoded_body = part_data;
 
152
                body_part->decoded_body_size = part_size;
 
153
        }
 
154
        
 
155
        /* Clear buffer */
 
156
        buffer_set_used_size(buf, 0);
 
157
}
 
158
 
 
159
static const char *_parse_content_type(const struct message_header_line *hdr)
 
160
{
 
161
        struct rfc822_parser_context parser;
 
162
        string_t *content_type;
 
163
 
 
164
        /* Initialize parsing */
 
165
        rfc822_parser_init(&parser, hdr->full_value, hdr->full_value_len, NULL);
 
166
        (void)rfc822_skip_lwsp(&parser);
 
167
 
 
168
        /* Parse content type */
 
169
        content_type = t_str_new(64);
 
170
        if (rfc822_parse_content_type(&parser, content_type) < 0)
 
171
                return "";
 
172
 
 
173
        /* Content-type value must end here, otherwise it is invalid after all */
 
174
        (void)rfc822_skip_lwsp(&parser);
 
175
        if ( parser.data != parser.end && *parser.data != ';' )
 
176
                return "";
 
177
 
 
178
        /* Success */
 
179
        return str_c(content_type);
 
180
}
 
181
 
 
182
/* ext_body_parts_add_missing():
 
183
 *   Add requested message body parts to the cache that are missing. 
 
184
 */
 
185
static bool ext_body_parts_add_missing
 
186
(const struct sieve_message_data *msgdata, struct ext_body_message_context *ctx, 
 
187
        const char * const *content_types, bool decode_to_plain)
 
188
{
 
189
        struct ext_body_part_cached *body_part = NULL;
 
190
        struct message_parser_ctx *parser;
 
191
        struct message_decoder_context *decoder;
 
192
        struct message_block block, decoded;
 
193
        struct message_part *parts, *prev_part = NULL;
 
194
        struct istream *input;
 
195
        unsigned int idx = 0;
 
196
        bool save_body = FALSE, have_all;
 
197
        int ret;
 
198
 
 
199
        /* First check whether any are missing */
 
200
        if (ext_body_get_return_parts(ctx, content_types, decode_to_plain)) {
 
201
                /* Cache hit; all are present */
 
202
                return TRUE;
 
203
        }
 
204
 
 
205
        /* Get the message stream */
 
206
        if ( mail_get_stream(msgdata->mail, NULL, NULL, &input) < 0 )
 
207
                return FALSE;
 
208
                
 
209
        buffer_set_used_size(ctx->tmp_buffer, 0);
 
210
        
 
211
        /* Initialize body decoder */
 
212
        decoder = decode_to_plain ? message_decoder_init(FALSE) : NULL;
 
213
        
 
214
        parser = message_parser_init(ctx->pool, input, 0, 0);
 
215
        while ( (ret = message_parser_parse_next_block(parser, &block)) > 0 ) {
 
216
                if ( block.part != prev_part ) {
 
217
                        /* Save previous body part */
 
218
                        if ( body_part != NULL && save_body ) {
 
219
                                ext_body_part_save(ctx, prev_part, body_part, decoder != NULL);
 
220
                        }
 
221
                        
 
222
                        /* Start processing next */
 
223
                        prev_part = block.part;
 
224
                        body_part = array_idx_modifiable(&ctx->cached_body_parts, idx);
 
225
                        idx++;
 
226
                        body_part->content_type = "text/plain";
 
227
                }
 
228
                
 
229
                if ( block.hdr != NULL || block.size == 0 ) {
 
230
                        /* Reading headers */
 
231
 
 
232
                        /* Decode block */
 
233
                        if ( decoder != NULL )
 
234
                                (void)message_decoder_decode_next_block(decoder, &block, &decoded);
 
235
 
 
236
                        /* Check for end of headers */
 
237
                        if ( block.hdr == NULL ) {
 
238
                                /* Save bodies only if we have a wanted content-type */
 
239
                                save_body = _is_wanted_content_type
 
240
                                        (content_types, body_part->content_type);
 
241
                                continue;
 
242
                        }
 
243
                        
 
244
                        /* Encountered the empty line that indicates the end of the headers and 
 
245
                         * the start of the body
 
246
                         */
 
247
                        if ( block.hdr->eoh )
 
248
                                body_part->have_body = TRUE;
 
249
                                
 
250
                        /* We're interested of only Content-Type: header */
 
251
                        if ( strcasecmp(block.hdr->name, "Content-Type" ) != 0)
 
252
                                continue;
 
253
 
 
254
                        /* Header can have folding whitespace. Acquire the full value before 
 
255
                         * continuing
 
256
                         */
 
257
                        if ( block.hdr->continues ) {
 
258
                                block.hdr->use_full_value = TRUE;
 
259
                                continue;
 
260
                        }
 
261
                
 
262
                        /* Parse the content type from the Content-type header */
 
263
                        T_BEGIN {
 
264
                                body_part->content_type =
 
265
                                        p_strdup(ctx->pool, _parse_content_type(block.hdr));
 
266
                        } T_END;
 
267
                        
 
268
                        continue;
 
269
                }
 
270
 
 
271
                /* Reading body */
 
272
                if ( save_body ) {
 
273
                        if ( decoder != NULL ) {
 
274
                                (void)message_decoder_decode_next_block(decoder, &block, &decoded);
 
275
                                buffer_append(ctx->tmp_buffer, decoded.data, decoded.size);
 
276
                        } else {
 
277
                                buffer_append(ctx->tmp_buffer, block.data, block.size);
 
278
                        }
 
279
                }
 
280
        }
 
281
 
 
282
        /* Save last body part if necessary */
 
283
        if ( body_part != NULL && save_body )
 
284
                ext_body_part_save(ctx, prev_part, body_part, decoder != NULL);
 
285
 
 
286
        /* Try to fill the return_body_parts array once more */
 
287
        have_all = ext_body_get_return_parts(ctx, content_types, decode_to_plain);
 
288
        
 
289
        /* This time, failure is a bug */
 
290
        i_assert(have_all);
 
291
 
 
292
        /* Cleanup */
 
293
        (void)message_parser_deinit(&parser, &parts);
 
294
        if (decoder != NULL)
 
295
                message_decoder_deinit(&decoder);
 
296
        
 
297
        /* Return status */
 
298
        return ( input->stream_errno == 0 );
 
299
}
 
300
 
 
301
static struct ext_body_message_context *ext_body_get_context
 
302
(struct sieve_message_context *msgctx)
 
303
{
 
304
        pool_t pool = sieve_message_context_pool(msgctx);
 
305
        struct ext_body_message_context *ctx;
 
306
        
 
307
        /* Get message context (contains cached message body information) */
 
308
        ctx = (struct ext_body_message_context *)
 
309
                sieve_message_context_extension_get(msgctx, &body_extension);
 
310
        
 
311
        /* Create it if it does not exist already */
 
312
        if ( ctx == NULL ) {
 
313
                ctx = p_new(pool, struct ext_body_message_context, 1);  
 
314
                ctx->pool = pool;
 
315
                p_array_init(&ctx->cached_body_parts, pool, 8);
 
316
                p_array_init(&ctx->return_body_parts, pool, 8);
 
317
                ctx->tmp_buffer = buffer_create_dynamic(pool, 1024*64);
 
318
                ctx->raw_body = NULL;           
 
319
 
 
320
                /* Register context */
 
321
                sieve_message_context_extension_set(msgctx, &body_extension, (void *) ctx);
 
322
        }
 
323
        
 
324
        return ctx;
 
325
}
 
326
 
 
327
bool ext_body_get_content
 
328
(const struct sieve_runtime_env *renv, const char * const *content_types,
 
329
        int decode_to_plain, struct ext_body_part **parts_r)
 
330
{
 
331
        bool result = TRUE;
 
332
        struct ext_body_message_context *ctx = ext_body_get_context(renv->msgctx);
 
333
 
 
334
        T_BEGIN {
 
335
                /* Fill the return_body_parts array */
 
336
                if ( !ext_body_parts_add_missing
 
337
                        (renv->msgdata, ctx, content_types, decode_to_plain != 0) )
 
338
                        result = FALSE;
 
339
        } T_END;
 
340
        
 
341
        /* Check status */
 
342
        if ( !result ) return FALSE;
 
343
 
 
344
        /* Return the array of body items */
 
345
        (void) array_append_space(&ctx->return_body_parts); /* NULL-terminate */
 
346
        *parts_r = array_idx_modifiable(&ctx->return_body_parts, 0);
 
347
 
 
348
        return result;
 
349
}
 
350
 
 
351
bool ext_body_get_raw
 
352
(const struct sieve_runtime_env *renv, struct ext_body_part **parts_r)
 
353
{
 
354
        struct ext_body_message_context *ctx = ext_body_get_context(renv->msgctx);
 
355
        struct ext_body_part *return_part;
 
356
        buffer_t *buf;
 
357
 
 
358
        if ( ctx->raw_body == NULL ) {
 
359
                struct mail *mail = renv->msgdata->mail;
 
360
                struct istream *input;
 
361
                struct message_size hdr_size, body_size;
 
362
                const unsigned char *data;
 
363
                size_t size;
 
364
                int ret;
 
365
 
 
366
                ctx->raw_body = buf = buffer_create_dynamic(ctx->pool, 1024*64);
 
367
 
 
368
                /* Get stream for message */
 
369
                if ( mail_get_stream(mail, &hdr_size, &body_size, &input) < 0 )
 
370
                        return FALSE;
 
371
 
 
372
                /* Skip stream to beginning of body */
 
373
                i_stream_skip(input, hdr_size.physical_size);
 
374
 
 
375
                /* Read raw message body */
 
376
                while ( (ret = i_stream_read_data(input, &data, &size, 0)) > 0 ) {      
 
377
                        buffer_append(buf, data, size);
 
378
 
 
379
                        i_stream_skip(input, size);
 
380
                }
 
381
        } else {
 
382
                buf = ctx->raw_body;    
 
383
        }
 
384
 
 
385
        /* Clear result array */
 
386
        array_clear(&ctx->return_body_parts);
 
387
 
 
388
        if ( buf->used > 0  ) {
 
389
                /* Add terminating NUL to the body part buffer */
 
390
                buffer_append_c(buf, '\0');
 
391
        
 
392
                /* Add single item to the result */
 
393
                return_part = array_append_space(&ctx->return_body_parts);
 
394
                return_part->content = buf->data;
 
395
                return_part->size = buf->used - 1;
 
396
        }
 
397
 
 
398
        /* Return the array of body items */
 
399
        (void) array_append_space(&ctx->return_body_parts); /* NULL-terminate */
 
400
        *parts_r = array_idx_modifiable(&ctx->return_body_parts, 0);
 
401
 
 
402
        return TRUE;
 
403
}