2002-06-19 06:39:00 +02:00
|
|
|
# IMAP server support
|
2018-06-07 18:39:08 +02:00
|
|
|
# Copyright (C) 2002-2018 John Goerzen & contributors.
|
2002-06-19 06:39:00 +02:00
|
|
|
#
|
|
|
|
# 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
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-06-19 06:39:00 +02:00
|
|
|
#
|
|
|
|
# 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, write to the Free Software
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-06-19 06:39:00 +02:00
|
|
|
|
2018-11-18 19:52:00 +01:00
|
|
|
import datetime
|
2020-10-31 00:06:02 +01:00
|
|
|
import hashlib
|
2011-03-11 22:13:21 +01:00
|
|
|
import hmac
|
2014-01-09 21:30:41 +01:00
|
|
|
import json
|
2020-10-31 15:24:40 +01:00
|
|
|
import urllib.request
|
|
|
|
import urllib.parse
|
|
|
|
import urllib.error
|
2011-08-15 11:55:42 +02:00
|
|
|
import time
|
2011-08-15 13:15:36 +02:00
|
|
|
import errno
|
2016-07-25 03:20:53 +02:00
|
|
|
import socket
|
2011-05-04 16:45:25 +02:00
|
|
|
from socket import gaierror
|
2016-07-25 03:20:53 +02:00
|
|
|
from sys import exc_info
|
2012-02-05 12:17:02 +01:00
|
|
|
from ssl import SSLError, cert_time_to_seconds
|
2016-06-29 03:42:57 +02:00
|
|
|
from threading import Lock, BoundedSemaphore, Thread, Event, currentThread
|
2015-02-15 15:16:20 +01:00
|
|
|
import offlineimap.accounts
|
2016-06-29 03:42:57 +02:00
|
|
|
from offlineimap import imaplibutil, imaputil, threadutil, OfflineImapError
|
2015-02-15 15:16:20 +01:00
|
|
|
from offlineimap.ui import getglobalui
|
|
|
|
|
2008-03-09 06:46:51 +01:00
|
|
|
try:
|
2018-02-27 00:08:00 +01:00
|
|
|
import gssapi
|
|
|
|
have_gss = True
|
2008-03-09 06:46:51 +01:00
|
|
|
except ImportError:
|
2018-02-27 00:08:00 +01:00
|
|
|
have_gss = False
|
2002-07-03 15:04:40 +02:00
|
|
|
|
2016-05-20 18:20:07 +02:00
|
|
|
|
2020-10-31 15:39:28 +01:00
|
|
|
class IMAPServer:
|
2011-08-12 08:31:09 +02:00
|
|
|
"""Initializes all variables from an IMAPRepository() instance
|
|
|
|
|
|
|
|
Various functions, such as acquireconnection() return an IMAP4
|
2011-12-01 10:12:54 +01:00
|
|
|
object on which we can operate.
|
|
|
|
|
|
|
|
Public instance variables are: self.:
|
|
|
|
delim The server's folder delimiter. Only valid after acquireconnection()
|
2015-01-14 22:58:25 +01:00
|
|
|
"""
|
|
|
|
|
2011-08-12 08:31:09 +02:00
|
|
|
def __init__(self, repos):
|
2016-05-19 08:35:45 +02:00
|
|
|
""":repos: a IMAPRepository instance."""
|
|
|
|
|
2011-01-05 17:00:55 +01:00
|
|
|
self.ui = getglobalui()
|
2011-08-12 08:31:09 +02:00
|
|
|
self.repos = repos
|
|
|
|
self.config = repos.getconfig()
|
2013-05-03 15:56:20 +02:00
|
|
|
|
|
|
|
self.preauth_tunnel = repos.getpreauthtunnel()
|
|
|
|
self.transport_tunnel = repos.gettransporttunnel()
|
|
|
|
if self.preauth_tunnel and self.transport_tunnel:
|
2020-08-29 20:19:23 +02:00
|
|
|
raise OfflineImapError('%s: ' % repos +
|
|
|
|
'you must enable precisely one '
|
|
|
|
'type of tunnel (preauth or transport), '
|
|
|
|
'not both', OfflineImapError.ERROR.REPO)
|
2013-05-03 15:56:20 +02:00
|
|
|
self.tunnel = \
|
2015-01-14 22:58:25 +01:00
|
|
|
self.preauth_tunnel if self.preauth_tunnel \
|
2020-08-29 20:19:23 +02:00
|
|
|
else self.transport_tunnel
|
2013-05-03 15:56:20 +02:00
|
|
|
|
|
|
|
self.username = \
|
2015-01-14 22:58:25 +01:00
|
|
|
None if self.preauth_tunnel else repos.getuser()
|
2013-08-03 14:06:44 +02:00
|
|
|
self.user_identity = repos.get_remote_identity()
|
2013-08-07 13:43:51 +02:00
|
|
|
self.authmechs = repos.get_auth_mechanisms()
|
2011-08-12 08:31:09 +02:00
|
|
|
self.password = None
|
2002-11-05 00:15:42 +01:00
|
|
|
self.passworderror = None
|
2007-07-05 06:04:14 +02:00
|
|
|
self.goodpassword = None
|
2013-05-03 15:56:20 +02:00
|
|
|
|
|
|
|
self.usessl = repos.getssl()
|
2016-02-23 01:45:18 +01:00
|
|
|
self.useipv6 = repos.getipv6()
|
2016-09-13 04:41:31 +02:00
|
|
|
if self.useipv6 is True:
|
2016-02-23 01:45:18 +01:00
|
|
|
self.af = socket.AF_INET6
|
2016-09-13 04:41:31 +02:00
|
|
|
elif self.useipv6 is False:
|
2016-02-23 01:45:18 +01:00
|
|
|
self.af = socket.AF_INET
|
|
|
|
else:
|
|
|
|
self.af = socket.AF_UNSPEC
|
2017-10-27 18:52:01 +02:00
|
|
|
self.hostname = None if self.transport_tunnel or self.preauth_tunnel else repos.gethost()
|
2011-08-12 08:31:09 +02:00
|
|
|
self.port = repos.getport()
|
2016-09-13 04:41:31 +02:00
|
|
|
if self.port is None:
|
2011-08-12 08:31:09 +02:00
|
|
|
self.port = 993 if self.usessl else 143
|
|
|
|
self.sslclientcert = repos.getsslclientcert()
|
|
|
|
self.sslclientkey = repos.getsslclientkey()
|
|
|
|
self.sslcacertfile = repos.getsslcacertfile()
|
2011-08-16 10:48:37 +02:00
|
|
|
if self.sslcacertfile is None:
|
2020-08-29 20:19:23 +02:00
|
|
|
self.__verifycert = None # Disable cert verification.
|
|
|
|
# This way of working sucks hard...
|
2015-01-18 08:45:46 +01:00
|
|
|
self.fingerprint = repos.get_ssl_fingerprint()
|
2015-08-25 05:32:00 +02:00
|
|
|
self.tlslevel = repos.gettlslevel()
|
2016-07-25 03:20:53 +02:00
|
|
|
self.sslversion = repos.getsslversion()
|
2016-06-23 03:55:00 +02:00
|
|
|
self.starttls = repos.getstarttls()
|
2013-05-03 15:56:20 +02:00
|
|
|
|
2018-07-02 17:36:22 +02:00
|
|
|
if self.usessl \
|
2020-08-29 20:19:23 +02:00
|
|
|
and self.tlslevel != "tls_compat" \
|
|
|
|
and self.sslversion is None:
|
2018-07-02 17:31:23 +02:00
|
|
|
raise Exception("When 'tls_level' is not 'tls_compat' "
|
2020-08-29 20:19:23 +02:00
|
|
|
"the 'ssl_version' must be set explicitly.")
|
2016-07-25 03:20:53 +02:00
|
|
|
|
2014-01-09 21:30:41 +01:00
|
|
|
self.oauth2_refresh_token = repos.getoauth2_refresh_token()
|
2015-12-28 14:53:53 +01:00
|
|
|
self.oauth2_access_token = repos.getoauth2_access_token()
|
2014-01-09 21:30:41 +01:00
|
|
|
self.oauth2_client_id = repos.getoauth2_client_id()
|
|
|
|
self.oauth2_client_secret = repos.getoauth2_client_secret()
|
|
|
|
self.oauth2_request_url = repos.getoauth2_request_url()
|
2018-11-18 19:52:00 +01:00
|
|
|
self.oauth2_access_token_expires_at = None
|
2014-01-09 21:30:41 +01:00
|
|
|
|
2002-06-19 07:22:21 +02:00
|
|
|
self.delim = None
|
|
|
|
self.root = None
|
2011-08-12 08:31:09 +02:00
|
|
|
self.maxconnections = repos.getmaxconnections()
|
2002-07-03 15:04:40 +02:00
|
|
|
self.availableconnections = []
|
|
|
|
self.assignedconnections = []
|
2002-07-11 05:51:20 +02:00
|
|
|
self.lastowner = {}
|
2002-07-03 15:04:40 +02:00
|
|
|
self.semaphore = BoundedSemaphore(self.maxconnections)
|
|
|
|
self.connectionlock = Lock()
|
2011-08-12 08:31:09 +02:00
|
|
|
self.reference = repos.getreference()
|
|
|
|
self.idlefolders = repos.getidlefolders()
|
2008-03-09 06:46:51 +01:00
|
|
|
self.gss_vc = None
|
|
|
|
self.gssapi = False
|
2002-06-19 06:39:00 +02:00
|
|
|
|
2015-02-15 15:16:20 +01:00
|
|
|
# In order to support proxy connection, we have to override the
|
|
|
|
# default socket instance with our own socksified socket instance.
|
|
|
|
# We add this option to bypass the GFW in China.
|
2016-12-19 06:11:44 +01:00
|
|
|
self.proxied_socket = self._get_proxy('proxy', socket.socket)
|
2015-02-15 15:16:20 +01:00
|
|
|
|
2016-12-19 06:11:44 +01:00
|
|
|
# Turns out that the GFW in China is no longer blocking imap.gmail.com
|
|
|
|
# However accounts.google.com (for oauth2) definitey is. Therefore
|
|
|
|
# it is not strictly necessary to use a proxy for *both* IMAP *and*
|
|
|
|
# oauth2, so a new option is added: authproxy.
|
|
|
|
|
|
|
|
# Set proxy for use in authentication (only) if desired.
|
|
|
|
# If not set, is same as proxy option (compatible with current configs)
|
|
|
|
# To use a proxied_socket but not an authproxied_socket
|
|
|
|
# set authproxy = '' in config
|
|
|
|
self.authproxied_socket = self._get_proxy('authproxy',
|
|
|
|
self.proxied_socket)
|
|
|
|
|
|
|
|
def _get_proxy(self, proxysection, dfltsocket):
|
|
|
|
_account_section = 'Account ' + self.repos.account.name
|
|
|
|
if not self.config.has_option(_account_section, proxysection):
|
|
|
|
return dfltsocket
|
|
|
|
proxy = self.config.get(_account_section, proxysection)
|
|
|
|
if proxy == '':
|
|
|
|
# explicitly set no proxy (overrides default return of dfltsocket)
|
|
|
|
return socket.socket
|
|
|
|
|
|
|
|
# Powered by PySocks.
|
|
|
|
try:
|
|
|
|
import socks
|
|
|
|
proxy_type, host, port = proxy.split(":")
|
|
|
|
port = int(port)
|
|
|
|
socks.setdefaultproxy(getattr(socks, proxy_type), host, port)
|
|
|
|
return socks.socksocket
|
|
|
|
except ImportError:
|
|
|
|
self.ui.warn("PySocks not installed, ignoring proxy option.")
|
|
|
|
except (AttributeError, ValueError) as e:
|
|
|
|
self.ui.warn("Bad proxy option %s for account %s: %s "
|
2020-08-29 20:19:23 +02:00
|
|
|
"Ignoring %s option." %
|
|
|
|
(proxy, self.repos.account.name, e, proxysection))
|
2016-12-19 06:11:44 +01:00
|
|
|
return dfltsocket
|
2015-02-15 15:16:20 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __getpassword(self):
|
2011-08-12 08:31:09 +02:00
|
|
|
"""Returns the server password or None"""
|
2016-06-16 19:51:41 +02:00
|
|
|
|
2020-08-30 11:27:03 +02:00
|
|
|
if self.goodpassword is not None: # use cached good one first
|
2007-07-05 06:04:14 +02:00
|
|
|
return self.goodpassword
|
|
|
|
|
2020-08-30 11:27:03 +02:00
|
|
|
if self.password is not None and self.passworderror is None:
|
2020-08-29 20:19:23 +02:00
|
|
|
return self.password # non-failed preconfigured one
|
2002-11-05 00:15:42 +01:00
|
|
|
|
2011-08-12 08:31:09 +02:00
|
|
|
# get 1) configured password first 2) fall back to asking via UI
|
|
|
|
self.password = self.repos.getpassword() or \
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.getpass(self.username, self.config, self.passworderror)
|
2002-11-05 00:15:42 +01:00
|
|
|
self.passworderror = None
|
|
|
|
return self.password
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __md5handler(self, response):
|
2002-11-02 23:30:41 +01:00
|
|
|
challenge = response.strip()
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.debug('imap', '__md5handler: got challenge %s' % challenge)
|
2002-11-06 02:10:14 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
passwd = self.__getpassword()
|
2020-10-31 00:06:02 +01:00
|
|
|
retval = self.username + ' ' +\
|
|
|
|
hmac.new(bytes(passwd, encoding='utf-8'), challenge,
|
|
|
|
digestmod=hashlib.md5).hexdigest()
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.debug('imap', '__md5handler: returning %s' % retval)
|
2002-11-02 23:14:02 +01:00
|
|
|
return retval
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __loginauth(self, imapobj):
|
2015-01-08 17:13:33 +01:00
|
|
|
""" Basic authentication via LOGIN command."""
|
|
|
|
|
2013-08-05 23:10:10 +02:00
|
|
|
self.ui.debug('imap', 'Attempting IMAP LOGIN authentication')
|
2014-03-16 13:27:35 +01:00
|
|
|
imapobj.login(self.username, self.__getpassword())
|
2008-03-09 06:46:51 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __plainhandler(self, response):
|
2015-01-08 17:13:33 +01:00
|
|
|
"""Implements SASL PLAIN authentication, RFC 4616,
|
|
|
|
http://tools.ietf.org/html/rfc4616"""
|
2013-08-03 14:06:44 +02:00
|
|
|
|
|
|
|
authc = self.username
|
2018-06-13 14:13:50 +02:00
|
|
|
if not authc:
|
|
|
|
raise OfflineImapError("No username provided for '%s'"
|
2020-08-29 20:19:23 +02:00
|
|
|
% self.repos.getname(),
|
2018-06-13 14:13:50 +02:00
|
|
|
OfflineImapError.ERROR.REPO)
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
passwd = self.__getpassword()
|
2020-08-28 13:06:31 +02:00
|
|
|
authz = ''
|
|
|
|
NULL = '\x00'
|
2020-08-30 11:27:03 +02:00
|
|
|
if self.user_identity is not None:
|
2013-08-03 14:06:44 +02:00
|
|
|
authz = self.user_identity
|
2020-08-28 13:06:31 +02:00
|
|
|
|
2016-06-16 19:51:41 +02:00
|
|
|
retval = NULL.join((authz, authc, passwd))
|
2021-02-20 01:18:04 +01:00
|
|
|
self.ui.debug('imap', '__plainhandler: returning %s %s '
|
|
|
|
'(passwd hidden for log)' % (authz, authc))
|
2013-08-03 14:06:44 +02:00
|
|
|
return retval
|
|
|
|
|
2014-01-09 21:30:41 +01:00
|
|
|
def __xoauth2handler(self, response):
|
2018-11-18 19:52:00 +01:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
if self.oauth2_access_token_expires_at \
|
|
|
|
and self.oauth2_access_token_expires_at < now:
|
|
|
|
self.oauth2_access_token = None
|
|
|
|
self.ui.debug('imap', 'xoauth2handler: oauth2_access_token expired')
|
|
|
|
|
2014-01-09 21:30:41 +01:00
|
|
|
if self.oauth2_access_token is None:
|
2016-04-25 18:38:31 +02:00
|
|
|
if self.oauth2_request_url is None:
|
|
|
|
raise OfflineImapError("No remote oauth2_request_url for "
|
2020-08-29 20:19:23 +02:00
|
|
|
"repository '%s' specified." %
|
|
|
|
self, OfflineImapError.ERROR.REPO)
|
2016-04-25 18:38:31 +02:00
|
|
|
|
|
|
|
# Generate new access token.
|
2014-01-09 21:30:41 +01:00
|
|
|
params = {}
|
|
|
|
params['client_id'] = self.oauth2_client_id
|
|
|
|
params['client_secret'] = self.oauth2_client_secret
|
|
|
|
params['refresh_token'] = self.oauth2_refresh_token
|
|
|
|
params['grant_type'] = 'refresh_token'
|
|
|
|
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.debug('imap', 'xoauth2handler: url "%s"' %
|
|
|
|
self.oauth2_request_url)
|
|
|
|
self.ui.debug('imap', 'xoauth2handler: params "%s"' % params)
|
2014-01-09 21:30:41 +01:00
|
|
|
|
2016-02-15 09:16:26 +01:00
|
|
|
original_socket = socket.socket
|
2016-12-19 06:11:44 +01:00
|
|
|
socket.socket = self.authproxied_socket
|
2016-02-15 09:16:26 +01:00
|
|
|
try:
|
2020-08-28 03:32:43 +02:00
|
|
|
response = urllib.request.urlopen(
|
imapserver.py: fix __xoauth2handler in Python 3
Error when using the XOAUTH2 token refresh logic:
("POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str. (configuration is: {....}", <traceback object at 0x7fc6e69f2b40>)
[' File ".../offlineimap3/offlineimap/accounts.py", line 298, in syncrunner
self.__sync()
', ' File ".../offlineimap3/offlineimap/accounts.py", line 374, in __sync
remoterepos.getfolders()
', ' File ".../offlineimap3/offlineimap/repository/IMAP.py", line 446, in getfolders
imapobj = self.imapserver.acquireconnection()
', ' File ".../offlineimap3/offlineimap/imapserver.py", line 579, in acquireconnection
self.__authn_helper(imapobj)
', ' File ".../offlineimap3/offlineimap/imapserver.py", line 443, in __authn_helper
if func(imapobj):
', ' File ".../offlineimap3/offlineimap/imapserver.py", line 377, in __authn_xoauth2
imapobj.authenticate(\'XOAUTH2\', self.__xoauth2handler)
', ' File ".../offlineimap3/venv/lib/python3.7/site-packages/imaplib2.py", line 681, in authenticate
typ, dat = self._simple_command(\'AUTHENTICATE\', mechanism.upper())
', ' File ".../offlineimap3/venv/lib/python3.7/site-packages/imaplib2.py", line 1674, in _simple_command
return self._command_complete(self._command(name, *args), kw)
', ' File ".../offlineimap3/venv/lib/python3.7/site-packages/imaplib2.py", line 1394, in _command
literal = literator(data, rqb)
', ' File ".../offlineimap3/venv/lib/python3.7/site-packages/imaplib2.py", line 2237, in process
ret = self.mech(self.decode(data))
', ' File ".../offlineimap3/offlineimap/imapserver.py", line 253, in __xoauth2handler
raise type(e)(msg, exc_info()[2])
']
Fix by encoding the data passed to urllib.
Signed-off-by: Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
2020-09-29 09:54:41 +02:00
|
|
|
self.oauth2_request_url, urllib.parse.urlencode(params).encode('utf-8')).read()
|
2016-08-15 02:07:14 +02:00
|
|
|
except Exception as e:
|
|
|
|
try:
|
2020-08-29 20:19:23 +02:00
|
|
|
msg = "%s (configuration is: %s)" % (e, str(params))
|
2016-08-15 02:07:14 +02:00
|
|
|
except Exception as eparams:
|
2020-08-29 20:19:23 +02:00
|
|
|
msg = "%s [cannot display configuration: %s]" % (e, eparams)
|
2020-09-03 21:02:19 +02:00
|
|
|
|
imapserver: fix exception handling in xoauth2handler
In case of an exception in XOAUTH2 code refresh (HTTP Error 400: Bad
Request), following exception occurs:
ERROR: While attempting to sync account 'xxx'
__init__() missing 3 required positional arguments: 'msg', 'hdrs', and 'fp'
Traceback:
File ".../offlineimap3/offlineimap/accounts.py", line 298, in syncrunner
self.__sync()
File ".../offlineimap3/offlineimap/accounts.py", line 374, in __sync
remoterepos.getfolders()
File ".../offlineimap3/offlineimap/repository/IMAP.py", line 648, in getfolders
imapobj = self.imapserver.acquireconnection()
File ".../offlineimap3/offlineimap/imapserver.py", line 591, in acquireconnection
self.__authn_helper(imapobj)
File ".../offlineimap3/offlineimap/imapserver.py", line 448, in __authn_helper
if func(imapobj):
File ".../offlineimap3/offlineimap/imapserver.py", line 382, in __authn_xoauth2
imapobj.authenticate('XOAUTH2', self.__xoauth2handler)
File ".../offlineimap3/venv/lib/python3.7/site-packages/imaplib2.py", line 682, in authenticate
typ, dat = self._simple_command('AUTHENTICATE', mechanism.upper())
File ".../offlineimap3/venv/lib/python3.7/site-packages/imaplib2.py", line 1675, in _simple_command
return self._command_complete(self._command(name, *args), kw)
File ".../offlineimap3/venv/lib/python3.7/site-packages/imaplib2.py", line 1395, in _command
literal = literator(data, rqb)
File ".../offlineimap3/venv/lib/python3.7/site-packages/imaplib2.py", line 2238, in process
ret = self.mech(self.decode(data))
File ".../offlineimap3/offlineimap/imapserver.py", line 257, in __xoauth2handler
raise type(e)(msg, exc_info()[2])
The exception 'e' is of type HTTPError, which does not have the same kind of
constructor as normal Python exceptions.
Instead, print the constructed message and just raise the existing
exception.
With that change, the same condition triggers another problem further on:
ERROR: While attempting to sync account 'xxx'
tuple index out of range
Traceback:
File ".../offlineimap3/offlineimap/accounts.py", line 298, in syncrunner
self.__sync()
File ".../offlineimap3/offlineimap/accounts.py", line 374, in __sync
remoterepos.getfolders()
File ".../offlineimap3/offlineimap/repository/IMAP.py", line 648, in getfolders
imapobj = self.imapserver.acquireconnection()
File ".../offlineimap3/offlineimap/imapserver.py", line 664, in acquireconnection
elif isinstance(e, socket.error) and e.args[0] == errno.ECONNREFUSED:
because e.args is empty.
2021-01-04 10:10:34 +01:00
|
|
|
self.ui.error(msg)
|
|
|
|
raise
|
2016-02-15 09:16:26 +01:00
|
|
|
finally:
|
|
|
|
socket.socket = original_socket
|
|
|
|
|
2014-01-09 21:30:41 +01:00
|
|
|
resp = json.loads(response)
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.debug('imap', 'xoauth2handler: response "%s"' % resp)
|
2020-08-28 03:32:43 +02:00
|
|
|
if 'error' in resp:
|
2020-08-29 20:19:23 +02:00
|
|
|
raise OfflineImapError("xoauth2handler got: %s" % resp,
|
|
|
|
OfflineImapError.ERROR.REPO)
|
2014-01-09 21:30:41 +01:00
|
|
|
self.oauth2_access_token = resp['access_token']
|
2020-08-28 03:32:43 +02:00
|
|
|
if 'expires_in' in resp:
|
2018-11-18 19:52:00 +01:00
|
|
|
self.oauth2_access_token_expires_at = now + datetime.timedelta(
|
2020-08-29 20:19:23 +02:00
|
|
|
seconds=resp['expires_in'] / 2
|
2018-11-18 19:52:00 +01:00
|
|
|
)
|
2014-01-09 21:30:41 +01:00
|
|
|
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.debug('imap', 'xoauth2handler: access_token "%s expires %s"' % (
|
2018-11-18 19:52:00 +01:00
|
|
|
self.oauth2_access_token, self.oauth2_access_token_expires_at))
|
2020-08-29 20:19:23 +02:00
|
|
|
auth_string = 'user=%s\1auth=Bearer %s\1\1' % (
|
2016-04-25 18:38:31 +02:00
|
|
|
self.username, self.oauth2_access_token)
|
2020-08-29 20:19:23 +02:00
|
|
|
# auth_string = base64.b64encode(auth_string)
|
|
|
|
self.ui.debug('imap', 'xoauth2handler: returning "%s"' % auth_string)
|
2014-01-09 21:30:41 +01:00
|
|
|
return auth_string
|
|
|
|
|
2018-02-27 00:08:00 +01:00
|
|
|
# Perform the next step handling a GSSAPI connection.
|
|
|
|
# Client sends first, so token will be ignored if there is no context.
|
|
|
|
def __gsshandler(self, token):
|
|
|
|
if token == "":
|
|
|
|
token = None
|
2008-03-09 06:46:51 +01:00
|
|
|
try:
|
2018-02-27 00:08:00 +01:00
|
|
|
if not self.gss_vc:
|
|
|
|
name = gssapi.Name('imap@' + self.hostname,
|
|
|
|
gssapi.NameType.hostbased_service)
|
|
|
|
self.gss_vc = gssapi.SecurityContext(usage="initiate",
|
|
|
|
name=name)
|
|
|
|
|
|
|
|
if not self.gss_vc.complete:
|
|
|
|
response = self.gss_vc.step(token)
|
|
|
|
return response if response else ""
|
2018-12-14 20:28:06 +01:00
|
|
|
elif token is None:
|
|
|
|
# uh... context is complete, so there's no negotiation we can
|
|
|
|
# do. But we also don't have a token, so we can't send any
|
|
|
|
# kind of response. Empirically, some (but not all) servers
|
|
|
|
# seem to put us in this state, and seem fine with getting no
|
|
|
|
# GSSAPI content in response, so give it to them.
|
|
|
|
return ""
|
2018-02-27 00:08:00 +01:00
|
|
|
|
|
|
|
# Don't bother checking qop because we're over a TLS channel
|
|
|
|
# already. But hey, if some server started encrypting tomorrow,
|
|
|
|
# we'd be ready since krb5 always requests integrity and
|
|
|
|
# confidentiality support.
|
|
|
|
response = self.gss_vc.unwrap(token)
|
2018-06-06 21:39:14 +02:00
|
|
|
|
|
|
|
# This is a behavior we got from pykerberos. First byte is one,
|
|
|
|
# first four bytes are preserved (pykerberos calls this a length).
|
|
|
|
# Any additional bytes are username.
|
imapserver: GSSAPI: make sure reply is all bytes
The current code mixed string and bytes leading to:
ERROR: Exceptions occurred during the run!
ERROR: While attempting to sync account 'honk.sigxcpu.org'
sequence item 1: expected str instance, int found
Traceback:
File "/usr/share/offlineimap3/offlineimap/accounts.py", line 298, in syncrunner
self.__sync()
File "/usr/share/offlineimap3/offlineimap/accounts.py", line 374, in __sync
remoterepos.getfolders()
File "/usr/share/offlineimap3/offlineimap/repository/IMAP.py", line 648, in getfolders
imapobj = self.imapserver.acquireconnection()
File "/usr/share/offlineimap3/offlineimap/imapserver.py", line 592, in acquireconnection
self.__authn_helper(imapobj)
File "/usr/share/offlineimap3/offlineimap/imapserver.py", line 449, in __authn_helper
if func(imapobj):
File "/usr/share/offlineimap3/offlineimap/imapserver.py", line 362, in __authn_gssapi
imapobj.authenticate('GSSAPI', self.__gsshandler)
File "/usr/lib/python3/dist-packages/imaplib2.py", line 691, in authenticate
typ, dat = self._simple_command('AUTHENTICATE', mechanism.upper())
File "/usr/lib/python3/dist-packages/imaplib2.py", line 1684, in _simple_command
return self._command_complete(self._command(name, *args), kw)
File "/usr/lib/python3/dist-packages/imaplib2.py", line 1404, in _command
literal = literator(data, rqb)
File "/usr/lib/python3/dist-packages/imaplib2.py", line 2247, in process
ret = self.mech(self.decode(data))
File "/usr/share/offlineimap3/offlineimap/imapserver.py", line 318, in __gsshandler
reply = ''.join(reply)
Closes: #46
Signed-off-by: Guido Günther <agx@sigxcpu.org>
2021-02-03 19:19:15 +01:00
|
|
|
reply = b'\x01' + response.message[1:4]
|
|
|
|
reply += bytes(self.username, 'utf-8')
|
2018-06-06 21:39:14 +02:00
|
|
|
|
|
|
|
response = self.gss_vc.wrap(reply, response.encrypted)
|
2018-02-27 00:08:00 +01:00
|
|
|
return response.message if response.message else ""
|
|
|
|
except gssapi.exceptions.GSSError as err:
|
|
|
|
# GSSAPI errored out on us; respond with None to cancel the
|
2008-03-09 06:46:51 +01:00
|
|
|
# authentication
|
2018-02-27 00:08:00 +01:00
|
|
|
self.ui.debug('imap', err.gen_message())
|
2008-03-09 06:46:51 +01:00
|
|
|
return None
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __start_tls(self, imapobj):
|
2013-08-07 13:43:51 +02:00
|
|
|
if 'STARTTLS' in imapobj.capabilities and not self.usessl:
|
|
|
|
self.ui.debug('imap', 'Using STARTTLS connection')
|
|
|
|
try:
|
|
|
|
imapobj.starttls()
|
|
|
|
except imapobj.error as e:
|
|
|
|
raise OfflineImapError("Failed to start "
|
2020-08-29 20:19:23 +02:00
|
|
|
"TLS connection: %s" % str(e),
|
2020-10-31 15:29:24 +01:00
|
|
|
OfflineImapError.ERROR.REPO,
|
|
|
|
exc_info()[2])
|
2020-08-29 20:19:23 +02:00
|
|
|
|
|
|
|
# All __authn_* procedures are helpers that do authentication.
|
|
|
|
# They are class methods that take one parameter, IMAP object.
|
|
|
|
#
|
|
|
|
# Each function should return True if authentication was
|
|
|
|
# successful and False if authentication wasn't even tried
|
|
|
|
# for some reason (but not when IMAP has no such authentication
|
|
|
|
# capability, calling code checks that).
|
|
|
|
#
|
|
|
|
# Functions can also raise exceptions; two types are special
|
|
|
|
# and will be handled by the calling code:
|
|
|
|
#
|
|
|
|
# - imapobj.error means that there was some error that
|
|
|
|
# comes from imaplib2;
|
|
|
|
#
|
|
|
|
# - OfflineImapError means that function detected some
|
|
|
|
# problem by itself.
|
2013-08-07 13:43:51 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __authn_gssapi(self, imapobj):
|
2013-08-07 13:43:51 +02:00
|
|
|
if not have_gss:
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.connectionlock.acquire()
|
|
|
|
try:
|
2018-02-27 00:08:00 +01:00
|
|
|
imapobj.authenticate('GSSAPI', self.__gsshandler)
|
2013-08-07 13:43:51 +02:00
|
|
|
return True
|
2020-08-30 18:35:01 +02:00
|
|
|
except imapobj.error:
|
2013-08-07 13:43:51 +02:00
|
|
|
self.gssapi = False
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
self.connectionlock.release()
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __authn_cram_md5(self, imapobj):
|
|
|
|
imapobj.authenticate('CRAM-MD5', self.__md5handler)
|
2013-08-07 13:43:51 +02:00
|
|
|
return True
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __authn_plain(self, imapobj):
|
|
|
|
imapobj.authenticate('PLAIN', self.__plainhandler)
|
2013-08-07 13:43:51 +02:00
|
|
|
return True
|
|
|
|
|
2014-01-09 21:30:41 +01:00
|
|
|
def __authn_xoauth2(self, imapobj):
|
2017-03-15 22:39:41 +01:00
|
|
|
if self.oauth2_refresh_token is None \
|
|
|
|
and self.oauth2_access_token is None:
|
|
|
|
return False
|
|
|
|
|
2014-01-09 21:30:41 +01:00
|
|
|
imapobj.authenticate('XOAUTH2', self.__xoauth2handler)
|
|
|
|
return True
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __authn_login(self, imapobj):
|
2013-08-07 13:43:51 +02:00
|
|
|
# Use LOGIN command, unless LOGINDISABLED is advertized
|
|
|
|
# (per RFC 2595)
|
|
|
|
if 'LOGINDISABLED' in imapobj.capabilities:
|
|
|
|
raise OfflineImapError("IMAP LOGIN is "
|
2020-08-29 20:19:23 +02:00
|
|
|
"disabled by server. Need to use SSL?",
|
|
|
|
OfflineImapError.ERROR.REPO)
|
2013-08-07 13:43:51 +02:00
|
|
|
else:
|
2014-03-16 13:27:35 +01:00
|
|
|
self.__loginauth(imapobj)
|
2013-08-07 13:43:51 +02:00
|
|
|
return True
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __authn_helper(self, imapobj):
|
2015-01-08 17:13:33 +01:00
|
|
|
"""Authentication machinery for self.acquireconnection().
|
2013-08-05 23:10:10 +02:00
|
|
|
|
|
|
|
Raises OfflineImapError() of type ERROR.REPO when
|
|
|
|
there are either fatal problems or no authentications
|
|
|
|
succeeded.
|
|
|
|
|
|
|
|
If any authentication method succeeds, routine should exit:
|
|
|
|
warnings for failed methods are to be produced in the
|
2015-01-08 17:13:33 +01:00
|
|
|
respective except blocks."""
|
2013-08-05 23:10:10 +02:00
|
|
|
|
2016-07-29 16:38:58 +02:00
|
|
|
# Stack stores pairs of (method name, exception)
|
|
|
|
exc_stack = []
|
|
|
|
tried_to_authn = False
|
|
|
|
tried_tls = False
|
2013-08-07 13:43:51 +02:00
|
|
|
# Authentication routines, hash keyed by method name
|
|
|
|
# with value that is a tuple with
|
|
|
|
# - authentication function,
|
|
|
|
# - tryTLS flag,
|
|
|
|
# - check IMAP capability flag.
|
|
|
|
auth_methods = {
|
2020-08-29 20:19:23 +02:00
|
|
|
"GSSAPI": (self.__authn_gssapi, False, True),
|
|
|
|
"XOAUTH2": (self.__authn_xoauth2, True, True),
|
|
|
|
"CRAM-MD5": (self.__authn_cram_md5, True, True),
|
|
|
|
"PLAIN": (self.__authn_plain, True, True),
|
|
|
|
"LOGIN": (self.__authn_login, True, False),
|
2013-08-07 13:43:51 +02:00
|
|
|
}
|
2013-08-05 23:10:10 +02:00
|
|
|
|
2016-07-29 16:38:58 +02:00
|
|
|
# GSSAPI is tried first by default: we will probably go TLS after it and
|
|
|
|
# GSSAPI mustn't be tunneled over TLS.
|
|
|
|
for m in self.authmechs:
|
2013-08-07 13:43:51 +02:00
|
|
|
if m not in auth_methods:
|
|
|
|
raise Exception("Bad authentication method %s, "
|
2020-08-29 20:19:23 +02:00
|
|
|
"please, file OfflineIMAP bug" % m)
|
2013-08-05 23:10:10 +02:00
|
|
|
|
2013-08-07 13:43:51 +02:00
|
|
|
func, tryTLS, check_cap = auth_methods[m]
|
|
|
|
|
|
|
|
# TLS must be initiated before checking capabilities:
|
|
|
|
# they could have been changed after STARTTLS.
|
2016-06-23 03:55:00 +02:00
|
|
|
if tryTLS and self.starttls and not tried_tls:
|
2013-08-07 13:43:51 +02:00
|
|
|
tried_tls = True
|
2014-03-16 13:27:35 +01:00
|
|
|
self.__start_tls(imapobj)
|
2013-08-07 13:43:51 +02:00
|
|
|
|
|
|
|
if check_cap:
|
|
|
|
cap = "AUTH=" + m
|
|
|
|
if cap not in imapobj.capabilities:
|
|
|
|
continue
|
2013-08-03 14:06:44 +02:00
|
|
|
|
2013-08-05 23:10:10 +02:00
|
|
|
tried_to_authn = True
|
2020-08-28 03:32:43 +02:00
|
|
|
self.ui.debug('imap', 'Attempting '
|
2020-08-29 20:19:23 +02:00
|
|
|
'%s authentication' % m)
|
2013-08-05 23:10:10 +02:00
|
|
|
try:
|
2013-08-07 13:43:51 +02:00
|
|
|
if func(imapobj):
|
|
|
|
return
|
|
|
|
except (imapobj.error, OfflineImapError) as e:
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.warn('%s authentication failed: %s' % (m, e))
|
2013-08-07 13:43:51 +02:00
|
|
|
exc_stack.append((m, e))
|
2013-08-05 23:10:10 +02:00
|
|
|
|
|
|
|
if len(exc_stack):
|
2016-08-04 23:53:25 +02:00
|
|
|
msg = "\n\t".join([": ".join((x[0], str(x[1]))) for x in exc_stack])
|
2013-08-05 23:10:10 +02:00
|
|
|
raise OfflineImapError("All authentication types "
|
2020-08-29 20:19:23 +02:00
|
|
|
"failed:\n\t%s" % msg, OfflineImapError.ERROR.REPO)
|
2013-08-05 23:10:10 +02:00
|
|
|
|
|
|
|
if not tried_to_authn:
|
2016-11-05 16:38:29 +01:00
|
|
|
methods = ", ".join([x[5:] for x in
|
2020-08-29 20:19:23 +02:00
|
|
|
[x for x in imapobj.capabilities if x[0:5] == "AUTH="]])
|
2020-08-28 03:32:43 +02:00
|
|
|
raise OfflineImapError("Repository %s: no supported "
|
2020-08-29 20:19:23 +02:00
|
|
|
"authentication mechanisms found; configured %s, "
|
|
|
|
"server advertises %s" % (self.repos,
|
|
|
|
", ".join(self.authmechs), methods),
|
|
|
|
OfflineImapError.ERROR.REPO)
|
2013-08-05 23:10:10 +02:00
|
|
|
|
2016-09-23 16:27:22 +02:00
|
|
|
def __verifycert(self, cert, hostname):
|
|
|
|
"""Verify that cert (in socket.getpeercert() format) matches hostname.
|
|
|
|
|
|
|
|
CRLs are not handled.
|
|
|
|
Returns error message if any problems are found and None on success."""
|
|
|
|
|
|
|
|
errstr = "CA Cert verifying failed: "
|
|
|
|
if not cert:
|
2020-08-30 18:31:30 +02:00
|
|
|
return '%s no certificate received' % errstr
|
2016-09-23 16:27:22 +02:00
|
|
|
dnsname = hostname.lower()
|
|
|
|
certnames = []
|
|
|
|
|
|
|
|
# cert expired?
|
|
|
|
notafter = cert.get('notAfter')
|
|
|
|
if notafter:
|
|
|
|
if time.time() >= cert_time_to_seconds(notafter):
|
2020-08-29 20:19:23 +02:00
|
|
|
return '%s certificate expired %s' % (errstr, notafter)
|
2016-09-23 16:27:22 +02:00
|
|
|
|
|
|
|
# First read commonName
|
|
|
|
for s in cert.get('subject', []):
|
|
|
|
key, value = s[0]
|
|
|
|
if key == 'commonName':
|
|
|
|
certnames.append(value.lower())
|
|
|
|
if len(certnames) == 0:
|
2020-08-30 18:31:30 +02:00
|
|
|
return '%s no commonName found in certificate' % errstr
|
2016-09-23 16:27:22 +02:00
|
|
|
|
|
|
|
# Then read subjectAltName
|
|
|
|
for key, value in cert.get('subjectAltName', []):
|
|
|
|
if key == 'DNS':
|
|
|
|
certnames.append(value.lower())
|
|
|
|
|
|
|
|
# And finally try to match hostname with one of these names
|
|
|
|
for certname in certnames:
|
|
|
|
if (certname == dnsname or
|
2020-08-29 20:19:23 +02:00
|
|
|
'.' in dnsname and certname == '*.' + dnsname.split('.', 1)[1]):
|
2016-09-23 16:27:22 +02:00
|
|
|
return None
|
|
|
|
|
2020-08-30 18:31:30 +02:00
|
|
|
return '%s no matching domain name found in certificate' % errstr
|
2013-08-05 23:10:10 +02:00
|
|
|
|
2002-07-03 15:04:40 +02:00
|
|
|
def acquireconnection(self):
|
|
|
|
"""Fetches a connection from the pool, making sure to create a new one
|
|
|
|
if needed, to obey the maximum connection limits, etc.
|
|
|
|
Opens a connection to the server and returns an appropriate
|
2002-06-19 06:39:00 +02:00
|
|
|
object."""
|
|
|
|
|
2002-07-03 15:04:40 +02:00
|
|
|
self.semaphore.acquire()
|
|
|
|
self.connectionlock.acquire()
|
2012-02-05 12:34:27 +01:00
|
|
|
curThread = currentThread()
|
2002-06-19 06:39:00 +02:00
|
|
|
imapobj = None
|
2002-07-03 15:04:40 +02:00
|
|
|
|
2020-11-08 21:30:22 +01:00
|
|
|
imap_debug = 0
|
|
|
|
if 'imap' in self.ui.debuglist:
|
|
|
|
imap_debug = 5
|
|
|
|
|
2020-08-29 20:19:23 +02:00
|
|
|
if len(self.availableconnections): # One is available.
|
2002-07-11 05:51:20 +02:00
|
|
|
# Try to find one that previously belonged to this thread
|
|
|
|
# as an optimization. Start from the back since that's where
|
|
|
|
# they're popped on.
|
|
|
|
for i in range(len(self.availableconnections) - 1, -1, -1):
|
|
|
|
tryobj = self.availableconnections[i]
|
2012-02-05 12:34:27 +01:00
|
|
|
if self.lastowner[tryobj] == curThread.ident:
|
2002-07-11 05:51:20 +02:00
|
|
|
imapobj = tryobj
|
2020-08-29 20:19:23 +02:00
|
|
|
del (self.availableconnections[i])
|
2002-07-11 05:51:20 +02:00
|
|
|
break
|
|
|
|
if not imapobj:
|
|
|
|
imapobj = self.availableconnections[0]
|
2020-08-29 20:19:23 +02:00
|
|
|
del (self.availableconnections[0])
|
2002-07-03 15:04:40 +02:00
|
|
|
self.assignedconnections.append(imapobj)
|
2012-02-05 12:34:27 +01:00
|
|
|
self.lastowner[imapobj] = curThread.ident
|
2002-07-03 15:04:40 +02:00
|
|
|
self.connectionlock.release()
|
|
|
|
return imapobj
|
2013-07-21 21:00:23 +02:00
|
|
|
|
2020-08-29 20:19:23 +02:00
|
|
|
self.connectionlock.release() # Release until need to modify data
|
2002-07-03 15:04:40 +02:00
|
|
|
|
2015-01-08 17:13:33 +01:00
|
|
|
# Must be careful here that if we fail we should bail out gracefully
|
|
|
|
# and release locks / threads so that the next attempt can try...
|
2016-05-19 08:32:38 +02:00
|
|
|
success = False
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
try:
|
2016-05-19 08:32:38 +02:00
|
|
|
while success is not True:
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
# Generate a new connection.
|
|
|
|
if self.tunnel:
|
2016-07-29 05:25:06 +02:00
|
|
|
self.ui.connecting(
|
|
|
|
self.repos.getname(), 'tunnel', self.tunnel)
|
2015-02-15 15:16:20 +01:00
|
|
|
imapobj = imaplibutil.IMAP4_Tunnel(
|
|
|
|
self.tunnel,
|
|
|
|
timeout=socket.getdefaulttimeout(),
|
2020-11-08 21:30:22 +01:00
|
|
|
debug=imap_debug,
|
2015-02-15 15:16:20 +01:00
|
|
|
use_socket=self.proxied_socket,
|
2020-08-29 20:19:23 +02:00
|
|
|
)
|
2016-05-19 08:32:38 +02:00
|
|
|
success = True
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
elif self.usessl:
|
2016-07-29 05:25:06 +02:00
|
|
|
self.ui.connecting(
|
|
|
|
self.repos.getname(), self.hostname, self.port)
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.debug('imap', "%s: level '%s', version '%s'" %
|
|
|
|
(self.repos.getname(), self.tlslevel, self.sslversion))
|
2015-02-15 15:16:20 +01:00
|
|
|
imapobj = imaplibutil.WrappedIMAP4_SSL(
|
2016-07-25 03:20:53 +02:00
|
|
|
host=self.hostname,
|
|
|
|
port=self.port,
|
|
|
|
keyfile=self.sslclientkey,
|
|
|
|
certfile=self.sslclientcert,
|
|
|
|
ca_certs=self.sslcacertfile,
|
|
|
|
cert_verify_cb=self.__verifycert,
|
|
|
|
ssl_version=self.sslversion,
|
2020-11-08 21:30:22 +01:00
|
|
|
debug=imap_debug,
|
2015-02-15 15:16:20 +01:00
|
|
|
timeout=socket.getdefaulttimeout(),
|
|
|
|
fingerprint=self.fingerprint,
|
|
|
|
use_socket=self.proxied_socket,
|
2015-08-25 05:32:00 +02:00
|
|
|
tls_level=self.tlslevel,
|
2016-02-23 01:45:18 +01:00
|
|
|
af=self.af,
|
2020-08-29 20:19:23 +02:00
|
|
|
)
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
else:
|
2016-07-29 05:25:06 +02:00
|
|
|
self.ui.connecting(
|
|
|
|
self.repos.getname(), self.hostname, self.port)
|
2015-02-15 15:16:20 +01:00
|
|
|
imapobj = imaplibutil.WrappedIMAP4(
|
|
|
|
self.hostname, self.port,
|
|
|
|
timeout=socket.getdefaulttimeout(),
|
|
|
|
use_socket=self.proxied_socket,
|
2020-11-08 21:30:22 +01:00
|
|
|
debug=imap_debug,
|
2016-02-23 01:45:18 +01:00
|
|
|
af=self.af,
|
2020-08-29 20:19:23 +02:00
|
|
|
)
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
|
2021-10-11 22:54:54 +02:00
|
|
|
# If 'ID' extension is used by the server, we should use it
|
|
|
|
if 'ID' in imapobj.capabilities:
|
|
|
|
l_str = '("name" "OfflineIMAP" "version" "{}")'.format(offlineimap.__version__)
|
|
|
|
imapobj.id(l_str)
|
|
|
|
|
2013-05-03 15:56:20 +02:00
|
|
|
if not self.preauth_tunnel:
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
try:
|
2014-03-16 13:27:35 +01:00
|
|
|
self.__authn_helper(imapobj)
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
self.goodpassword = self.password
|
2016-05-19 08:32:38 +02:00
|
|
|
success = True
|
2013-08-05 23:10:10 +02:00
|
|
|
except OfflineImapError as e:
|
|
|
|
self.passworderror = str(e)
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
raise
|
|
|
|
|
2012-10-25 14:25:19 +02:00
|
|
|
# Enable compression
|
|
|
|
if self.repos.getconfboolean('usecompression', 0):
|
|
|
|
imapobj.enable_compression()
|
|
|
|
|
2011-09-21 02:16:33 +02:00
|
|
|
# update capabilities after login, e.g. gmail serves different ones
|
|
|
|
typ, dat = imapobj.capability()
|
|
|
|
if dat != [None]:
|
2020-11-07 15:52:06 +01:00
|
|
|
# Get the capabilities and convert them to string from bytes
|
|
|
|
s_dat = [x.decode('utf-8') for x in dat[-1].upper().split()]
|
|
|
|
imapobj.capabilities = tuple(s_dat)
|
2011-09-21 02:16:33 +02:00
|
|
|
|
2020-08-30 11:27:03 +02:00
|
|
|
if self.delim is None:
|
2020-08-31 18:11:48 +02:00
|
|
|
listres = imapobj.list(self.reference, '""')[1]
|
2020-08-30 11:27:03 +02:00
|
|
|
if listres == [None] or listres is None:
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
# Some buggy IMAP servers do not respond well to LIST "" ""
|
|
|
|
# Work around them.
|
|
|
|
listres = imapobj.list(self.reference, '"*"')[1]
|
2020-08-30 11:27:03 +02:00
|
|
|
if listres == [None] or listres is None:
|
2011-03-03 10:43:03 +01:00
|
|
|
# No Folders were returned. This occurs, e.g. if the
|
|
|
|
# 'reference' prefix does not exist on the mail
|
|
|
|
# server. Raise exception.
|
2020-08-29 20:19:23 +02:00
|
|
|
err = "Server '%s' returned no folders in '%s'" % \
|
|
|
|
(self.repos.getname(), self.reference)
|
2011-03-03 10:43:03 +01:00
|
|
|
self.ui.warn(err)
|
|
|
|
raise Exception(err)
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
self.delim, self.root = \
|
2020-08-29 20:19:23 +02:00
|
|
|
imaputil.imapsplit(listres[0])[1:]
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
self.delim = imaputil.dequote(self.delim)
|
|
|
|
self.root = imaputil.dequote(self.root)
|
|
|
|
|
2013-07-21 22:08:39 +02:00
|
|
|
with self.connectionlock:
|
|
|
|
self.assignedconnections.append(imapobj)
|
|
|
|
self.lastowner[imapobj] = curThread.ident
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
return imapobj
|
2012-02-05 10:14:23 +01:00
|
|
|
except Exception as e:
|
2011-05-04 16:45:25 +02:00
|
|
|
"""If we are here then we did not succeed in getting a
|
|
|
|
connection - we should clean up and then re-raise the
|
|
|
|
error..."""
|
2015-01-08 17:13:33 +01:00
|
|
|
|
Patch for error handling / separation of accounts etc.
Dear All,
I have made the attached patch to try and make offlineimap a bit more
stable in challenging situations. It's extremely useful in slow
connection environments - but sometimes if one account had the wrong
password or the connection went down then unfortunately the whole
program would crash.
I have tested this on our connection and tried throwing at it just about
every situation - connection, up down, up, down again, change password,
error whilst copying one message, etc. I have been running this patch
for the last 5 days or so syncing 6 accounts at the moment... It seems
to work and stay alive nicely (even if your connection does not)...
Hope that this can go in for the next release... Please let me know if
anyone notices any problems with this...
Regards,
-Mike
-- Attached file included as plaintext by Ecartis --
-- File: submit
From 1d6777cab23637eb830031c7cab0ae9b8589afd6 Mon Sep 17 00:00:00 2001
From: mike <mike@mikelaptop.(none)>
Date: Mon, 24 Aug 2009 19:37:59 +0430
Subject: [PATCH] This patch attempts to introduce a little more error handling - e.g.
if one account has an error because of a changed password or something
that should not affect the other accounts.
Specifically:
If one sync run has an issue this is in a try-except clause - if it
has an auto refresh period the thread will sleep and try again - this
could be quite useful in the event of the connection going down for a
little while, changed password etc.
If one folder cannot be created an error message will be displayed through
the UI and the program will continue (e.g. permission denied to create a folder)
If one message does not want to copy for whatever resaon an error message
will be displayed through the UI and at least the other messages will
be copied
If one folder run has an exception then the others will still run
2009-08-24 17:17:57 +02:00
|
|
|
self.semaphore.release()
|
|
|
|
|
2011-06-27 11:08:54 +02:00
|
|
|
severity = OfflineImapError.ERROR.REPO
|
2011-06-16 23:38:55 +02:00
|
|
|
if type(e) == gaierror:
|
2020-08-29 20:19:23 +02:00
|
|
|
# DNS related errors. Abort Repo sync
|
|
|
|
# TODO: special error msg for e.errno == 2 "Name or service not known"?
|
|
|
|
reason = "Could not resolve name '%s' for repository " \
|
|
|
|
"'%s'. Make sure you have configured the ser" \
|
|
|
|
"ver name correctly and that you are online." % \
|
2011-08-12 08:31:09 +02:00
|
|
|
(self.hostname, self.repos)
|
2020-09-03 21:02:19 +02:00
|
|
|
raise OfflineImapError(reason, severity, exc_info()[2])
|
2011-06-06 11:04:55 +02:00
|
|
|
|
2021-10-30 23:55:00 +02:00
|
|
|
if isinstance(e, SSLError) and e.errno == errno.EPERM:
|
2011-06-06 11:04:55 +02:00
|
|
|
# SSL unknown protocol error
|
|
|
|
# happens e.g. when connecting via SSL to a non-SSL service
|
2011-08-22 10:33:55 +02:00
|
|
|
if self.port != 993:
|
2020-08-29 20:19:23 +02:00
|
|
|
reason = "Could not connect via SSL to host '%s' and non-s" \
|
|
|
|
"tandard ssl port %d configured. Make sure you connect" \
|
|
|
|
" to the correct port. Got: %s" % (
|
|
|
|
self.hostname, self.port, e)
|
2011-06-06 11:04:55 +02:00
|
|
|
else:
|
2020-08-29 20:19:23 +02:00
|
|
|
reason = "Unknown SSL protocol connecting to host '%s' for " \
|
|
|
|
"repository '%s'. OpenSSL responded:\n%s" \
|
|
|
|
% (self.hostname, self.repos, e)
|
2020-09-03 21:02:19 +02:00
|
|
|
raise OfflineImapError(reason, severity, exc_info()[2])
|
2011-06-06 11:04:55 +02:00
|
|
|
|
2021-10-30 23:55:00 +02:00
|
|
|
if isinstance(e, socket.error) and e.args and e.args[0] == errno.ECONNREFUSED:
|
2011-06-06 11:04:55 +02:00
|
|
|
# "Connection refused", can be a non-existing port, or an unauthorized
|
|
|
|
# webproxy (open WLAN?)
|
2020-08-29 20:19:23 +02:00
|
|
|
reason = "Connection to host '%s:%d' for repository '%s' was " \
|
|
|
|
"refused. Make sure you have the right host and port " \
|
|
|
|
"configured and that you are actually able to access the " \
|
|
|
|
"network." % (self.hostname, self.port, self.repos)
|
2020-09-03 21:02:19 +02:00
|
|
|
raise OfflineImapError(reason, severity, exc_info()[2])
|
|
|
|
|
2011-05-24 23:45:39 +02:00
|
|
|
# Could not acquire connection to the remote;
|
|
|
|
# socket.error(last_error) raised
|
|
|
|
if str(e)[:24] == "can't open socket; error":
|
2020-09-03 21:02:19 +02:00
|
|
|
raise OfflineImapError(
|
|
|
|
"Could not connect to remote server '%s' "
|
|
|
|
"for repository '%s'. Remote does not answer." % (self.hostname, self.repos),
|
|
|
|
OfflineImapError.ERROR.REPO,
|
|
|
|
exc_info()[2])
|
2021-10-30 23:55:00 +02:00
|
|
|
if e.args:
|
|
|
|
try:
|
|
|
|
if e.args[0][:35] == 'IMAP4 protocol error: socket error:':
|
|
|
|
raise OfflineImapError(
|
|
|
|
"Could not connect to remote server '{}' "
|
|
|
|
"for repository '{}'. Connection Refused.".format(
|
|
|
|
self.hostname, self.repos),
|
|
|
|
OfflineImapError.ERROR.CRITICAL)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# re-raise all other errors
|
|
|
|
raise
|
2011-11-02 11:30:16 +01:00
|
|
|
|
2002-07-03 15:04:40 +02:00
|
|
|
def connectionwait(self):
|
2015-01-08 17:13:33 +01:00
|
|
|
"""Waits until there is a connection available.
|
|
|
|
|
|
|
|
Note that between the time that a connection becomes available and the
|
|
|
|
time it is requested, another thread may have grabbed it. This function
|
|
|
|
is mainly present as a way to avoid spawning thousands of threads to
|
|
|
|
copy messages, then have them all wait for 3 available connections.
|
|
|
|
It's OK if we have maxconnections + 1 or 2 threads, which is what this
|
|
|
|
will help us do."""
|
|
|
|
|
2020-08-29 20:19:23 +02:00
|
|
|
self.semaphore.acquire() # Blocking until maxconnections has free slots.
|
2011-05-11 20:55:54 +02:00
|
|
|
self.semaphore.release()
|
2002-07-03 15:04:40 +02:00
|
|
|
|
2002-06-21 09:25:24 +02:00
|
|
|
def close(self):
|
2002-07-03 15:04:40 +02:00
|
|
|
# Make sure I own all the semaphores. Let the threads finish
|
|
|
|
# their stuff. This is a blocking method.
|
2011-11-02 11:30:16 +01:00
|
|
|
with self.connectionlock:
|
2011-11-02 11:56:04 +01:00
|
|
|
# first, wait till all connections had been released.
|
|
|
|
# TODO: won't work IMHO, as releaseconnection() also
|
|
|
|
# requires the connectionlock, leading to a potential
|
|
|
|
# deadlock! Audit & check!
|
2011-11-02 11:30:16 +01:00
|
|
|
threadutil.semaphorereset(self.semaphore, self.maxconnections)
|
|
|
|
for imapobj in self.assignedconnections + self.availableconnections:
|
|
|
|
imapobj.logout()
|
|
|
|
self.assignedconnections = []
|
|
|
|
self.availableconnections = []
|
|
|
|
self.lastowner = {}
|
2018-02-27 00:08:00 +01:00
|
|
|
# reset GSSAPI state
|
2011-11-02 11:30:16 +01:00
|
|
|
self.gss_vc = None
|
|
|
|
self.gssapi = False
|
2002-07-03 15:04:40 +02:00
|
|
|
|
2002-07-10 13:18:07 +02:00
|
|
|
def keepalive(self, timeout, event):
|
2015-01-08 17:13:33 +01:00
|
|
|
"""Sends a NOOP to each connection recorded.
|
|
|
|
|
|
|
|
It will wait a maximum of timeout seconds between doing this, and will
|
|
|
|
continue to do so until the Event object as passed is true. This method
|
|
|
|
is expected to be invoked in a separate thread, which should be join()'d
|
|
|
|
after the event is set."""
|
|
|
|
|
2011-01-05 17:00:55 +01:00
|
|
|
self.ui.debug('imap', 'keepalive thread started')
|
2011-10-27 16:56:18 +02:00
|
|
|
while not event.isSet():
|
2002-07-10 13:18:07 +02:00
|
|
|
self.connectionlock.acquire()
|
|
|
|
numconnections = len(self.assignedconnections) + \
|
|
|
|
len(self.availableconnections)
|
|
|
|
self.connectionlock.release()
|
2011-10-27 16:56:18 +02:00
|
|
|
|
2002-07-10 13:18:07 +02:00
|
|
|
threads = []
|
|
|
|
for i in range(numconnections):
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.debug('imap', 'keepalive: processing connection %d of %d' %
|
|
|
|
(i, numconnections))
|
2011-05-19 21:02:28 +02:00
|
|
|
if len(self.idlefolders) > i:
|
2011-10-27 16:56:18 +02:00
|
|
|
# IDLE thread
|
2011-05-19 21:02:28 +02:00
|
|
|
idler = IdleThread(self, self.idlefolders[i])
|
|
|
|
else:
|
2011-10-27 16:56:18 +02:00
|
|
|
# NOOP thread
|
2011-05-19 21:02:28 +02:00
|
|
|
idler = IdleThread(self)
|
|
|
|
idler.start()
|
|
|
|
threads.append(idler)
|
2003-06-02 21:06:18 +02:00
|
|
|
|
2011-05-19 21:02:28 +02:00
|
|
|
self.ui.debug('imap', 'keepalive: waiting for timeout')
|
|
|
|
event.wait(timeout)
|
|
|
|
self.ui.debug('imap', 'keepalive: after wait')
|
|
|
|
|
|
|
|
for idler in threads:
|
2002-07-10 13:18:07 +02:00
|
|
|
# Make sure all the commands have completed.
|
2011-05-19 21:02:28 +02:00
|
|
|
idler.stop()
|
|
|
|
idler.join()
|
2011-10-27 16:56:18 +02:00
|
|
|
self.ui.debug('imap', 'keepalive: all threads joined')
|
|
|
|
self.ui.debug('imap', 'keepalive: event is set; exiting')
|
|
|
|
return
|
2011-08-15 11:55:42 +02:00
|
|
|
|
2016-09-23 16:27:22 +02:00
|
|
|
def releaseconnection(self, connection, drop_conn=False):
|
|
|
|
"""Releases a connection, returning it to the pool.
|
2011-08-15 11:55:42 +02:00
|
|
|
|
2020-08-30 18:37:09 +02:00
|
|
|
:param connection: Connection object
|
2016-09-23 16:27:22 +02:00
|
|
|
:param drop_conn: If True, the connection will be released and
|
|
|
|
not be reused. This can be used to indicate broken connections."""
|
2011-08-15 11:55:42 +02:00
|
|
|
|
2016-09-23 16:27:22 +02:00
|
|
|
if connection is None:
|
2020-08-29 20:19:23 +02:00
|
|
|
return # Noop on bad connection.
|
2011-08-15 11:55:42 +02:00
|
|
|
|
2016-09-23 16:27:22 +02:00
|
|
|
self.connectionlock.acquire()
|
|
|
|
self.assignedconnections.remove(connection)
|
|
|
|
# Don't reuse broken connections
|
|
|
|
if connection.Terminate or drop_conn:
|
|
|
|
connection.logout()
|
|
|
|
else:
|
|
|
|
self.availableconnections.append(connection)
|
|
|
|
self.connectionlock.release()
|
|
|
|
self.semaphore.release()
|
2011-08-15 11:55:42 +02:00
|
|
|
|
|
|
|
|
2020-08-30 18:31:30 +02:00
|
|
|
class IdleThread:
|
2011-05-19 21:02:27 +02:00
|
|
|
def __init__(self, parent, folder=None):
|
2011-09-12 10:37:54 +02:00
|
|
|
"""If invoked without 'folder', perform a NOOP and wait for
|
|
|
|
self.stop() to be called. If invoked with folder, switch to IDLE
|
|
|
|
mode and synchronize once we have a new message"""
|
2015-01-08 17:13:33 +01:00
|
|
|
|
2011-05-19 21:02:27 +02:00
|
|
|
self.parent = parent
|
|
|
|
self.folder = folder
|
2011-09-12 10:37:55 +02:00
|
|
|
self.stop_sig = Event()
|
2011-09-01 11:01:00 +02:00
|
|
|
self.ui = getglobalui()
|
2011-05-19 21:02:27 +02:00
|
|
|
if folder is None:
|
|
|
|
self.thread = Thread(target=self.noop)
|
|
|
|
else:
|
2014-05-06 23:17:52 +02:00
|
|
|
self.thread = Thread(target=self.__idle)
|
2020-10-31 15:35:54 +01:00
|
|
|
self.thread.setDaemon(True)
|
2011-05-19 21:02:27 +02:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.thread.start()
|
|
|
|
|
|
|
|
def stop(self):
|
2011-09-12 10:37:55 +02:00
|
|
|
self.stop_sig.set()
|
2011-05-19 21:02:27 +02:00
|
|
|
|
|
|
|
def join(self):
|
|
|
|
self.thread.join()
|
|
|
|
|
|
|
|
def noop(self):
|
2015-01-08 17:13:33 +01:00
|
|
|
# TODO: AFAIK this is not optimal, we will send a NOOP on one
|
|
|
|
# random connection (ie not enough to keep all connections
|
|
|
|
# open). In case we do the noop multiple times, we can well use
|
|
|
|
# the same connection every time, as we get a random one. This
|
|
|
|
# function should IMHO send a noop on ALL available connections
|
|
|
|
# to the server.
|
2011-05-19 21:02:27 +02:00
|
|
|
imapobj = self.parent.acquireconnection()
|
2011-10-05 18:18:55 +02:00
|
|
|
try:
|
|
|
|
imapobj.noop()
|
|
|
|
except imapobj.abort:
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.warn('Attempting NOOP on dropped connection %s' %
|
|
|
|
imapobj.identifier)
|
2011-10-05 18:18:55 +02:00
|
|
|
self.parent.releaseconnection(imapobj, True)
|
|
|
|
imapobj = None
|
|
|
|
finally:
|
|
|
|
if imapobj:
|
|
|
|
self.parent.releaseconnection(imapobj)
|
2020-08-29 20:19:23 +02:00
|
|
|
self.stop_sig.wait() # wait until we are supposed to quit
|
2011-05-19 21:02:27 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __dosync(self):
|
2011-05-19 21:02:27 +02:00
|
|
|
remoterepos = self.parent.repos
|
|
|
|
account = remoterepos.account
|
|
|
|
remoterepos = account.remoterepos
|
2017-10-02 01:26:29 +02:00
|
|
|
remotefolder = remoterepos.getfolder(self.folder, decode=False)
|
2013-07-04 05:48:12 +02:00
|
|
|
|
|
|
|
hook = account.getconf('presynchook', '')
|
2021-09-07 09:38:10 +02:00
|
|
|
account.callhook(hook, "idle")
|
2011-11-03 13:27:35 +01:00
|
|
|
offlineimap.accounts.syncfolder(account, remotefolder, quick=False)
|
2013-07-04 05:48:12 +02:00
|
|
|
hook = account.getconf('postsynchook', '')
|
2021-09-07 09:38:10 +02:00
|
|
|
account.callhook(hook, "idle")
|
2013-07-04 05:48:12 +02:00
|
|
|
|
2011-05-19 21:02:27 +02:00
|
|
|
ui = getglobalui()
|
2020-08-29 20:19:23 +02:00
|
|
|
ui.unregisterthread(currentThread()) # syncfolder registered the thread
|
2011-05-19 21:02:27 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
def __idle(self):
|
2015-03-18 23:09:34 +01:00
|
|
|
"""Invoke IDLE mode until timeout or self.stop() is invoked."""
|
|
|
|
|
2011-09-12 10:37:53 +02:00
|
|
|
def callback(args):
|
2015-03-18 23:09:34 +01:00
|
|
|
"""IDLE callback function invoked by imaplib2.
|
2011-09-12 10:37:54 +02:00
|
|
|
|
|
|
|
This is invoked when a) The IMAP server tells us something
|
|
|
|
while in IDLE mode, b) we get an Exception (e.g. on dropped
|
|
|
|
connections, or c) the standard imaplib IDLE timeout of 29
|
|
|
|
minutes kicks in."""
|
2016-09-13 04:41:31 +02:00
|
|
|
|
2011-09-12 10:37:53 +02:00
|
|
|
result, cb_arg, exc_data = args
|
2011-09-12 10:37:57 +02:00
|
|
|
if exc_data is None and not self.stop_sig.isSet():
|
|
|
|
# No Exception, and we are not supposed to stop:
|
|
|
|
self.needsync = True
|
2020-08-29 20:19:23 +02:00
|
|
|
self.stop_sig.set() # Continue to sync.
|
2011-09-12 10:37:53 +02:00
|
|
|
|
2016-09-13 04:41:31 +02:00
|
|
|
def noop(imapobj):
|
|
|
|
"""Factorize the noop code."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
# End IDLE mode with noop, imapobj can point to a dropped conn.
|
|
|
|
imapobj.noop()
|
|
|
|
except imapobj.abort:
|
2020-08-29 20:19:23 +02:00
|
|
|
self.ui.warn('Attempting NOOP on dropped connection %s' %
|
|
|
|
imapobj.identifier)
|
2016-09-13 04:41:31 +02:00
|
|
|
self.parent.releaseconnection(imapobj, True)
|
|
|
|
else:
|
|
|
|
self.parent.releaseconnection(imapobj)
|
|
|
|
|
2011-09-12 10:37:56 +02:00
|
|
|
while not self.stop_sig.isSet():
|
2011-05-19 21:02:27 +02:00
|
|
|
self.needsync = False
|
2011-05-19 21:02:31 +02:00
|
|
|
|
2020-08-29 20:19:23 +02:00
|
|
|
success = False # Successfully selected FOLDER?
|
2011-09-01 11:01:00 +02:00
|
|
|
while not success:
|
|
|
|
imapobj = self.parent.acquireconnection()
|
|
|
|
try:
|
|
|
|
imapobj.select(self.folder)
|
2012-02-05 10:14:23 +01:00
|
|
|
except OfflineImapError as e:
|
2011-09-01 11:01:00 +02:00
|
|
|
if e.severity == OfflineImapError.ERROR.FOLDER_RETRY:
|
2015-03-18 23:09:34 +01:00
|
|
|
# Connection closed, release connection and retry.
|
2011-09-01 11:01:00 +02:00
|
|
|
self.ui.error(e, exc_info()[2])
|
2011-09-12 10:37:57 +02:00
|
|
|
self.parent.releaseconnection(imapobj, True)
|
2015-03-18 23:09:34 +01:00
|
|
|
elif e.severity == OfflineImapError.ERROR.FOLDER:
|
|
|
|
# Just continue the process on such error for now.
|
|
|
|
self.ui.error(e, exc_info()[2])
|
2011-09-01 11:01:00 +02:00
|
|
|
else:
|
2015-03-18 23:09:34 +01:00
|
|
|
# Stops future attempts to sync this account.
|
2015-01-11 21:44:24 +01:00
|
|
|
raise
|
2011-09-01 11:01:00 +02:00
|
|
|
else:
|
|
|
|
success = True
|
2011-05-22 09:19:29 +02:00
|
|
|
if "IDLE" in imapobj.capabilities:
|
2014-05-23 08:24:55 +02:00
|
|
|
imapobj.idle(callback=callback)
|
2011-05-22 09:19:29 +02:00
|
|
|
else:
|
2011-09-12 10:37:54 +02:00
|
|
|
self.ui.warn("IMAP IDLE not supported on server '%s'."
|
2020-08-29 20:19:23 +02:00
|
|
|
"Sleep until next refresh cycle." % imapobj.identifier)
|
|
|
|
noop(imapobj) # XXX: why?
|
|
|
|
self.stop_sig.wait() # self.stop() or IDLE callback are invoked.
|
2016-09-13 04:41:31 +02:00
|
|
|
noop(imapobj)
|
2011-09-12 10:37:57 +02:00
|
|
|
|
2011-05-19 21:02:27 +02:00
|
|
|
if self.needsync:
|
2015-03-18 23:09:34 +01:00
|
|
|
# Here not via self.stop, but because IDLE responded. Do
|
2011-09-12 10:37:54 +02:00
|
|
|
# another round and invoke actual syncing.
|
2011-09-12 10:37:55 +02:00
|
|
|
self.stop_sig.clear()
|
2014-03-16 13:27:35 +01:00
|
|
|
self.__dosync()
|