summaryrefslogtreecommitdiff
path: root/imap-dl
blob: 5a8494c3eff0731e069579dc1cda22195374c2c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/python3
# PYTHON_ARGCOMPLETE_OK
# -*- coding: utf-8 -*-

# Copyright (C) 2019-2020 Daniel Kahn Gillmor
# Copyright (C) 2020      Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

DESCRIPTION = '''Fetch messages from an IMAP inbox into a maildir

Example config file:

----------
[retriever]
server = mail.example.net
username = foo
password = sekr1t!

[destination]
path = /home/foo/Maildir

[options]
delete = True
----------

Run "man imap-dl" for more details.
'''

import re
import sys
import ssl
import enum
import time
import imaplib
import logging
import mailbox
import os.path
import argparse
import statistics
import configparser

from typing import Dict, List, Optional, Tuple, Union

try:
    import argcomplete #type: ignore
except ImportError:
    argcomplete = None

try:
    import gssapi # type: ignore
except ModuleNotFoundError:
    gssapi = None


class Splitter(object):
    def __init__(self, name:str, match:bytes):
        self._splitter = re.compile(match)
        self._name = name
    def breakup(self, line:bytes) -> Dict[str,int]:
        match = self._splitter.match(line)
        if not match:
            raise Exception(f'malformed {self._name} line {line!r}')
        ret:Dict[str,int] = {}
        i:str
        for i in ['id', 'uid', 'size']:
            ret[i] = int(match[i])
        return ret

OnSizeMismatch = enum.Enum('OnSizeMismatch', 'warn error none')

# b'1 (UID 160 RFC822.SIZE 1867)' -> {id: 1, uid: 160, size: 1867}
_summary_re = rb'^(?P<id>[0-9]+) \(UID (?P<uid>[0-9]+) RFC822.SIZE (?P<size>[0-9]+)\)$'
summary_splitter = Splitter('summary', _summary_re)
# b'1 (UID 160 BODY[] {1867}' -> {id: 1, uid: 160, size: 1867}
_fetch_re = rb'^(?P<id>[0-9]+) \(UID (?P<uid>[0-9]+) (FLAGS \([\\A-Za-z ]*\) )?BODY\[\] \{(?P<size>[0-9]+)\}$'
fetch_splitter = Splitter('fetch', _fetch_re)

def auth_builtin(username:str, imap:imaplib.IMAP4_SSL,
                 conf:configparser.ConfigParser, server:str) -> None:
    logging.info('Logging in as %s', username)
    resp:Tuple[str, List[Union[bytes,Tuple[bytes,bytes]]]]
    resp = imap.login(username, conf.get('retriever', 'password'))
    if resp[0] != 'OK':
        raise Exception(f'login failed with {resp} as user {username} on {server}')

# imaplib auth methods need to be in the form of callables, and they all
# requre both additional parameters and storage beyond what the function
# interface provides.
class GSSAPI_handler():
    gss_vc:gssapi.SecurityContext
    username:str

    def __init__(self, server:str, username:str) -> None:
        name = gssapi.Name(f'imap@{server}', gssapi.NameType.hostbased_service)
        self.gss_vc = gssapi.SecurityContext(usage="initiate", name=name)
        self.username = username

    def __call__(self, token:Optional[bytes]) -> bytes:
        if token == b"":
            token = None
        if not self.gss_vc.complete:
            response = self.gss_vc.step(token)
            return response if response else b"" # type: ignore
        elif token is None:
            return b""

        response = self.gss_vc.unwrap(token)

        # Preserve the "length" of the message we received, and set the first
        # byte to one.  If username is provided, it's next.
        reply:List[int] = []
        reply[0:4] = response.message[0:4]
        reply[0] = 1
        if self.username:
            reply[5:] = self.username.encode("utf-8")

        response = self.gss_vc.wrap(bytes(reply), response.encrypted)
        return response.message if response.message else b"" # type: ignore

def auth_gssapi(username:str, imap:imaplib.IMAP4_SSL,
                conf:configparser.ConfigParser, server:str) -> None:
    if not gssapi:
        raise Exception('Kerberos requested, but python3-gssapi not found')

    logging.info(f'Logging in as {username} with GSSAPI')

    callback = GSSAPI_handler(server, username)
    resp = imap.authenticate("GSSAPI", callback)
    if resp[0] != 'OK':
        raise Exception(f'GSSAPI login failed with {resp} as user {username} on {server}')

def scan_msgs(configfile:str, verbose:bool) -> None:
    conf = configparser.ConfigParser()
    conf.read_file(open(configfile, 'r'))
    oldloglevel = logging.getLogger().getEffectiveLevel()
    conf_verbose = conf.getint('options', 'verbose', fallback=1)
    if conf_verbose > 1:
        verbose = True
    if verbose:
        logging.getLogger().setLevel(logging.INFO)
    logging.info('pulling from config file %s', configfile)
    delete = conf.getboolean('options', 'delete', fallback=False)
    read_all = conf.getboolean('options', 'read_all', fallback=True)
    if not read_all:
        raise NotImplementedError('imap-dl only supports options.read_all=True, got False')
    rtype = conf.get('retriever', 'type', fallback='SimpleIMAPSSLRetriever')
    if rtype.lower() != 'simpleimapsslretriever':
        raise NotImplementedError(f'imap-dl only supports retriever.type=SimpleIMAPSSLRetriever, got {rtype}')
    # FIXME: handle `retriever.record_mailbox`
    dtype = conf.get('destination', 'type', fallback='Maildir')
    if dtype.lower() != 'maildir':
        raise NotImplementedError(f'imap-dl only supports destination.type=Maildir, got {dtype}')
    dst = conf.get('destination', 'path')
    dst = os.path.expanduser(dst)
    if os.path.exists(dst) and not os.path.isdir(dst):
        raise Exception('expected destination directory, but %s is not a directory'%(dst,))
    mdst:mailbox.Maildir = mailbox.Maildir(dst, create=True)
    ca_certs = conf.get('retriever', 'ca_certs', fallback=None)
    on_size_mismatch_str:str = conf.get('options', 'on_size_mismatch', fallback='error').lower()
    try:
        on_size_mismatch:OnSizeMismatch = OnSizeMismatch.__members__[on_size_mismatch_str]
    except KeyError:
        raise Exception(f'options.on_size_mismatch value should be one of:\n'
                        '{list(OnSizeMismatch.__members__.keys())}\n'
                        '(found "{on_size_mismatch_str}")')

    ctx = ssl.create_default_context(cafile=ca_certs)
    ssl_ciphers = conf.get('retriever', 'ssl_ciphers', fallback=None)
    if ssl_ciphers:
        ctx.set_ciphers(ssl_ciphers)

    server:str = conf.get('retriever', 'server')
    with imaplib.IMAP4_SSL(host=server, #type: ignore
                           port=int(conf.get('retriever', 'port', fallback=993)),
                           ssl_context=ctx) as imap:
        username:str = conf.get('retriever', 'username')
        authentication:str = conf.get('retriever', 'authentication',
                                      fallback='basic')
        # FIXME: have the default automatically choose an opinionated
        # best authentication method.  e.g., if the gssapi module is
        # installed and the user has a reasonable identity in their
        # local credential cache, choose kerberos, otherwise, choose
        # "basic".
        if authentication in ['kerberos', 'gssapi']:
            auth_gssapi(username, imap, conf, server)
        elif authentication == 'basic':
            auth_builtin(username, imap, conf, server)
        else:
            # FIXME: implement other authentication mechanisms
            raise Exception(f'retriever.authentication should be one of:\n'
                            '"basic" or "gssapi" (or "kerberos"). Got "{authentication}"')

        if verbose: # only enable debugging after login to avoid leaking credentials in the log
            imap.debug = 4
        logging.info("capabilities reported: %s", ', '.join(imap.capabilities))
        resp = imap.select(readonly=not delete)
        if resp[0] != 'OK':
            raise Exception(f'selection failed: {resp}')
        if len(resp[1]) != 1:
            raise Exception(f'expected exactly one EXISTS response from select, got {resp[1]}')
        data:Union[bytes,Tuple[bytes,bytes]] = resp[1][0]
        if not isinstance(data, bytes):
            raise Exception(f'expected bytes in response to SELECT, got {data}')
        n:int = int(data)
        if n == 0:
            logging.info('No messages to retrieve')
        else:
            pull_msgs(imap, n, mdst, on_size_mismatch, delete)
    logging.getLogger().setLevel(oldloglevel)

def pull_msgs(imap:imaplib.IMAP4_SSL, n:int, mdst:mailbox.Maildir,
              on_size_mismatch:OnSizeMismatch, delete:bool) -> None:
    sizes_mismatched:List[int] = []
    resp:Tuple[str, List[Union[bytes,Tuple[bytes,bytes]]]]
    resp = imap.fetch('1:%d'%(n), '(UID RFC822.SIZE)')
    if resp[0] != 'OK':
        raise Exception(f'initial FETCH 1:{n} not OK ({resp})')

    pending:List[Dict[str,int]] = []
    for data in resp[1]:
        if not isinstance(data, bytes):
            raise TypeError(f'Expected bytes, got {type(data)}')
        pending.append(summary_splitter.breakup(data))

    sizes:Dict[int,int] = {}
    for m in pending:
        sizes[m['uid']] = m['size']
    fetched:Dict[int,int] = {}
    uids = ','.join(map(str, sorted([x['uid'] for x in pending])))
    totalbytes = sum([x['size'] for x in pending])
    logging.info('Fetching %d messages, expecting %d bytes of message content',
                 len(pending), totalbytes)
    # FIXME: sort by size?
    # FIXME: fetch in batches or singly instead of all-at-once?
    # FIXME: rolling deletion?
    # FIXME: asynchronous work?
    before = time.perf_counter()
    resp = imap.uid('FETCH', uids, '(UID BODY.PEEK[])')
    after = time.perf_counter()
    if resp[0] != 'OK':
        raise Exception('UID fetch failed {resp[0]}')
    expected_objects:int = len(pending) * 2
    if len(resp[1]) != expected_objects:
        raise Exception(f'expected {expected_objects} responses for fetch, got {len(resp[1])}')
    for n in range(0, expected_objects, 2):
        # expected response is one "fetch" line, followed by a close-paren item
        data = resp[1][n]
        if not isinstance(data, tuple) or len(data) != 2:
            raise Exception(f'expected 2-part tuple, got {type(data)}')

        closeparen = resp[1][n+1]
        if not isinstance(closeparen, bytes) or closeparen != b')':
            raise Exception('Expected close parenthesis after message fetch')

        m = fetch_splitter.breakup(data[0])
        if m['size'] != len(data[1]):
            raise Exception(f'expected {m["size"]} octets, got {len(data[1])}')
        if m['size'] != sizes[m['uid']]:
            if on_size_mismatch == OnSizeMismatch.warn:
                if len(sizes_mismatched) == 0:
                    logging.warning('size mismatch: summary said %d octets, fetch sent %d',
                                    sizes[m['uid']], m['size'])
                elif len(sizes_mismatched) == 1:
                    logging.warning('size mismatch: (mismatches after the first suppressed until summary)')
                sizes_mismatched.append(sizes[m['uid']] - m['size'])
            elif on_size_mismatch == OnSizeMismatch.error:
                raise Exception(f"size mismatch: summary said {sizes[m['uid']]} octets, "
                                "fetch sent {m['size']}\n"
                                "(set options.on_size_mismatch to none or warn to avoid hard failure)")
        # convert any CRLF line-endings to UNIX standard line-
        # endings:
        fname = mdst.add(data[1].replace(b'\r\n', b'\n'))
        logging.info('stored message %d/%d (uid %d, %d bytes) in %s',
                     len(fetched) + 1, len(pending), m['uid'], m['size'], fname)
        del sizes[m['uid']]
        fetched[m['uid']] = m['size']
    if sizes:
        logging.warning('unhandled UIDs: %s', sizes)
    logging.info('%d bytes of %d messages fetched in %g seconds (~%g KB/s)',
                 sum(fetched.values()), len(fetched), after - before,
                 sum(fetched.values())/((after - before)*1024))
    if on_size_mismatch == OnSizeMismatch.warn and len(sizes_mismatched) > 1:
        logging.warning('%d size mismatches out of %d messages (mismatches in bytes: mean %f, stddev %f)',
                        len(sizes_mismatched), len(fetched),
                        statistics.mean(sizes_mismatched),
                        statistics.stdev(sizes_mismatched))
    if delete:
        logging.info('trying to delete %d messages from IMAP store', len(fetched))
        resp = imap.uid('STORE', ','.join(map(str, fetched.keys())), '+FLAGS', r'(\Deleted)')
        if resp[0] != 'OK':
            raise Exception(f'failed to set \\Deleted flag: {resp}')
        resp = imap.expunge()
        if resp[0] != 'OK':
            raise Exception(f'failed to expunge! {resp}')
    else:
        logging.info('not deleting any messages, since options.delete is not set')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=DESCRIPTION,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        'config', nargs='+', metavar='CONFIG',
        help="configuration file")
    parser.add_argument(
        '-v', '--verbose', action='store_true',
        help="verbose log output")

    if argcomplete:
        argcomplete.autocomplete(parser)
    elif '_ARGCOMPLETE' in os.environ:
        logging.error('Argument completion requested but the "argcomplete" '
                      'module is not installed. '
                      'Maybe you want to "apt install python3-argcomplete"')
        sys.exit(1)

    args = parser.parse_args()

    if args.verbose:
        logging.getLogger().setLevel(logging.INFO)

    errs = {}
    for confname in args.config:
        try:
            scan_msgs(confname, args.verbose)
        except imaplib.IMAP4.error as e:
            logging.error('IMAP failure for config file %s: %s', confname, e)
            errs[confname] = e
    if errs:
        exit(1)