IMAP.py 3.4 KB

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