~ubuntu-dev/ubuntu/lucid/dovecot/lucid-201002101901

« back to all changes in this revision

Viewing changes to src/plugins/lazy-expunge/lazy-expunge-plugin.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:
8
8
#include "mkdir-parents.h"
9
9
#include "maildir-storage.h"
10
10
#include "mail-namespace.h"
 
11
#include "mail-search-build.h"
11
12
#include "lazy-expunge-plugin.h"
12
13
 
13
14
#include <stdio.h>
41
42
        union mailbox_list_module_context module_ctx;
42
43
 
43
44
        struct mail_storage *storage;
 
45
        bool deleting;
44
46
};
45
47
 
46
48
struct lazy_expunge_mail_storage {
465
467
}
466
468
 
467
469
static int
 
470
mailbox_move_all_mails(struct mailbox_list *list,
 
471
                       const char *src_name, const char *dest_name)
 
472
{
 
473
        struct mail_storage *storage = list->ns->storage;
 
474
        struct mailbox *src_box, *dest_box;
 
475
        struct mail_search_args *search_args;
 
476
        struct mailbox_transaction_context *src_trans, *dest_trans;
 
477
        struct mail_search_context *search_ctx;
 
478
        struct mail_save_context *save_ctx;
 
479
        struct mail *mail;
 
480
        struct mail_keywords *keywords;
 
481
        const char *const *keywords_list;
 
482
        const char *errstr;
 
483
        enum mail_error error;
 
484
        int ret;
 
485
 
 
486
        dest_box = mailbox_open(&storage, dest_name, NULL, 0);
 
487
        if (dest_box == NULL) {
 
488
                errstr = mail_storage_get_last_error(storage, &error);
 
489
                i_error("lazy_expunge: Couldn't open DELETE dest mailbox "
 
490
                        "%s: %s", dest_name, errstr);
 
491
                return -1;
 
492
        }
 
493
 
 
494
        src_box = mailbox_open(&storage, src_name, NULL,
 
495
                               MAILBOX_OPEN_KEEP_LOCKED);
 
496
        if (src_box == NULL) {
 
497
                errstr = mail_storage_get_last_error(storage, &error);
 
498
                if (error == MAIL_ERROR_NOTFOUND)
 
499
                        return 0;
 
500
                i_error("lazy_expunge: Couldn't open DELETE source mailbox "
 
501
                        "%s: %s", src_name, errstr);
 
502
                return -1;
 
503
        }
 
504
 
 
505
        src_trans = mailbox_transaction_begin(src_box, 0);
 
506
        dest_trans = mailbox_transaction_begin(dest_box,
 
507
                                        MAILBOX_TRANSACTION_FLAG_EXTERNAL);
 
508
 
 
509
        search_args = mail_search_build_init();
 
510
        mail_search_build_add_all(search_args);
 
511
        search_ctx = mailbox_search_init(src_trans, search_args, NULL);
 
512
        mail_search_args_unref(&search_args);
 
513
 
 
514
        mail = mail_alloc(src_trans, 0, NULL);
 
515
        while ((ret = mailbox_search_next(search_ctx, mail)) > 0) {
 
516
                keywords_list = mail_get_keywords(mail);
 
517
                keywords = str_array_length(keywords_list) == 0 ? NULL :
 
518
                        mailbox_keywords_create_valid(dest_box, keywords_list);
 
519
 
 
520
                save_ctx = mailbox_save_alloc(dest_trans);
 
521
                mailbox_save_set_flags(save_ctx,
 
522
                                       mail_get_flags(mail) & ~MAIL_DELETED,
 
523
                                       keywords);
 
524
                ret = mailbox_copy(&save_ctx, mail);
 
525
                mailbox_keywords_free(dest_box, &keywords);
 
526
 
 
527
                if (ret < 0 && !mail->expunged)
 
528
                        break;
 
529
        }
 
530
        mail_free(&mail);
 
531
 
 
532
        if (mailbox_search_deinit(&search_ctx) < 0)
 
533
                ret = -1;
 
534
 
 
535
        (void)mailbox_transaction_commit(&src_trans);
 
536
        if (ret == 0)
 
537
                ret = mailbox_transaction_commit(&dest_trans);
 
538
        else
 
539
                mailbox_transaction_rollback(&dest_trans);
 
540
 
 
541
        mailbox_close(&src_box);
 
542
        mailbox_close(&dest_box);
 
543
 
 
544
        if (ret == 0)
 
545
                ret = mailbox_list_delete_mailbox(list, src_name);
 
546
        return ret;
 
547
}
 
548
 
 
549
static int
468
550
lazy_expunge_mailbox_list_delete(struct mailbox_list *list, const char *name)
469
551
{
470
552
        struct lazy_expunge_mail_user *luser =
472
554
        struct lazy_expunge_mailbox_list *llist =
473
555
                LAZY_EXPUNGE_LIST_CONTEXT(list);
474
556
        struct lazy_expunge_mail_storage *lstorage;
475
 
        struct mailbox_list *dest_list;
 
557
        struct mailbox_list *dest_list, *expunge_list;
476
558
        enum mailbox_name_status status;
477
559
        const char *destname;
478
560
        struct tm *tm;
479
561
        char timestamp[256];
480
562
        int ret;
481
563
 
482
 
        if (llist->storage == NULL) {
 
564
        if (llist->storage == NULL || llist->deleting) {
483
565
                /* not a maildir storage */
484
566
                return llist->module_ctx.super.delete_mailbox(list, name);
485
567
        }
503
585
                return -1;
504
586
        }
505
587
 
506
 
        /* destination mailbox name needs to contain a timestamp */
507
 
        tm = localtime(&ioloop_time);
508
 
        if (strftime(timestamp, sizeof(timestamp), "%Y%m%d-%H%M%S", tm) == 0)
509
 
                i_strocpy(timestamp, dec2str(ioloop_time), sizeof(timestamp));
510
 
        destname = t_strconcat(name, "-", timestamp, NULL);
 
588
        expunge_list = luser->lazy_ns[LAZY_NAMESPACE_EXPUNGE]->storage->list;
 
589
        dest_list = luser->lazy_ns[LAZY_NAMESPACE_DELETE]->storage->list;
 
590
 
 
591
        if (expunge_list == dest_list) {
 
592
                /* if there are no expunged messages in this mailbox,
 
593
                   we can simply rename the mailbox to the destination name */
 
594
                destname = name;
 
595
        } else {
 
596
                /* destination mailbox name needs to contain a timestamp */
 
597
                tm = localtime(&ioloop_time);
 
598
                if (strftime(timestamp, sizeof(timestamp),
 
599
                             "%Y%m%d-%H%M%S", tm) == 0) {
 
600
                        i_strocpy(timestamp, dec2str(ioloop_time),
 
601
                                  sizeof(timestamp));
 
602
                }
 
603
                destname = t_strconcat(name, "-", timestamp, NULL);
 
604
        }
511
605
 
512
606
        /* first move the actual mailbox */
513
 
        dest_list = luser->lazy_ns[LAZY_NAMESPACE_DELETE]->storage->list;
514
607
        if ((ret = mailbox_move(list, name, dest_list, &destname)) < 0)
515
608
                return -1;
516
609
        if (ret == 0) {
519
612
                return -1;
520
613
        }
521
614
 
 
615
        if (expunge_list == dest_list && strcmp(destname, name) != 0) {
 
616
                llist->deleting = TRUE;
 
617
                (void)mailbox_move_all_mails(dest_list, destname, name);
 
618
                llist->deleting = FALSE;
 
619
        }
 
620
 
522
621
        /* next move the expunged messages mailbox, if it exists */
523
 
        list = luser->lazy_ns[LAZY_NAMESPACE_EXPUNGE]->storage->list;
524
 
        dest_list =
525
 
                luser->lazy_ns[LAZY_NAMESPACE_DELETE_EXPUNGE]->storage->list;
526
 
        (void)mailbox_move(list, name, dest_list, &destname);
 
622
        dest_list = luser->lazy_ns[LAZY_NAMESPACE_DELETE_EXPUNGE]->storage->list;
 
623
        if (expunge_list != dest_list)
 
624
                (void)mailbox_move(expunge_list, name, dest_list, &destname);
527
625
        return 0;
528
626
}
529
627
 
538
636
        /* if this is one of our internal storages, mark it as such before
539
637
           quota plugin sees it */
540
638
        p = t_strsplit_spaces(getenv("LAZY_EXPUNGE"), " ");
541
 
        for (i = 0; i < LAZY_NAMESPACE_COUNT; i++, p++) {
 
639
        for (i = 0; i < LAZY_NAMESPACE_COUNT && *p != NULL; i++, p++) {
542
640
                if (strcmp(storage->ns->prefix, *p) == 0) {
543
641
                        storage->ns->flags |= NAMESPACE_FLAG_NOQUOTA;
544
642
                        break;
588
686
        int i;
589
687
 
590
688
        p = t_strsplit_spaces(getenv("LAZY_EXPUNGE"), " ");
591
 
        for (i = 0; i < LAZY_NAMESPACE_COUNT; i++, p++) {
 
689
        for (i = 0; i < LAZY_NAMESPACE_COUNT && *p != NULL; i++, p++) {
592
690
                const char *name = *p;
593
691
 
594
 
                if (name == NULL)
595
 
                        i_fatal("lazy_expunge: Missing namespace #%d", i + 1);
596
 
 
597
692
                luser->lazy_ns[i] =
598
693
                        mail_namespace_find_prefix(namespaces, name);
599
694
                if (luser->lazy_ns[i] == NULL)
608
703
                lstorage = LAZY_EXPUNGE_CONTEXT(luser->lazy_ns[i]->storage);
609
704
                lstorage->internal_namespace = TRUE;
610
705
        }
 
706
        if (i == 0)
 
707
                i_fatal("lazy_expunge: No namespaces defined");
 
708
        for (; i < LAZY_NAMESPACE_COUNT; i++)
 
709
                luser->lazy_ns[i] = luser->lazy_ns[i-1];
611
710
 
612
711
        if (lazy_expunge_next_hook_mail_namespaces_created != NULL)
613
712
                lazy_expunge_next_hook_mail_namespaces_created(namespaces);