IMAP.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. IMAP_WARNED_UNENCRYPTED = False
  35. def is_authenticated(user, password):
  36. """Check if ``user``/``password`` couple is valid."""
  37. global IMAP_WARNED_UNENCRYPTED
  38. if not user or not password:
  39. return False
  40. log.LOGGER.debug(
  41. "Connecting to IMAP server %s:%s." % (IMAP_SERVER, IMAP_SERVER_PORT,))
  42. connection_is_secure = False
  43. if IMAP_USE_SSL:
  44. connection = imaplib.IMAP4_SSL(host=IMAP_SERVER, port=IMAP_SERVER_PORT)
  45. connection_is_secure = True
  46. else:
  47. connection = imaplib.IMAP4(host=IMAP_SERVER, port=IMAP_SERVER_PORT)
  48. server_is_local = (IMAP_SERVER == "localhost")
  49. if not connection_is_secure:
  50. try:
  51. connection.starttls()
  52. log.LOGGER.debug("IMAP server connection changed to TLS.")
  53. connection_is_secure = True
  54. except AttributeError:
  55. if not server_is_local:
  56. log.LOGGER.error(
  57. "Python 3.2 or newer is required for IMAP + TLS.")
  58. except (imaplib.IMAP4.error, imaplib.IMAP4.abort) as exception:
  59. log.LOGGER.warning(
  60. "IMAP server at %s failed to accept TLS connection "
  61. "because of: %s" % (IMAP_SERVER, exception))
  62. if server_is_local and not connection_is_secure and not IMAP_WARNED_UNENCRYPTED:
  63. IMAP_WARNED_UNENCRYPTED = True
  64. log.LOGGER.warning(
  65. "IMAP server is local. "
  66. "Will allow transmitting unencrypted credentials.")
  67. if connection_is_secure or server_is_local:
  68. try:
  69. connection.login(user, password)
  70. connection.logout()
  71. log.LOGGER.debug(
  72. "Authenticated IMAP user %s "
  73. "via %s." % (user, IMAP_SERVER))
  74. return True
  75. except (imaplib.IMAP4.error, imaplib.IMAP4.abort) as exception:
  76. log.LOGGER.error(
  77. "IMAP server could not authenticate user %s "
  78. "because of: %s" % (user, exception))
  79. else:
  80. log.LOGGER.critical(
  81. "IMAP server did not support TLS and is not ``localhost``. "
  82. "Refusing to transmit passwords under these conditions. "
  83. "Authentication attempt aborted.")
  84. return False # authentication failed