IMAP.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012 Daniel Aleksandersen
  3. # Copyright © 2013 Nikita Koshikov
  4. # Copyright © 2013-2016 Guillaume Ayoub
  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. """
  26. import imaplib
  27. from .. import config, log
  28. IMAP_SERVER = config.get("auth", "imap_hostname")
  29. IMAP_SERVER_PORT = config.getint("auth", "imap_port")
  30. IMAP_USE_SSL = config.getboolean("auth", "imap_ssl")
  31. IMAP_WARNED_UNENCRYPTED = False
  32. def is_authenticated(user, password):
  33. """Check if ``user``/``password`` couple is valid."""
  34. global IMAP_WARNED_UNENCRYPTED
  35. if not user or not password:
  36. return False
  37. log.LOGGER.debug(
  38. "Connecting to IMAP server %s:%s." % (IMAP_SERVER, IMAP_SERVER_PORT,))
  39. connection_is_secure = False
  40. if IMAP_USE_SSL:
  41. connection = imaplib.IMAP4_SSL(host=IMAP_SERVER, port=IMAP_SERVER_PORT)
  42. connection_is_secure = True
  43. else:
  44. connection = imaplib.IMAP4(host=IMAP_SERVER, port=IMAP_SERVER_PORT)
  45. server_is_local = (IMAP_SERVER == "localhost")
  46. if not connection_is_secure:
  47. try:
  48. connection.starttls()
  49. log.LOGGER.debug("IMAP server connection changed to TLS.")
  50. connection_is_secure = True
  51. except AttributeError:
  52. if not server_is_local:
  53. log.LOGGER.error(
  54. "Python 3.2 or newer is required for IMAP + TLS.")
  55. except (imaplib.IMAP4.error, imaplib.IMAP4.abort) as exception:
  56. log.LOGGER.warning(
  57. "IMAP server at %s failed to accept TLS connection "
  58. "because of: %s" % (IMAP_SERVER, exception))
  59. if server_is_local and not connection_is_secure and not IMAP_WARNED_UNENCRYPTED:
  60. IMAP_WARNED_UNENCRYPTED = True
  61. log.LOGGER.warning(
  62. "IMAP server is local. "
  63. "Will allow transmitting unencrypted credentials.")
  64. if connection_is_secure or server_is_local:
  65. try:
  66. connection.login(user, password)
  67. connection.logout()
  68. log.LOGGER.debug(
  69. "Authenticated IMAP user %s "
  70. "via %s." % (user, IMAP_SERVER))
  71. return True
  72. except (imaplib.IMAP4.error, imaplib.IMAP4.abort) as exception:
  73. log.LOGGER.error(
  74. "IMAP server could not authenticate user %s "
  75. "because of: %s" % (user, exception))
  76. else:
  77. log.LOGGER.critical(
  78. "IMAP server did not support TLS and is not ``localhost``. "
  79. "Refusing to transmit passwords under these conditions. "
  80. "Authentication attempt aborted.")
  81. return False # authentication failed