~ubuntu-branches/ubuntu/wily/dovecot/wily

« back to all changes in this revision

Viewing changes to src/imap/cmd-select.c

  • Committer: Bazaar Package Importer
  • Author(s): CHuck Short, Chuck Short
  • Date: 2009-11-06 00:47:29 UTC
  • mfrom: (4.1.9 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091106004729-i39n7v9e7d4h51f6
Tags: 1:1.2.6-1ubuntu1
* 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 restrictions
    - 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 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
   + debian/patches/dovecot-postfix.conf.diff:
     + 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.
   + debian/rules:
     - Create emtpy stamp.h.in files in dovecot-sieve/ and dovecot-managesieve/
       if they're not there since empty files are not included in the diff.gz 
       file.
   + Add SMTP-AUTH support for Outlook (login auth mechanism)
   + Dropped:
     - debian/patches/security-CVE-2009-3235: Applied upstream.
     - debian/patches/fix-pop3-assertion.dpatch: Applied upstream.
     - dovecot-sieve and dovecot-managesieve: Use the debian patches instead.

  [Chuck Short]
  - Updated dovecot-sieve to 0.1.13.
  - Updated dovecot-managesieve to 0.11.9.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Copyright (c) 2002-2009 Dovecot authors, see the included COPYING file */
2
2
 
3
3
#include "common.h"
 
4
#include "seq-range-array.h"
4
5
#include "commands.h"
 
6
#include "mail-search-build.h"
 
7
#include "imap-seqset.h"
 
8
#include "imap-fetch.h"
5
9
#include "imap-sync.h"
6
10
 
7
 
bool cmd_select_full(struct client_command_context *cmd, bool readonly)
8
 
{
9
 
        struct client *client = cmd->client;
 
11
#include <stdlib.h>
 
12
 
 
13
struct imap_select_context {
 
14
        struct client_command_context *cmd;
10
15
        struct mail_storage *storage;
11
16
        struct mailbox *box;
 
17
 
 
18
        struct imap_fetch_context *fetch_ctx;
 
19
 
 
20
        uint32_t qresync_uid_validity;
 
21
        uint64_t qresync_modseq;
 
22
        ARRAY_TYPE(seq_range) qresync_known_uids;
 
23
        ARRAY_TYPE(uint32_t) qresync_sample_seqset;
 
24
        ARRAY_TYPE(uint32_t) qresync_sample_uidset;
 
25
 
 
26
        unsigned int condstore:1;
 
27
};
 
28
 
 
29
static int select_qresync_get_uids(struct imap_select_context *ctx,
 
30
                                   const ARRAY_TYPE(seq_range) *seqset,
 
31
                                   const ARRAY_TYPE(seq_range) *uidset)
 
32
{
 
33
        const struct seq_range *seq_range, *uid_range;
 
34
        struct seq_range_iter seq_iter;
 
35
        unsigned int i, seq_count, uid_count, diff, n = 0;
 
36
        uint32_t seq;
 
37
 
 
38
        /* change all n:m ranges to n,m and store the results */
 
39
        seq_range = array_get(seqset, &seq_count);
 
40
        uid_range = array_get(uidset, &uid_count);
 
41
 
 
42
        seq_range_array_iter_init(&seq_iter, seqset);
 
43
        i_array_init(&ctx->qresync_sample_uidset, uid_count);
 
44
        i_array_init(&ctx->qresync_sample_seqset, uid_count);
 
45
        for (i = 0; i < uid_count; i++) {
 
46
                if (!seq_range_array_iter_nth(&seq_iter, n++, &seq))
 
47
                        return -1;
 
48
                array_append(&ctx->qresync_sample_uidset,
 
49
                             &uid_range[i].seq1, 1);
 
50
                array_append(&ctx->qresync_sample_seqset, &seq, 1);
 
51
 
 
52
                diff = uid_range[i].seq2 - uid_range[i].seq1;
 
53
                if (diff > 0) {
 
54
                        n += diff - 1;
 
55
                        if (!seq_range_array_iter_nth(&seq_iter, n++, &seq))
 
56
                                return -1;
 
57
 
 
58
                        array_append(&ctx->qresync_sample_uidset,
 
59
                                     &uid_range[i].seq2, 1);
 
60
                        array_append(&ctx->qresync_sample_seqset, &seq, 1);
 
61
                }
 
62
        }
 
63
        if (seq_range_array_iter_nth(&seq_iter, n, &seq))
 
64
                return -1;
 
65
        return 0;
 
66
}
 
67
 
 
68
static bool
 
69
select_parse_qresync(struct imap_select_context *ctx,
 
70
                     const struct imap_arg *args)
 
71
{
 
72
        ARRAY_TYPE(seq_range) seqset, uidset;
 
73
        unsigned int count;
 
74
 
 
75
        if ((ctx->cmd->client->enabled_features &
 
76
             MAILBOX_FEATURE_QRESYNC) == 0) {
 
77
                client_send_command_error(ctx->cmd, "QRESYNC not enabled");
 
78
                return FALSE;
 
79
        }
 
80
        if (args->type != IMAP_ARG_LIST) {
 
81
                client_send_command_error(ctx->cmd,
 
82
                                          "QRESYNC parameters missing");
 
83
                return FALSE;
 
84
        }
 
85
        args = IMAP_ARG_LIST_ARGS(args);
 
86
        for (count = 0; args[count].type != IMAP_ARG_EOL; count++) ;
 
87
 
 
88
        if (count < 2 || count > 4 ||
 
89
            args[0].type != IMAP_ARG_ATOM ||
 
90
            args[1].type != IMAP_ARG_ATOM ||
 
91
            (count > 2 && args[2].type != IMAP_ARG_ATOM) ||
 
92
            (count > 3 && args[3].type != IMAP_ARG_LIST)) {
 
93
                client_send_command_error(ctx->cmd,
 
94
                                          "Invalid QRESYNC parameters");
 
95
                return FALSE;
 
96
        }
 
97
        ctx->qresync_uid_validity =
 
98
                strtoul(IMAP_ARG_STR_NONULL(&args[0]), NULL, 10);
 
99
        ctx->qresync_modseq =
 
100
                strtoull(IMAP_ARG_STR_NONULL(&args[1]), NULL, 10);
 
101
        if (count > 2) {
 
102
                i_array_init(&ctx->qresync_known_uids, 64);
 
103
                if (imap_seq_set_parse(IMAP_ARG_STR_NONULL(&args[2]),
 
104
                                       &ctx->qresync_known_uids) < 0) {
 
105
                        client_send_command_error(ctx->cmd,
 
106
                                                  "Invalid QRESYNC known-uids");
 
107
                        return FALSE;
 
108
                }
 
109
        } else {
 
110
                i_array_init(&ctx->qresync_known_uids, 64);
 
111
                seq_range_array_add_range(&ctx->qresync_known_uids,
 
112
                                          1, (uint32_t)-1);
 
113
        }
 
114
        if (count > 3) {
 
115
                args = IMAP_ARG_LIST_ARGS(&args[3]);
 
116
                if (args[0].type != IMAP_ARG_ATOM ||
 
117
                    args[1].type != IMAP_ARG_ATOM ||
 
118
                    args[2].type != IMAP_ARG_EOL) {
 
119
                        client_send_command_error(ctx->cmd,
 
120
                                "Invalid QRESYNC known set parameters");
 
121
                        return FALSE;
 
122
                }
 
123
                t_array_init(&seqset, 32);
 
124
                if (imap_seq_set_parse(IMAP_ARG_STR_NONULL(&args[0]),
 
125
                                       &seqset) < 0) {
 
126
                        client_send_command_error(ctx->cmd,
 
127
                                "Invalid QRESYNC known-sequence-set");
 
128
                        return FALSE;
 
129
                }
 
130
                t_array_init(&uidset, 32);
 
131
                if (imap_seq_set_parse(IMAP_ARG_STR_NONULL(&args[1]),
 
132
                                       &uidset) < 0) {
 
133
                        client_send_command_error(ctx->cmd,
 
134
                                "Invalid QRESYNC known-uid-set");
 
135
                        return FALSE;
 
136
                }
 
137
                if (select_qresync_get_uids(ctx, &seqset, &uidset) < 0) {
 
138
                        client_send_command_error(ctx->cmd,
 
139
                                "Invalid QRESYNC sets");
 
140
                        return FALSE;
 
141
                }
 
142
        }
 
143
        return TRUE;
 
144
}
 
145
 
 
146
static bool
 
147
select_parse_options(struct imap_select_context *ctx,
 
148
                     const struct imap_arg *args)
 
149
{
 
150
        const char *name;
 
151
 
 
152
        while (args->type != IMAP_ARG_EOL) {
 
153
                if (args->type != IMAP_ARG_ATOM) {
 
154
                        client_send_command_error(ctx->cmd,
 
155
                                "SELECT options contain non-atoms.");
 
156
                        return FALSE;
 
157
                }
 
158
                name = t_str_ucase(IMAP_ARG_STR(args));
 
159
                args++;
 
160
 
 
161
                if (strcmp(name, "CONDSTORE") == 0)
 
162
                        ctx->condstore = TRUE;
 
163
                else if (strcmp(name, "QRESYNC") == 0) {
 
164
                        if (!select_parse_qresync(ctx, args))
 
165
                                return FALSE;
 
166
                        args++;
 
167
                } else {
 
168
                        client_send_command_error(ctx->cmd,
 
169
                                                  "Unknown FETCH modifier");
 
170
                        return FALSE;
 
171
                }
 
172
        }
 
173
        return TRUE;
 
174
}
 
175
 
 
176
static void select_context_free(struct imap_select_context *ctx)
 
177
{
 
178
        if (array_is_created(&ctx->qresync_known_uids))
 
179
                array_free(&ctx->qresync_known_uids);
 
180
        if (array_is_created(&ctx->qresync_sample_seqset))
 
181
                array_free(&ctx->qresync_sample_seqset);
 
182
        if (array_is_created(&ctx->qresync_sample_uidset))
 
183
                array_free(&ctx->qresync_sample_uidset);
 
184
}
 
185
 
 
186
static void cmd_select_finish(struct imap_select_context *ctx, int ret)
 
187
{
 
188
        if (ret < 0) {
 
189
                if (ctx->box != NULL)
 
190
                        mailbox_close(&ctx->box);
 
191
                client_send_storage_error(ctx->cmd, ctx->storage);
 
192
                ctx->cmd->client->mailbox = NULL;
 
193
        } else {
 
194
                client_send_tagline(ctx->cmd, mailbox_is_readonly(ctx->box) ?
 
195
                                    "OK [READ-ONLY] Select completed." :
 
196
                                    "OK [READ-WRITE] Select completed.");
 
197
        }
 
198
        select_context_free(ctx);
 
199
}
 
200
 
 
201
static bool cmd_select_continue(struct client_command_context *cmd)
 
202
{
 
203
        struct imap_select_context *ctx = cmd->context;
 
204
        int ret;
 
205
 
 
206
        if ((ret = imap_fetch_more(ctx->fetch_ctx)) == 0) {
 
207
                /* unfinished */
 
208
                return FALSE;
 
209
        }
 
210
 
 
211
        ret = imap_fetch_deinit(ctx->fetch_ctx);
 
212
        cmd_select_finish(ctx, ret);
 
213
        return TRUE;
 
214
}
 
215
 
 
216
static int select_qresync(struct imap_select_context *ctx)
 
217
{
 
218
        struct imap_fetch_context *fetch_ctx;
 
219
        struct mail_search_args *search_args;
 
220
 
 
221
        search_args = mail_search_build_init();
 
222
        search_args->args = p_new(search_args->pool, struct mail_search_arg, 1);
 
223
        search_args->args->type = SEARCH_UIDSET;
 
224
        search_args->args->value.seqset = ctx->qresync_known_uids;
 
225
 
 
226
        fetch_ctx = imap_fetch_init(ctx->cmd, ctx->box);
 
227
        if (fetch_ctx == NULL)
 
228
                return -1;
 
229
 
 
230
        fetch_ctx->search_args = search_args;
 
231
        fetch_ctx->send_vanished = TRUE;
 
232
        fetch_ctx->qresync_sample_seqset = &ctx->qresync_sample_seqset;
 
233
        fetch_ctx->qresync_sample_uidset = &ctx->qresync_sample_uidset;
 
234
 
 
235
        if (!imap_fetch_add_changed_since(fetch_ctx, ctx->qresync_modseq) ||
 
236
            !imap_fetch_init_handler(fetch_ctx, "UID", NULL) ||
 
237
            !imap_fetch_init_handler(fetch_ctx, "FLAGS", NULL) ||
 
238
            !imap_fetch_init_handler(fetch_ctx, "MODSEQ", NULL)) {
 
239
                (void)imap_fetch_deinit(fetch_ctx);
 
240
                return -1;
 
241
        }
 
242
 
 
243
        if (imap_fetch_begin(fetch_ctx) == 0) {
 
244
                if (imap_fetch_more(fetch_ctx) == 0) {
 
245
                        /* unfinished */
 
246
                        ctx->fetch_ctx = fetch_ctx;
 
247
                        ctx->cmd->state = CLIENT_COMMAND_STATE_WAIT_OUTPUT;
 
248
 
 
249
                        ctx->cmd->func = cmd_select_continue;
 
250
                        ctx->cmd->context = ctx;
 
251
                        return FALSE;
 
252
                }
 
253
        }
 
254
 
 
255
        return imap_fetch_deinit(fetch_ctx);
 
256
}
 
257
 
 
258
static int
 
259
select_open(struct imap_select_context *ctx, const char *mailbox, bool readonly)
 
260
{
 
261
        struct client *client = ctx->cmd->client;
12
262
        struct mailbox_status status;
13
 
        const char *mailbox;
14
 
 
15
 
        /* <mailbox> */
16
 
        if (!client_read_string_args(cmd, 1, &mailbox))
17
 
                return FALSE;
18
 
 
19
 
        if (client->mailbox != NULL) {
20
 
                box = client->mailbox;
21
 
                client->mailbox = NULL;
22
 
 
23
 
                storage = mailbox_get_storage(box);
24
 
                if (mailbox_close(&box) < 0)
25
 
                        client_send_untagged_storage_error(client, storage);
26
 
        }
27
 
 
28
 
        storage = client_find_storage(cmd, &mailbox);
29
 
        if (storage == NULL)
30
 
                return TRUE;
31
 
 
32
 
        box = mailbox_open(storage, mailbox, NULL, !readonly ? 0 :
33
 
                           (MAILBOX_OPEN_READONLY | MAILBOX_OPEN_KEEP_RECENT));
34
 
        if (box == NULL) {
35
 
                client_send_storage_error(cmd, storage);
36
 
                return TRUE;
37
 
        }
38
 
 
39
 
        if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FULL_READ,
 
263
        enum mailbox_open_flags open_flags = 0;
 
264
 
 
265
        if (readonly)
 
266
                open_flags |= MAILBOX_OPEN_READONLY | MAILBOX_OPEN_KEEP_RECENT;
 
267
        ctx->box = mailbox_open(&ctx->storage, mailbox, NULL, open_flags);
 
268
        if (ctx->box == NULL)
 
269
                return -1;
 
270
 
 
271
        if (client->enabled_features != 0)
 
272
                mailbox_enable(ctx->box, client->enabled_features);
 
273
        if (mailbox_sync(ctx->box, MAILBOX_SYNC_FLAG_FULL_READ,
40
274
                         STATUS_MESSAGES | STATUS_RECENT |
41
275
                         STATUS_FIRST_UNSEEN_SEQ | STATUS_UIDVALIDITY |
42
 
                         STATUS_UIDNEXT | STATUS_KEYWORDS, &status) < 0) {
43
 
                client_send_storage_error(cmd, storage);
44
 
                mailbox_close(&box);
45
 
                return TRUE;
46
 
        }
 
276
                         STATUS_UIDNEXT | STATUS_KEYWORDS |
 
277
                         STATUS_HIGHESTMODSEQ, &status) < 0)
 
278
                return -1;
47
279
 
48
 
        /* set client's mailbox only after getting status to make sure
49
 
           we're not sending any expunge/exists replies too early to client */
50
 
        client->mailbox = box;
 
280
        client->mailbox = ctx->box;
51
281
        client->select_counter++;
52
282
        client->mailbox_examined = readonly;
53
 
 
54
283
        client->messages_count = status.messages;
55
284
        client->recent_count = status.recent;
56
285
        client->uidvalidity = status.uidvalidity;
77
306
                         t_strdup_printf("* OK [UIDNEXT %u] Predicted next UID",
78
307
                                         status.uidnext));
79
308
 
80
 
        client_send_tagline(cmd, mailbox_is_readonly(box) ?
81
 
                            "OK [READ-ONLY] Select completed." :
82
 
                            "OK [READ-WRITE] Select completed.");
 
309
        if (status.nonpermanent_modseqs) {
 
310
                client_send_line(client,
 
311
                                 "* OK [NOMODSEQ] No permanent modsequences");
 
312
        } else {
 
313
                client_send_line(client,
 
314
                        t_strdup_printf("* OK [HIGHESTMODSEQ %llu] Highest",
 
315
                                (unsigned long long)status.highest_modseq));
 
316
                client->sync_last_full_modseq = status.highest_modseq;
 
317
        }
 
318
 
 
319
        if (ctx->qresync_uid_validity == status.uidvalidity) {
 
320
                if (select_qresync(ctx) < 0)
 
321
                        return -1;
 
322
        }
 
323
        return 0;
 
324
}
 
325
 
 
326
bool cmd_select_full(struct client_command_context *cmd, bool readonly)
 
327
{
 
328
        struct client *client = cmd->client;
 
329
        struct mailbox *box;
 
330
        struct imap_select_context *ctx;
 
331
        const struct imap_arg *args;
 
332
        const char *mailbox;
 
333
        int ret;
 
334
 
 
335
        /* <mailbox> [(optional parameters)] */
 
336
        if (!client_read_args(cmd, 0, 0, &args))
 
337
                return FALSE;
 
338
 
 
339
        if (!IMAP_ARG_TYPE_IS_STRING(args[0].type)) {
 
340
                client_send_command_error(cmd, "Invalid arguments.");
 
341
                return FALSE;
 
342
        }
 
343
        mailbox = IMAP_ARG_STR(&args[0]);
 
344
 
 
345
        ctx = p_new(cmd->pool, struct imap_select_context, 1);
 
346
        ctx->cmd = cmd;
 
347
        ctx->storage = client_find_storage(cmd, &mailbox);
 
348
        if (ctx->storage == NULL)
 
349
                return TRUE;
 
350
 
 
351
        if (args[1].type == IMAP_ARG_LIST) {
 
352
                if (!select_parse_options(ctx, IMAP_ARG_LIST_ARGS(&args[1]))) {
 
353
                        select_context_free(ctx);
 
354
                        return TRUE;
 
355
                }
 
356
        }
 
357
 
 
358
        i_assert(client->mailbox_change_lock == NULL);
 
359
        client->mailbox_change_lock = cmd;
 
360
 
 
361
        if (client->mailbox != NULL) {
 
362
                struct mail_storage *old_storage =
 
363
                        mailbox_get_storage(client->mailbox);
 
364
 
 
365
                client_search_updates_free(client);
 
366
                box = client->mailbox;
 
367
                client->mailbox = NULL;
 
368
 
 
369
                if (mailbox_close(&box) < 0)
 
370
                        client_send_untagged_storage_error(client, old_storage);
 
371
                /* CLOSED response is required by QRESYNC */
 
372
                client_send_line(client,
 
373
                                 "* OK [CLOSED] Previous mailbox closed.");
 
374
        }
 
375
 
 
376
        if (ctx->condstore) {
 
377
                /* Enable while no mailbox is opened to avoid sending
 
378
                   HIGHESTMODSEQ for previously opened mailbox */
 
379
                client_enable(client, MAILBOX_FEATURE_CONDSTORE);
 
380
        }
 
381
 
 
382
        ret = select_open(ctx, mailbox, readonly);
 
383
        cmd_select_finish(ctx, ret);
83
384
        return TRUE;
84
385
}
85
386