~ubuntu-branches/ubuntu/trusty/dovecot/trusty

« back to all changes in this revision

Viewing changes to src/doveadm/doveadm-mail-import.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 11:11:54 UTC
  • mfrom: (1.15.2) (4.1.27 sid)
  • Revision ID: package-import@ubuntu.com-20120611111154-678cwbdj6ktgsv1h
Tags: 1:2.1.7-1ubuntu1
* Merge from Debian unstable, remaining changes:
  + Add mail-stack-delivery package:
    - Update d/rules
    - d/control: convert existing dovecot-postfix package to a dummy
      package and add new mail-stack-delivery package.
    - Update maintainer scripts.
    - Rename d/dovecot-postfix.* to debian/mail-stack-delivery.*
    - d/mail-stack-delivery.preinst: Move previously installed backups and
      config files to a new package namespace.
    - d/mail-stack-delivery.prerm: Added to handle downgrades.
  + Use Snakeoil SSL certificates by default:
    - d/control: Depend on ssl-cert.
    - d/dovecot-core.postinst: Relax grep for SSL_* a bit.
  + Add autopkgtest to debian/tests/*.
  + Add ufw integration:
    - d/dovecot-core.ufw.profile: new ufw profile.
    - d/rules: install profile in dovecot-core.
    - d/control: dovecot-core - suggest ufw.
  + d/{control,rules}: enable PIE hardening.
  + d/dovecot-core.dirs: Added usr/share/doc/dovecot-core
  + Add apport hook:
    - d/rules, d/source_dovecot.py
  + Add upstart job:
    - d/rules, d/dovecot-core.dovecot.upstart, d/control,
      d/dovecot-core.dirs, dovecot-imapd.{postrm, postinst, prerm},
      d/dovecot-pop3d.{postinst, postrm, prerm}.
      d/mail-stack-deliver.postinst: Convert init script to upstart.
  + d/control: Added Pre-Depends: dpkg (>= 1.15.6) to dovecot-dbg to support
    xz compression in Ubuntu.
  + d/control: Demote dovecot-common Recommends: to Suggests: to prevent
    install of extra packages on upgrade.
  + d/patches/dovecot-drac.patch: Updated with version for dovecot >= 2.0.0.
  + d/control: Drop B-D on systemd.
* Dropped changes:
  + d/patches/fix-racey-restart.patch: part of 2.1.x, no longer required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2010-2011 Dovecot authors, see the included COPYING file */
 
1
/* Copyright (c) 2010-2012 Dovecot authors, see the included COPYING file */
2
2
 
3
3
#include "lib.h"
4
4
#include "array.h"
5
5
#include "mail-storage.h"
6
6
#include "mail-storage-service.h"
7
7
#include "mail-namespace.h"
8
 
#include "doveadm-mail-list-iter.h"
 
8
#include "doveadm-mailbox-list-iter.h"
9
9
#include "doveadm-mail-iter.h"
10
10
#include "doveadm-mail.h"
11
11
 
14
14
 
15
15
        struct mail_user *src_user;
16
16
        const char *dest_parent;
 
17
        bool subscribe;
17
18
};
18
19
 
19
20
static int
24
25
        struct mail_namespace *ns;
25
26
        struct mailbox *box;
26
27
        enum mail_error error;
27
 
        const char *errstr, *storage_name;
 
28
        const char *errstr;
28
29
 
29
30
        if (*ctx->dest_parent != '\0') {
30
31
                /* prefix destination mailbox name with given parent mailbox */
31
 
                storage_name = ctx->dest_parent;
32
 
                ns = mail_namespace_find(user->namespaces, &storage_name);
 
32
                ns = mail_namespace_find(user->namespaces, ctx->dest_parent);
33
33
                if (ns == NULL) {
34
34
                        i_error("Can't find namespace for parent mailbox %s",
35
35
                                ctx->dest_parent);
 
36
                        doveadm_mail_failed_error(&ctx->ctx, MAIL_ERROR_NOTFOUND);
36
37
                        return -1;
37
38
                }
38
39
                name = t_strdup_printf("%s%c%s", ctx->dest_parent,
39
 
                                       ns->sep, name);
 
40
                                       mail_namespace_get_sep(ns), name);
40
41
        }
41
42
 
42
 
        storage_name = name;
43
 
        ns = mail_namespace_find(user->namespaces, &storage_name);
 
43
        ns = mail_namespace_find(user->namespaces, name);
44
44
        if (ns == NULL) {
45
45
                i_error("Can't find namespace for mailbox %s", name);
 
46
                doveadm_mail_failed_error(&ctx->ctx, MAIL_ERROR_NOTFOUND);
46
47
                return -1;
47
48
        }
48
49
 
49
 
        box = mailbox_alloc(ns->list, storage_name, MAILBOX_FLAG_SAVEONLY |
50
 
                            MAILBOX_FLAG_KEEP_RECENT);
 
50
        box = mailbox_alloc(ns->list, name, MAILBOX_FLAG_SAVEONLY);
51
51
        if (mailbox_create(box, NULL, FALSE) < 0) {
52
 
                errstr = mail_storage_get_last_error(mailbox_get_storage(box),
53
 
                                                     &error);
 
52
                errstr = mailbox_get_last_error(box, &error);
54
53
                if (error != MAIL_ERROR_EXISTS) {
55
54
                        i_error("Couldn't create mailbox %s: %s", name, errstr);
 
55
                        doveadm_mail_failed_mailbox(&ctx->ctx, box);
56
56
                        mailbox_free(&box);
57
57
                        return -1;
58
58
                }
59
59
        }
 
60
        if (ctx->subscribe) {
 
61
                if (mailbox_set_subscribed(box, TRUE) < 0) {
 
62
                        i_error("Couldn't subscribe to mailbox %s: %s",
 
63
                                name, mailbox_get_last_error(box, NULL));
 
64
                }
 
65
        }
60
66
        if (mailbox_sync(box, MAILBOX_SYNC_FLAG_FULL_READ) < 0) {
61
67
                i_error("Syncing mailbox %s failed: %s", name,
62
 
                        mail_storage_get_last_error(mailbox_get_storage(box),
63
 
                                                    NULL));
 
68
                        mailbox_get_last_error(box, NULL));
 
69
                doveadm_mail_failed_mailbox(&ctx->ctx, box);
64
70
                mailbox_free(&box);
65
71
                return -1;
66
72
        }
72
78
cmd_import_box_contents(struct doveadm_mail_iter *iter, struct mail *src_mail,
73
79
                        struct mailbox *dest_box)
74
80
{
75
 
        struct mail_storage *dest_storage = mailbox_get_storage(dest_box);
76
81
        struct mail_save_context *save_ctx;
77
82
        struct mailbox_transaction_context *dest_trans;
78
83
        const char *mailbox = mailbox_get_vname(dest_box);
86
91
                                mailbox, src_mail->uid);
87
92
                }
88
93
                save_ctx = mailbox_save_alloc(dest_trans);
 
94
                mailbox_save_copy_flags(save_ctx, src_mail);
89
95
                if (mailbox_copy(&save_ctx, src_mail) < 0) {
90
96
                        i_error("Copying box=%s uid=%u failed: %s",
91
97
                                mailbox, src_mail->uid,
92
 
                                mail_storage_get_last_error(dest_storage, NULL));
 
98
                                mailbox_get_last_error(dest_box, NULL));
93
99
                        ret = -1;
94
100
                }
95
 
        } while (doveadm_mail_iter_next(iter, src_mail));
 
101
        } while (doveadm_mail_iter_next(iter, &src_mail));
96
102
 
97
103
        if (mailbox_transaction_commit(&dest_trans) < 0) {
98
104
                i_error("Committing copied mails to %s failed: %s", mailbox,
99
 
                        mail_storage_get_last_error(dest_storage, NULL));
 
105
                        mailbox_get_last_error(dest_box, NULL));
100
106
                ret = -1;
101
107
        }
102
108
        return ret;
113
119
        struct mail *mail;
114
120
        int ret = 0;
115
121
 
116
 
        if (doveadm_mail_iter_init(info, search_args, &trans, &iter) < 0)
 
122
        if (doveadm_mail_iter_init(&ctx->ctx, info, search_args, 0, NULL,
 
123
                                   &trans, &iter) < 0)
117
124
                return -1;
118
125
 
119
 
        mail = mail_alloc(trans, 0, NULL);
120
 
        if (doveadm_mail_iter_next(iter, mail)) {
 
126
        if (doveadm_mail_iter_next(iter, &mail)) {
121
127
                /* at least one mail matches in this mailbox */
122
128
                if (dest_mailbox_open_or_create(ctx, dest_user, info->name,
123
 
                                                &box) == 0) {
124
 
                        if (cmd_import_box_contents(iter, mail, box) < 0)
 
129
                                                &box) < 0)
 
130
                        ret = -1;
 
131
                else {
 
132
                        if (cmd_import_box_contents(iter, mail, box) < 0) {
 
133
                                doveadm_mail_failed_mailbox(&ctx->ctx, mail->box);
125
134
                                ret = -1;
 
135
                        }
126
136
                        mailbox_free(&box);
127
137
                }
128
138
        }
129
 
        mail_free(&mail);
130
139
        if (doveadm_mail_iter_deinit_sync(&iter) < 0)
131
140
                ret = -1;
132
141
        return ret;
133
142
}
134
143
 
135
 
static void
 
144
static int
136
145
cmd_import_run(struct doveadm_mail_cmd_context *_ctx, struct mail_user *user)
137
146
{
138
147
        struct import_cmd_context *ctx = (struct import_cmd_context *)_ctx;
139
148
        const enum mailbox_list_iter_flags iter_flags =
140
149
                MAILBOX_LIST_ITER_RAW_LIST |
141
 
                MAILBOX_LIST_ITER_NO_AUTO_INBOX |
 
150
                MAILBOX_LIST_ITER_NO_AUTO_BOXES |
142
151
                MAILBOX_LIST_ITER_RETURN_NO_FLAGS;
143
 
        struct doveadm_mail_list_iter *iter;
 
152
        struct doveadm_mailbox_list_iter *iter;
144
153
        const struct mailbox_info *info;
 
154
        int ret = 0;
145
155
 
146
 
        iter = doveadm_mail_list_iter_init(ctx->src_user,
147
 
                                           _ctx->search_args, iter_flags);
148
 
        while ((info = doveadm_mail_list_iter_next(iter)) != NULL) T_BEGIN {
149
 
                (void)cmd_import_box(ctx, user, info, _ctx->search_args);
 
156
        iter = doveadm_mailbox_list_iter_init(_ctx, ctx->src_user,
 
157
                                              _ctx->search_args, iter_flags);
 
158
        while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) T_BEGIN {
 
159
                if (cmd_import_box(ctx, user, info, _ctx->search_args) < 0)
 
160
                        ret = -1;
150
161
        } T_END;
151
 
        doveadm_mail_list_iter_deinit(&iter);
 
162
        if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
 
163
                ret = -1;
 
164
        return ret;
152
165
}
153
166
 
154
167
static void cmd_import_init(struct doveadm_mail_cmd_context *_ctx,
190
203
        mail_user_unref(&ctx->src_user);
191
204
}
192
205
 
 
206
static bool cmd_import_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
 
207
{
 
208
        struct import_cmd_context *ctx = (struct import_cmd_context *)_ctx;
 
209
 
 
210
        switch (c) {
 
211
        case 's':
 
212
                ctx->subscribe = TRUE;
 
213
                break;
 
214
        default:
 
215
                return FALSE;
 
216
        }
 
217
        return TRUE;
 
218
}
 
219
 
193
220
static struct doveadm_mail_cmd_context *cmd_import_alloc(void)
194
221
{
195
222
        struct import_cmd_context *ctx;
196
223
 
197
224
        ctx = doveadm_mail_cmd_alloc(struct import_cmd_context);
 
225
        ctx->ctx.getopt_args = "s";
 
226
        ctx->ctx.v.parse_arg = cmd_import_parse_arg;
198
227
        ctx->ctx.v.init = cmd_import_init;
199
228
        ctx->ctx.v.deinit = cmd_import_deinit;
200
229
        ctx->ctx.v.run = cmd_import_run;
203
232
 
204
233
struct doveadm_mail_cmd cmd_import = {
205
234
        cmd_import_alloc, "import",
206
 
        "<source mail location> <dest parent mailbox> <search query>"
 
235
        "[-s] <source mail location> <dest parent mailbox> <search query>"
207
236
};