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

« back to all changes in this revision

Viewing changes to src/lib-imap-urlauth/imap-urlauth-backend.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-08 09:35:49 UTC
  • mfrom: (1.15.3) (96.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140108093549-814nkqdcxfbvgktg
Tags: 1:2.2.9-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/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.
  + Use the autotools-dev dh addon to update config.guess/config.sub for
    arm64.
* Dropped changes, included in Debian:
  - Update Dovecot name to reflect distribution in login greeting.
  - Update Drac plugin for >= 2.0.0 support.
* d/control: Drop dovecot-postfix package as its no longer required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
 
2
 
 
3
#include "lib.h"
 
4
#include "buffer.h"
 
5
#include "hex-binary.h"
 
6
#include "randgen.h"
 
7
#include "mail-user.h"
 
8
#include "mail-storage.h"
 
9
#include "mailbox-list-iter.h"
 
10
#include "imap-urlauth-private.h"
 
11
#include "imap-urlauth-backend.h"
 
12
 
 
13
#define IMAP_URLAUTH_KEY MAILBOX_ATTRIBUTE_PREFIX_DOVECOT"imap-urlauth"
 
14
 
 
15
static int
 
16
imap_urlauth_backend_trans_get_mailbox_key(struct mailbox_transaction_context *trans,
 
17
                                           bool create,
 
18
                                           unsigned char mailbox_key_r[IMAP_URLAUTH_KEY_LEN],
 
19
                                           const char **error_r,
 
20
                                           enum mail_error *error_code_r)
 
21
{
 
22
        struct mailbox *box = mailbox_transaction_get_mailbox(trans);
 
23
        struct mail_user *user = mail_storage_get_user(mailbox_get_storage(box));
 
24
        struct mail_attribute_value urlauth_key;
 
25
        const char *mailbox_key_hex = NULL;
 
26
        buffer_t key_buf;
 
27
        int ret;
 
28
 
 
29
        *error_r = "Internal server error";
 
30
        *error_code_r = MAIL_ERROR_TEMP;
 
31
 
 
32
        ret = mailbox_attribute_get(trans, MAIL_ATTRIBUTE_TYPE_PRIVATE,
 
33
                                    IMAP_URLAUTH_KEY, &urlauth_key);
 
34
        if (ret < 0)
 
35
                return -1;
 
36
 
 
37
        if (user->mail_debug) {
 
38
                i_debug("imap-urlauth: %skey found for mailbox %s",
 
39
                        (ret > 0 ? "" : "no "), mailbox_get_vname(box));
 
40
        }
 
41
 
 
42
        if (ret == 0) {
 
43
                if (!create)
 
44
                        return 0;
 
45
 
 
46
                /* create new key */
 
47
                random_fill(mailbox_key_r, IMAP_URLAUTH_KEY_LEN);
 
48
                mailbox_key_hex = binary_to_hex(mailbox_key_r,
 
49
                                                IMAP_URLAUTH_KEY_LEN);
 
50
                memset(&urlauth_key, 0, sizeof(urlauth_key));
 
51
                urlauth_key.value = mailbox_key_hex;
 
52
                ret = mailbox_attribute_set(trans, MAIL_ATTRIBUTE_TYPE_PRIVATE,
 
53
                                            IMAP_URLAUTH_KEY, &urlauth_key);
 
54
                if (ret < 0)
 
55
                        return -1;
 
56
                if (user->mail_debug) {
 
57
                        i_debug("imap-urlauth: created key for mailbox %s",
 
58
                                mailbox_get_vname(box));
 
59
                }
 
60
        } else {
 
61
                /* read existing key */
 
62
                buffer_create_from_data(&key_buf, mailbox_key_r,
 
63
                                        IMAP_URLAUTH_KEY_LEN);
 
64
                mailbox_key_hex = urlauth_key.value;
 
65
                if (strlen(mailbox_key_hex) != 2*IMAP_URLAUTH_KEY_LEN ||
 
66
                    hex_to_binary(mailbox_key_hex, &key_buf) < 0 ||
 
67
                    key_buf.used != IMAP_URLAUTH_KEY_LEN) {
 
68
                        i_error("imap-urlauth: key found for mailbox %s is invalid",
 
69
                                mailbox_get_vname(box));
 
70
                        return -1;
 
71
                }
 
72
        }
 
73
        return 1;
 
74
}
 
75
 
 
76
int imap_urlauth_backend_get_mailbox_key(struct mailbox *box, bool create,
 
77
                                         unsigned char mailbox_key_r[IMAP_URLAUTH_KEY_LEN],
 
78
                                         const char **error_r,
 
79
                                         enum mail_error *error_code_r)
 
80
{
 
81
        struct mailbox_transaction_context *t;
 
82
        int ret;
 
83
 
 
84
        t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL);
 
85
        ret = imap_urlauth_backend_trans_get_mailbox_key(t, create, mailbox_key_r, error_r, error_code_r);
 
86
        if (mailbox_transaction_commit(&t) < 0)
 
87
                ret = -1;
 
88
        return ret;
 
89
}
 
90
 
 
91
int imap_urlauth_backend_reset_mailbox_key(struct mailbox *box)
 
92
{
 
93
        struct mailbox_transaction_context *t;
 
94
        int ret;
 
95
 
 
96
        t = mailbox_transaction_begin(box, MAILBOX_TRANSACTION_FLAG_EXTERNAL);
 
97
        ret = mailbox_attribute_unset(t, MAIL_ATTRIBUTE_TYPE_PRIVATE,
 
98
                                      IMAP_URLAUTH_KEY);
 
99
        if (mailbox_transaction_commit(&t) < 0)
 
100
                ret = -1;
 
101
        return ret;
 
102
}
 
103
 
 
104
static int imap_urlauth_backend_mailbox_reset_key(struct mailbox *box)
 
105
{
 
106
        const char *errstr;
 
107
        enum mail_error error;
 
108
 
 
109
        if (mailbox_open(box) < 0) {
 
110
                errstr = mailbox_get_last_error(box, &error);
 
111
                if (error == MAIL_ERROR_NOTFOUND || error == MAIL_ERROR_PERM)
 
112
                        return 0;
 
113
                i_error("urlauth key reset: Couldn't open mailbox %s: %s",
 
114
                        mailbox_get_vname(box), errstr);
 
115
                return -1;
 
116
        }
 
117
        return imap_urlauth_backend_reset_mailbox_key(box);
 
118
}
 
119
 
 
120
int imap_urlauth_backend_reset_all_keys(struct mail_user *user)
 
121
 
122
        const char *const patterns[] = { "*", NULL };
 
123
        struct mailbox_list_iterate_context *iter;
 
124
        const struct mailbox_info *info;
 
125
        struct mailbox *box;
 
126
        int ret = 0;
 
127
 
 
128
        iter = mailbox_list_iter_init_namespaces(user->namespaces, patterns,
 
129
                                                 MAIL_NAMESPACE_TYPE_MASK_ALL,
 
130
                                                 MAILBOX_LIST_ITER_NO_AUTO_BOXES |
 
131
                                                 MAILBOX_LIST_ITER_SKIP_ALIASES |
 
132
                                                 MAILBOX_LIST_ITER_RETURN_NO_FLAGS);
 
133
        while ((info = mailbox_list_iter_next(iter)) != NULL) {
 
134
                box = mailbox_alloc(info->ns->list, info->vname, 0);
 
135
                if (imap_urlauth_backend_mailbox_reset_key(box) < 0)
 
136
                        ret = -1;
 
137
                mailbox_free(&box);
 
138
        }
 
139
        if (mailbox_list_iter_deinit(&iter) < 0)
 
140
                ret = -1;
 
141
        return ret;
 
142
}