IMAP.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2012 Daniel Aleksandersen
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. IMAP authentication.
  20. Secure authentication based on the ``imaplib`` module.
  21. Validating users against a modern IMAP4rev1 server that awaits STARTTLS on
  22. port 143. Legacy SSL (often on legacy port 993) is deprecated and thus
  23. unsupported. STARTTLS is enforced except if host is ``localhost`` as
  24. passwords are sent in PLAIN.
  25. Python 3.2 or newer is required for TLS.
  26. """
  27. import imaplib
  28. from .. import config, log
  29. IMAP_SERVER = config.get("auth", "imap_auth_host_name")
  30. IMAP_SERVER_PORT = config.get("auth", "imap_auth_host_port")
  31. def is_authenticated(user, password):
  32. """Check if ``user``/``password`` couple is valid."""
  33. log.LOGGER.debug(
  34. "[IMAP AUTH] Connecting to %s:%s." % (IMAP_SERVER, IMAP_SERVER_PORT,))
  35. connection = imaplib.IMAP4(host=IMAP_SERVER, port=IMAP_SERVER_PORT)
  36. server_is_local = (IMAP_SERVER == "localhost")
  37. connection_is_secure = False
  38. try:
  39. connection.starttls()
  40. log.LOGGER.debug("IMAP server connection changed to TLS.")
  41. connection_is_secure = True
  42. except AttributeError:
  43. if not server_is_local:
  44. log.LOGGER.error(
  45. "Python 3.2 or newer is required for IMAP + TLS.")
  46. except (imaplib.IMAP4.error, imaplib.IMAP4.abort) as exception:
  47. log.LOGGER.warning(
  48. "IMAP server at %s failed to accept TLS connection "
  49. "because of: %s" % (IMAP_SERVER, exception))
  50. if server_is_local and not connection_is_secure:
  51. log.LOGGER.warning(
  52. "IMAP server is local. "
  53. "Will allow transmitting unencrypted credentials.")
  54. if connection_is_secure or server_is_local:
  55. try:
  56. connection.login(user, password)
  57. connection.logout()
  58. log.LOGGER.debug(
  59. "Authenticated IMAP user %s "
  60. "via %s." % (user, IMAP_SERVER))
  61. return True
  62. except (imaplib.IMAP4.error, imaplib.IMAP4.abort) as exception:
  63. log.LOGGER.error(
  64. "IMAP server could not authenticate user %s "
  65. "because of: %s" % (user, exception))
  66. else:
  67. log.LOGGER.critical(
  68. "IMAP server did not support TLS and is not ``localhost``. "
  69. "Refusing to transmit passwords under these conditions. "
  70. "Authentication attempt aborted.")
  71. return False # authentication failed